简体   繁体   English

TensorFlow逻辑运算((A == B)&&(C == D))导致“形状不兼容:[2]与[3]”

[英]TensorFlow logical operation ((A == B) && (C == D)) results in “Incompatible shapes: [2] vs. [3]”

I'm trying to build the following logical expression - 我正在尝试构建以下逻辑表达式-

tf.logical_and(tf.equal(tf.argmax(y_conv,0), tf.argmax(y_,0)), 
                tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)), name=None)

But it results in the below error - 但这会导致以下错误-

Incompatible shapes: [2] vs. [3] 不兼容的形状:[2]与[3]

tf.equal(tf.argmax(y_conv,0), tf.argmax(y_,0)) and tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) work fine separately, the error occurs only with tf.logical_and . tf.equal(tf.argmax(y_conv,0), tf.argmax(y_,0))tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))工作正常,仅tf.logical_and会发生错误。 tf.logical_and expects boolean tensors and tf.equal returns boolean tensors, so all arguments are in order so not sure why it fails. tf.logical_and期望布尔张量,而tf.equal返回布尔张量,因此所有参数都顺序正确,因此不确定为什么失败。

To give some context, the original code is below and I'm just trying to update correct_prediction to include both 0 and 1 for tf.argmax 给予一定的情况下,原来的代码如下,我只是想更新correct_prediction包括两个0和1 tf.argmax

UPDATE1 Start (this adds all of the variable declarations) UPDATE1开始(这将添加所有变量声明)

sess = tf.InteractiveSession()
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)
def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)
def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
W_conv1 = weight_variable([3, 3, 1, 32])
b_conv1 = bias_variable([32])
x = tf.placeholder(tf.float32, shape=[None, 9])
x_image = tf.reshape(x, [-1,3,3,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
h_pool2_flat = tf.reshape(h_conv1, [-1, 3 * 3 * 32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
W_fc2 = weight_variable([64, 2])
b_fc2 = bias_variable([2])
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

UPDATE1 End (this adds all of the variable declarations) UPDATE1 End(这将添加所有变量声明)

This is where the problem is located - 这是问题所在的位置-

y_ = tf.placeholder(tf.float32, shape=[None, 2])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
#This works - correct_prediction = tf.equal(tf.argmax(y_conv,0), tf.argmax(y_,0)) . Changed it to -
correct_prediction = tf.logical_and(tf.equal(tf.argmax(y_conv,0), tf.argmax(y_,0)), 
                tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)), 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())
train_step.run(feed_dict={x: xtrain, y_: ytrain, keep_prob: 0.5})
#In debugging mode, code breaks at the below line
print("test accuracy %g"%accuracy.eval(feed_dict={x: xtest, y_: ytest, keep_prob: 1.0}))

How can I debug this error? 如何调试此错误?

The problem arises because tf.equal() is an elementwise operation and it returns a tensor with the same shape as its arguments. 出现问题是因为tf.equal()元素操作,并且它返回张量的形状与其参数相同。 The easiest way to fix your expression is to use tf.reduce_all() to aggregate the results of tf.equal() down to a scalar before computing the tf.logical_and() , as follows: 修复你表达的最简单方法是使用tf.reduce_all()聚合的结果tf.equal()计算之前下降到标tf.logical_and()如下所示:

tf.logical_and(
    tf.reduce_all(tf.equal(tf.argmax(y_conv, 0), tf.argmax(y_, 0))), 
    tf.reduce_all(tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TensorFlow 不兼容的形状:CNN 的 [870] 与 [2] - TensorFlow Incompatible shapes: [870] vs. [2] for CNN tensorflow 多标签分类 - 不兼容的形状:[7,5] 与 [7] - tensorflow multilabel classification - Incompatible shapes: [7,5] vs. [7] Tensorflow:不兼容的形状:[1,2] 与 [1,4,4,2048] - Tensorflow: Incompatible shapes: [1,2] vs. [1,4,4,2048] InvalidArgumentError:不兼容的形状:[3] 与 [4] - InvalidArgumentError: Incompatible shapes: [3] vs. [4] TensorFlow Model 训练:InvalidArgumentError:不兼容的形状:[8,10] vs. [32,1] - TensorFlow Model Training: InvalidArgumentError: Incompatible shapes: [8,10] vs. [32,1] Tensorflow InvalidArgumentError(参见上面的回溯):不兼容的形状:[10000,10]与[10000] - Tensorflow InvalidArgumentError (see above for traceback): Incompatible shapes: [10000,10] vs. [10000] tensorflow.python.framework.errors_impl.InvalidArgumentError:不兼容的形状:[10]与[10000] - tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [10] vs. [10000] Tensorflow 2.2.0 InvalidArgumentError:不兼容的形状:[98,2] vs. [32,2] - Tensorflow 2.2.0 InvalidArgumentError: Incompatible shapes: [98,2] vs. [32,2] InvalidArgumentError:不兼容的形状:[400]与[50] - InvalidArgumentError: Incompatible shapes: [400] vs. [50] TensorFlow:不兼容的形状:[100,155]与[128,155]结合使用CNN和LSTM - TensorFlow: Incompatible shapes: [100,155] vs. [128,155] when combining CNN and LSTM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM