【问题标题】:tf.where() doesn't produce result same as np.where()tf.where() 不会产生与 np.where() 相同的结果
【发布时间】:2017-10-25 03:34:10
【问题描述】:

我正在使用相同的逻辑代码将 tensorflow 与 numpy 进行比较。

在实现 tf.where 时,我无法得到与 np.where 相同的结果
下面的代码或用法有什么问题?

数据:

X_batch = np.concatenate([np.arange(10).reshape(1, -1) for i in range(10)], axis=0)

tensorflow tf.where 玩具代码:

X = tf.placeholder(dtype=tf.int32, shape=[10, 10])

with tf.Session() as sess:
    print(sess.run(tf.where(X > 5, tf.zeros([10, 10], dtype=tf.int32), 
                            X), feed_dict={X: X_batch}))

numpy np.where 玩具代码:

np.where(X_batch > 5, np.zeros([10,10]), X_batch)

代码有一些拼写错误。修正完成

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    我编辑了代码。 tf.where() 的输入应与 np.where() 相同。因此,对于 tf.where() 的参数,您应该将零 10*10 矩阵和 x_batch 作为输入作为您的 np.where() 方法参数。

    import tensorflow as tf
    import numpy as np
    
    
    X_batch = np.concatenate([np.arange(10).reshape(1, -1) for i in range(10)], axis=0)
    
    #print(X_batch )
    
    X = tf.placeholder(dtype=tf.int32, shape=[10, 10])
    
    with tf.Session() as sess:
        print(sess.run(tf.where(X > 5, tf.fill([10, 10], 0),
                                X), feed_dict={X: X_batch}))
    

    希望这会有所帮助。

    【讨论】:

    • 我的意思是当使用相同的tensorflow和numpy功能代码时,结果不同,所以我问了一个问题。但现在我做同样的事情,它的工作原理是一样的......抱歉打扰你
    猜你喜欢
    • 2010-09-11
    • 1970-01-01
    • 2020-02-09
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多