Pra*_*yal 6 tensorflow mutate tensor tensorflow.js
如何在 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();
Run Code Online (Sandbox Code Playgroud)
Tensor
[5, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我必须使用mulStrictand addStrictwhich 进行元素乘法和加法来完成此操作。
const a = tf.tensor1d([1,2,3,4]);
tf.mulStrict(a, tf.tensor1d([0,1,1,1]))
.addStrict(tf.tensor1d([100, 0, 0, 0]);
Run Code Online (Sandbox Code Playgroud)
这是基于 这里的答案
| 归档时间: |
|
| 查看次数: |
1539 次 |
| 最近记录: |