【发布时间】:2017-10-26 05:06:10
【问题描述】:
我正在尝试在张量流中执行这个 numpy 代码。这样做的原因是因为我想以定制的方式(不使用 softmax)进行二进制预测,然后在我的网络的损失中使用它。 Output1 是网络输出的内容,一个大小为 (1, batch_size) 的数组。这是numpy代码:
predictions = []
for j in range(batch_size):
if output1[0, j] >= output2[0] and output1[0, j] <= output2[1]:
predictions.append(1)
else:
predictions.append(0)
在 Tensorflow 中,我尝试使用 tf.cond 做类似的事情,因为我想评估网络输出的值并基于此做一些事情:
predictions = []
for j in range(batch_size):
condResult = tf.cond(output1[0, j] >= output2[0], lambda: predictions.append(1), lambda: predictions.append(0))
condResultFalse = tf.cond(output1[0, j] <= output2[1], lambda: predictions.append(1), lambda: predictions(0))
但是,这有一些问题。首先,如果两个条件都为真,它会将 1 附加到列表中两次,这是我不想要的。其次,它会抛出一个错误,说ValueError: true_fn must have a return value. 显然,我必须返回一个张量,但我不确定如何执行此操作,因为我只想附加到一个列表中。
任何将其翻译成 Tensorflow 的帮助都会很棒!
谢谢
【问题讨论】:
标签: python numpy tensorflow deep-learning