gpt4 book ai didi

tensorflow - 如何在 Tensorflow.js 中改变张量的值?

转载 作者:行者123 更新时间:2023-12-03 23:54:59 25 4
gpt4 key购买 nike

如何在 Tensorflow.js 中改变张量的值?例如,如果我有一个这样创建的张量:
const a = tf.tensor1d([1,2,3,4])

如何更改张量的第三个元素的值?我知道张量是不可变的,而变量是可变的。

这样做:const a = tf.variable(tf.tensor1d([1,2,3,4]))似乎不能解决问题。我不能做:
const a = a[0].assign(5)
我可以在 中做到这一点python tensorflow 像这样:

a = tf.Variable([1,2,3,4])
a = a[0].assign(100)
with tf.Session() as sess:
sess.run(tf.global_variables_iniliazer())
print sess.run(a)

这输出 [100, 2,3,4]

最佳答案

是否 tf.buffer 为你工作?

// Create a buffer and set values at particular indices.
const a = tf.tensor1d([1, 2, 3, 4]);
const buffer = tf.buffer(a.shape, a.dtype, a.dataSync());
buffer.set(5, 0);
const b = buffer.toTensor();
// Convert the buffer back to a tensor.
b.print();
Tensor
[5, 2, 3, 4]

关于tensorflow - 如何在 Tensorflow.js 中改变张量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49802499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com