简体   繁体   English

如何在Tensorflow中使用张量中的数据?

[英]How can I use the data in a tensor in Tensorflow?

我在Tensorflow中的代码

As you can see in the picture, I want to compute the total cost during one training, but the tf.equal returns a tensor type and the tf.equal(y1[i],y2[i]) can't be True . 如您在图片中看到的,我想在一次训练中计算总成本,但是tf.equal返回张量类型,而tf.equal(y1[i],y2[i])不能为True

How can I use the data in the tensor 我如何使用张量中的数据

You cannot do this in Tensorflow (pyTorch can do something like this). 您无法在Tensorflow中执行此操作(pyTorch可以执行以下操作)。 The reason is, that TF requires a static graph. 原因是TF需要静态图。 But you are trying to do dynamic evaluations. 但是您正在尝试进行动态评估。 Many people claim this static graph being a disadvantage of TF. 许多人声称此静态图是TF的缺点。 But in fact, it enables many cool features. 但实际上,它启用了许多很酷的功能。 But in you use case it is a little bit cumbersome to get a solution: 但是,在您使用的情况下,获得解决方案有点麻烦:

You need to write it like: 您需要这样写:

z = tf.zeros_like(y1)
label_a = z + 2
label_b = z + 20


case_001 = tf.where(tf.equal(y1, z), z + 2, z)
case_002 = tf.where(tf.equal(y2, z), z + 20, z)
switch_op = tf.where(tf.equal(y1, y2), ..., case_001 + case_002)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM