tensorflow - 评估 tensorflow 中向量 y 的每个元素的条件

标签 tensorflow

我正在尝试评估向量 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/

相关文章:

tensorflow - 有没有办法用 Keras 获得可变的重要性?

python - 在 GPU 上执行外部优化器

python-3.x - 无法克隆对象 <tensorflow.python.keras.wrappers.scikit_learn.KerasClassifier 对象

python - Keras 训练在多次正确执行后崩溃

python - TensorFlow concat 中的排名不匹配错误

迭代器重置时的 tensorflow 数据集洗牌行为

python - 我怎么知道 tensorflow 是否使用 cuda 和 cudnn?

tensorflow - 在 Keras/TensorFlow 中训练一个具有可变大小输入的完全卷积神经网络需要花费不合理的长时间

python - AttributeError : module 'tensorflow' has no attribute 'compat' when loading tf. compat.v1.train.SessionRunHook

python - TensorFlow 权限被拒绝错误/位置