0
例如上部分的神经元采用激活函数从一层,有一个张量如何Tensorflow
a=[[1,2,3,4,5],
[2,3,4,5,6]]
indices =[[1, 0, 1, 0, 0],
[0, 1, 0, 0, 0]]
我只想用活化上的元素(从),其索引是具有值1 (来自b)。例如,我只想在索引[0,0],[0,2],[1,1]的元素上使用激活函数。
谢谢!
例如上部分的神经元采用激活函数从一层,有一个张量如何Tensorflow
a=[[1,2,3,4,5],
[2,3,4,5,6]]
indices =[[1, 0, 1, 0, 0],
[0, 1, 0, 0, 0]]
我只想用活化上的元素(从),其索引是具有值1 (来自b)。例如,我只想在索引[0,0],[0,2],[1,1]的元素上使用激活函数。
谢谢!
您可以使用tf.where:
tf.where(tf.cast(indices, dtype=tf.bool), tf.nn.sigmoid(a), a)
对于示例:
import tensorflow as tf
a = tf.constant([[1,2,3,4,5], [2,3,4,5,6]], dtype=tf.float32)
indices = tf.constant([[1, 0, 1, 0, 0], [0, 1, 0, 0, 0]],
dtype = tf.int32)
result = tf.where(tf.cast(indices, dtype=tf.bool), tf.nn.sigmoid(a), a)
with tf.Session() as sess:
print(sess.run(result))
此打印:
[[ 0.7310586 2. 0.95257413 4. 5. ]
[ 2. 0.95257413 4. 5. 6 ]]
完美解决我的问题的好方法,非常感谢。 –
是否有错字在'指数为[0,0], [0,1],[1,1]'?我认为你需要'索引[0,0],[0,2],[1,1]',对吗? – Akhilesh
你是对的,我修改了这个错字。谢谢。你有回答我的问题吗?谢谢! –
非常感谢你,我已经尝试过这些功能,他们实际上工作。但是很难使用。我认为下面的答案会更有效率。再次感谢你。 –