TensorFlow 2.6: 使用 tf.tensor_scatter_nd_update() 将数据填充到矩阵
你可以使用tf.fill()函数和tf.tensor_scatter_nd_update()函数来实现将R的值填充进矩阵B的每一行。具体的代码如下所示:
import tensorflow as tf
# 假设R的值为200个数据
R = tf.constant([1, 2, 3, ..., 200]) # 替换...为具体的200个数据
# 创建矩阵B,行数为R的长度,列数为1
B = tf.fill((R.shape[0], 1), 0) # 0为填充的初始值,可以根据需求自行修改
# 将R的值填充进矩阵B的每一行
B = tf.tensor_scatter_nd_update(B, indices=tf.expand_dims(tf.range(R.shape[0]), axis=1), updates=tf.expand_dims(R, axis=1))
# 打印结果
print(B)
这样,你就可以将R的值填充进矩阵B的每一行,得到一个形状为(200, 1)的矩阵B。
原文地址: https://www.cveoy.top/t/topic/pwam 著作权归作者所有。请勿转载和采集!