Python Tensorflow条件值错误
我试图将条件句与tensorflow结合使用,但我得到了一个错误:Python Tensorflow条件值错误,python,tensorflow,Python,Tensorflow,我试图将条件句与tensorflow结合使用,但我得到了一个错误: ValueError: Shapes (1,) and () are not compatible 下面是我使用的抛出错误的代码。 它是说错误在条件中 import tensorflow as tf import numpy as np X = tf.constant([1, 0]) Y = tf.constant([0, 1]) BOTH = tf.constant([1, 1]) WORKING = tf.constan
ValueError: Shapes (1,) and () are not compatible
下面是我使用的抛出错误的代码。
它是说错误在条件中
import tensorflow as tf
import numpy as np
X = tf.constant([1, 0])
Y = tf.constant([0, 1])
BOTH = tf.constant([1, 1])
WORKING = tf.constant(1)
def create_mult_func(tf, amount, list):
def f1():
return tf.scalar_mul(amount, list)
return f1
def create_no_op_func(tensor):
def f1():
return tensor
return f1
def stretch(tf, points, dim, amount):
"""points is a 2 by ??? tensor, dim is a 1 by 2 tensor, amount is tensor scalor"""
x_list, y_list = tf.split(0, 2, points)
x_stretch, y_stretch = tf.split(1, 2, dim)
is_stretch_X = tf.equal(x_stretch, WORKING, name="is_stretch_x")
is_stretch_Y = tf.equal(y_stretch, WORKING, name="is_stretch_Y")
x_list_stretched = tf.cond(is_stretch_X,
create_mult_func(tf, amount, x_list), create_no_op_func(x_list))
y_list_stretched = tf.cond(is_stretch_Y,
create_mult_func(tf, amount, y_list), create_no_op_func(y_list))
return tf.concat(1, [x_list_stretched, y_list_stretched])
example_points = np.array([[1, 1], [2, 2], [3, 3]], dtype=np.float32)
example_point_list = tf.placeholder(tf.float32)
result = stretch(tf, example_point_list, X, 1)
sess = tf.Session()
with tf.Session() as sess:
result = sess.run(result, feed_dict={example_point_list: example_points})
print(result)
堆栈跟踪:
File "/path/test2.py", line 36, in <module>
result = stretch(tf, example_point_list, X, 1)
File "/path/test2.py", line 28, in stretch
create_mult_func(tf, amount, x_list), create_no_op_func(x_list))
File "/path/tensorflow/python/ops/control_flow_ops.py", line 1142, in cond
p_2, p_1 = switch(pred, pred)
File "/path/tensorflow/python/ops/control_flow_ops.py", line 203, in switch
return gen_control_flow_ops._switch(data, pred, name=name)
File "/path/tensorflow/python/ops/gen_control_flow_ops.py", line 297, in _switch
return _op_def_lib.apply_op("Switch", data=data, pred=pred, name=name)
File "/path/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
op_def=op_def)
File "/path/tensorflow/python/framework/ops.py", line 2156, in create_op
set_shapes_for_outputs(ret)
File "/path/tensorflow/python/framework/ops.py", line 1612, in set_shapes_for_outputs
shapes = shape_func(op)
File "/path/tensorflow/python/ops/control_flow_ops.py", line 2032, in _SwitchShape
unused_pred_shape = op.inputs[1].get_shape().merge_with(tensor_shape.scalar())
File "/path/tensorflow/python/framework/tensor_shape.py", line 554, in merge_with
(self, other))
ValueError: Shapes (1,) and () are not compatible
文件“/path/test2.py”,第36行,在
结果=拉伸(tf,示例点列表,X,1)
文件“/path/test2.py”,第28行,拉伸
创建多功能(tf、金额、x列表),创建无功能(x列表))
文件“/path/tensorflow/python/ops/control_flow_ops.py”,第1142行,单位为cond
p_2,p_1=开关(pred,pred)
文件“/path/tensorflow/python/ops/control\u flow\u ops.py”,第203行,在交换机中
返回发电机控制流量操作开关(数据,pred,名称=名称)
文件“/path/tensorflow/python/ops/gen_control_flow_ops.py”,第297行,in_开关
返回_op_def_lib.apply_op(“开关”,data=data,pred=pred,name=name)
文件“/path/tensorflow/python/ops/op_def_library.py”,第655行,在apply_op中
op_def=op_def)
文件“/path/tensorflow/python/framework/ops.py”,第2156行,在create_op中
为输出设置形状(ret)
文件“/path/tensorflow/python/framework/ops.py”,第1612行,用于输出的集合形状
形状=形状函数(op)
文件“/path/tensorflow/python/ops/control\u flow\u ops.py”,第2032行,在SwitchShape中
未使用的\u pred\u shape=op.inputs[1]。获取\u shape()。将\u与(张量\u shape.scalar())合并
文件“/path/tensorflow/python/framework/tensor\u shape.py”,第554行,与合并
(自身、其他)
ValueError:形状(1、)和()不兼容
我尝试将工作更改为数组而不是标量
我认为问题在于
tf.equal
返回的是int32
,而不是它根据文档应该返回的bool问题在于tf.cond
的第一个参数。从文档中,关于tf.cond
的第一个参数的类型:
pred: A scalar determining whether to return the result of fn1 or fn2.
请注意,它必须是标量。您使用的是比较张量和张量的结果,这会给出一个(1,)
张量,而不是标量。可以使用运算符将其转换为标量,如下所示:
t = tf.equal(x_stretch, WORKING, name="is_stretch_x")
x_list_stretched = tf.cond(tf.reshape(t, []),
create_mult_func(tf, amount, x_list), create_no_op_func(x_list))
完成工作计划:
import tensorflow as tf
import numpy as np
X = tf.constant([1, 0])
Y = tf.constant([0, 1])
BOTH = tf.constant([1, 1])
WORKING = tf.constant(1)
def create_mult_func(tf, amount, list):
def f1():
return tf.scalar_mul(amount, list)
return f1
def create_no_op_func(tensor):
def f1():
return tensor
return f1
def stretch(tf, points, dim, amount):
"""points is a 2 by ??? tensor, dim is a 1 by 2 tensor, amount is tensor scalor"""
x_list, y_list = tf.split(0, 2, points)
x_stretch, y_stretch = tf.split(0, 2, dim)
is_stretch_X = tf.equal(x_stretch, WORKING, name="is_stretch_x")
is_stretch_Y = tf.equal(y_stretch, WORKING, name="is_stretch_Y")
x_list_stretched = tf.cond(tf.reshape(is_stretch_X, []),
create_mult_func(tf, amount, x_list), create_no_op_func(x_list))
y_list_stretched = tf.cond(tf.reshape(is_stretch_Y, []),
create_mult_func(tf, amount, y_list), create_no_op_func(y_list))
return tf.pack([x_list_stretched, y_list_stretched])
example_points = np.array([[1, 1], [2, 2]], dtype=np.float32)
example_point_list = tf.placeholder(tf.float32)
result = stretch(tf, example_point_list, X, 1)
sess = tf.Session()
with tf.Session() as sess:
result = sess.run(result, feed_dict={example_point_list: example_points})
print(result)
否错误不在条件中,而是在您尝试比较的张量形状中:
x\u拉伸
与工作
的形状不同。似乎x_拉伸比工作
大一个维度。点的内容是什么?如果你提供一个可运行的代码,我可能会提供更多帮助。我制作了一个完整的可运行示例,你可以粘贴进去,看看有什么失败。这不起作用。抛出错误:“使用tf.Tensor
作为Pythonbool
是不允许的。我还制作了一个完整的可运行代码示例。他们的示例代码使用一个张量作为第一个参数作为条件:不是真的。对tf.pack
的输入是一个列表。请尝试返回tf.pack([x\u list\u strated,y\u list\u strated]))
另外,您的示例_points
是一个3x2
张量,您试图在维度0上将其拆分为两个方向。这将失败。请尝试示例_points=np.array([[1,1],[2,2]],dtype=np.float32)
。问题仍然存在于条件中,当我使用重塑时,它说不允许使用python bool。它甚至没有得到结果(即使你是正确的,这也是错误的),我现在在dim 1上拆分,并用完整的工作程序更新了我的答案。