我试图在偏差+权重上实现L1规范.为此,我尝试将它们连接在一起并采取一种方法.
也就是说,我有偏见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(.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
什么?
这个问题似乎是由这条tf.concat()
实现线上的一个错误造成的.目前 - 与大多数其他可变参数TensorFlow运算符不同 - tf.concat()
只接受张量列表,并且不能正确处理元组.如果您将代码更改为以下代码,它应该工作:
l1_penalty = tf.reduce_mean(tf.abs(tf.concat(1, [self.W1, self.b1])))
在此期间,我将修复此错误并将其提交到上游,以便在下一版本中处理.