python - 如何在 tensorflow 中的张量中执行项目分配?

标签 python tensorflow neural-network

设一个张量为a = [0,0,0,0,0,0,0,0],另一个张量为b = [1,3,0,5 ],这里我想要一个 tensorflow 操作,将 1 放入张量 a 中,并从张量 b 中获取位置值。因此,输出张量将为[1,1,0,1,0,1,0,0]。 如何解决这个问题呢?

最佳答案

这个怎么样?

a = tf.Variable([0,0,0,0,0,0,0,0])
b = tf.Variable([1,3,0,5])

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    update = tf.scatter_update(a,
                               b,
                               tf.tile(tf.constant([1],
                                       tf.int32),
                               b.shape))

    print(update.eval(session=sess))

输出为

[1 1 0 1 0 1 0 0]

关于python - 如何在 tensorflow 中的张量中执行项目分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52144159/

相关文章:

python - pandas 创建数据框 - 解析 key

python - 允许用户下载在 AWS 上生成的 zip 文件

python - 在两个子图之间共享 Tensorflow 中的权重

implementation - 实现 Barabasi-Albert 方法来创建无标度网络

python - Keras:ModelCheckpoint 保存到变量而不是文件?

machine-learning - 使用 Keras 进行视频预测(时间序列)

python - Spacy 实体链接模型的训练时间

python - numpy.sum 的内部结构

python - UnboundLocalError : local variable 'batch_index' referenced before assignment

python - coursera作业python文件编译错误