如何在arrayTensorflow

时间:2018-12-03 10:15:53

标签: tensorflow

我是TensorFlow的初学者。我想使用数组变量中给出的真值索引来初始化布尔矩阵。

初始矩阵(必须为张量): [[False, False, False], [False, False, False], [False, False, False]]

给定的索引数组(必须为张量):

[1,3,5,6,8]

结果:

[[False, True, False],
[True, False, True],
[True, False, True]]

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

import tensorflow as tf

f=tf.Variable([False,False,False,False,False,False,False,False,False])
t=tf.constant([True,True,True,True,True])

indice=tf.constant([1,3,5,6,8], dtype=tf.int32)

res_1D=tf.scatter_update(f, indice, t)

result_mat = tf.reshape(res_1D, shape=(3,3))

init=tf.initialize_all_variables()

with tf.Session() as sess:

    sess.run(init)

    result=sess.run(result_mat)

print(result)