我正在尝试评估向量 y 的每个元素的条件,以便我得到一个向量,其 i'th
元素告诉我 y[i]
是否满足条件。有没有办法不使用循环来做到这一点?到目前为止,我已经尝试了以下方法:
dim = 3
x = tf.placeholder(tf.float32, shape = [dim])
y = tf.log(x)
tf1 = tf.constant(1)
tf0 = tf.constant(0)
x_0 = tf.tile([x[0]], [dim])
delta = tf.cond(tf.equal(y,x_0), tf1, tf0))
sess = tf.Session()
a = np.ones((1,3))
print(sess.run(delta, feed_dict={x:a}))
对于给定的输入 x
,如果 y[i] = x[,我希望
和 delta[i]
为 1
0]0
否则。
我得到错误
shape must be of equal rank but are 0 and 1 for 'Select_2' (op: 'select') with input shapes [3], [],[]
我是 TensorFlow 的新手,如有任何帮助,我们将不胜感激!
最佳答案
似乎您有错误,因为您正在尝试比较具有不同形状的张量。
这是工作代码:
import tensorflow as tf
import numpy as np
dim = 3
x = tf.placeholder(tf.float32, shape=(1, dim), name='ktf')
y = tf.log(x)
delta = tf.cast(tf.equal(y, x[0]), dtype=tf.int32)
sess = tf.Session()
a = np.ones((1, 3))
print(sess.run(delta, feed_dict={x: a}))
关于tensorflow - 评估 tensorflow 中向量 y 的每个元素的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47735825/