2
我正試圖在偏置+權重上實現L1規範。爲此,我嘗試將它們連接在一起並採取一種平均值。tensorflow並置錯誤:一個神祕的字符串格式問題
即,我有一個偏壓b1
(形狀:[1,1]
)和重量W1
(形狀:[1, xlen]
)。於是,我就天真地沿着第一維串連:
self.W1 = tf.Variable(tf.truncated_normal([1, self.xlen], stddev=0.1), name="weight")
self.b1 = tf.Variable(tf.constant(0.1, shape=[1, 1]), name="bias")
...
l1_penalty = tf.reduce_mean(tf.abs(tf.concat(1, (self.W1,self.b1))))
但是我得到:
---> 53 l1_penalty = tf.reduce_mean(tf.abs(tf.concat(1, (self.W1,self.b1))))
54
55 tot_loss = l2_loss + self.ALPHA * l1_penalty
/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/array_ops.py in concat(concat_dim, values, name)
304 # TODO(mrry): Change to return values?
305 if len(values) == 1: # Degenerate case of one tensor.
--> 306 return identity(values[0], name=name)
307 return gen_array_ops._concat(concat_dim=concat_dim,
308 values=values,
/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/gen_array_ops.py in identity(input, name)
313 A `Tensor`. Has the same type as `input`.
314 """
--> 315 return _op_def_lib.apply_op("Identity", input=input, name=name)
316
317
/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/op_def_library.py in apply_op(self, op_type_name, g, name, **keywords)
419 values = ops.convert_to_tensor(
420 values, name=input_arg.name, dtype=dtype,
--> 421 as_ref=input_arg.is_ref)
422 except ValueError:
423 # What type does convert_to_tensor think it has?
/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref)
522 for base_type, conversion_func in funcs_at_priority:
523 if isinstance(value, base_type):
--> 524 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
525 if not isinstance(ret, Tensor):
526 raise RuntimeError(
/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
176 as_ref=False):
177 _ = as_ref
--> 178 return constant(v, dtype=dtype, name=name)
179
180
/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/constant_op.py in constant(value, dtype, shape, name)
159 tensor_value = attr_value_pb2.AttrValue()
160 tensor_value.tensor.CopyFrom(
--> 161 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
162 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
163 const_tensor = g.create_op(
/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape)
382 if numpy_dtype == dtypes.string and not isinstance(values, np.ndarray):
383 proto_values = _FlattenToStrings(values)
--> 384 tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
385 return tensor_proto
386
/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_util.py in <listcomp>(.0)
382 if numpy_dtype == dtypes.string and not isinstance(values, np.ndarray):
383 proto_values = _FlattenToStrings(values)
--> 384 tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
385 return tensor_proto
386
/usr/local/lib/python3.4/dist-packages/tensorflow/python/util/compat.py in as_bytes(bytes_or_text)
41 return bytes_or_text
42 else:
---> 43 raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)
44
45
TypeError: not all arguments converted during string formatting
添加一個名稱參數沒有幫助。
作爲替代,tf.reduce_sum
與我一起工作,但tf.concat
有什麼問題?