gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:09:07 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com