【问题标题】:How to Execute If Statements to Create a List Tensorflow?如何执行 If 语句以创建列表 TensorFlow?
【发布时间】: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


    【解决方案1】:

    一个好的解决方案是直接使用逻辑函数,例如 tf.less_equal 或 'True,它将是'1'。

    import tensorflow as tf
    import numpy as np
    
    output1 = tf.constant(np.random.randn(1, 200), dtype='float32')
    output2 = tf.constant([0.1, 0.5], dtype='float32')
    
    a = output2[0] <= output1[0]
    b = output1[0] <= output2[1]
    c = tf.cast(tf.logical_and(a, b), tf.int64)
    
    init  = tf.global_variables_initializer()
    
    sess = tf.Session()
    sess.run(init)
    res = sess.run(c)
    
    print res
    

    编辑:实际使用 int64

    【讨论】:

    • 这会在构建图表时进行评估吗?我不想这样做,因为 output2 和 output1 是网络的输出,所以我想看看它们的值是什么,然后根据它做一些事情。这就是为什么我认为我必须使用tf.cond。我尝试了您的解决方案,在发送数据之前c 始终为 0,因此我相信它正在评估此图表,而不是实际值。
    • 我知道 output1 和 output2 是网络的输出,我把它们作为常量这样你有一个例子。 c 这里实际上是一个向量,你只得到一个 0 ?这很奇怪。真的是 output1 a (1, batch_size) 和 output2 a (2) 大小的张量吗?实际上你没有提到 output2 的维度。
    • Output1 实际上是(2, batch_size),但我一次只关心一个,所以我用output1[0, j] 对其进行索引。 Output2 的大小为 [2]。我打印的是在任何数据传入之前,上面写着Tensor("Cast_1:0", shape=(), dtype=int32)。我还没有用数据运行它,很快就会这样做并更新。如果你能做到这一点,那么使用tf.cond 有什么意义呢?它们是一样的吗?
    • 我现在有另一个问题,并且已经发布了对原始问题的编辑。你能帮我解决这个问题吗?
    • 也许问题是我没有正确创建列表?我先predictions.append(0) 然后tf.convert_to_tensor(predictions)。用 TensorBoard 调试这个,我在图中没有看到 predictions,也许这就是它没有链接到结果并且无法计算梯度的原因?您知道如何正确执行此操作吗?
    【解决方案2】:

    这样做的方法接近答案,但这对我不起作用。

    predictions = []
    for j in range(batch_size):
            condition = tf.less_equal(output1[0, j], output2[1])
            condition2 = tf.greater_equal(output1[0, j], output2[0])
            resultingCondition = tf.where(condition, True, False)
            resultingCondition2 = tf.where(condition2, True, False)
            finalResultingCondition = tf.cast(tf.logical_not(tf.logical_and(resultingCondition, resultingCondition2)), tf.float32)
            predictions.append(finalResultingCondition)
    

    这将附加10 而不是1-1

    【讨论】:

    • 在你的 batch_size 上循环是相当耗时的,你应该使用广播。
    • 我给你的解决方案是直接给出“预测”作为张量。
    • 好的,我会尝试一下,然后告诉你。当我尝试您的原始解决方案时,它不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2016-03-03
    相关资源
    最近更新 更多