考慮下面的代碼:
import tensorflow as tf
test=tf.constant([[[100., 2., -30.],[-4,5,6]], [[4., 5., 6.],[-7,8,9]]]) # matrix
print(test)
test1=tf.constant([[[100.]],[[ 8.]]])
print(test1)
將 test1 添加到測驗的前兩列時,我們將得到以下輸出:
print(test[:,:,0:2] test1)
我不想將 test1 變數添加到測驗變數的最后一列,但同時我想在輸出中包含測驗變數的最后一列不變:
[[[200. 102., -30.]
[ 96. 105., 6.]]
[[ 12. 13., 6]
[ 1. 16., 9]]]
我將如何快速編碼?
uj5u.com熱心網友回復:
最簡單的選擇是使用tf.concat
:
import tensorflow as tf
test = tf.constant([[[100., 2., -30.],[-4,5,6]], [[4., 5., 6.],[-7,8,9]]]) # matrix
test1 = tf.constant([[[100.]],[[ 8.]]])
print(tf.concat([test[:,:,0:2] test1, test[:,:,2:]], axis=-1))
tf.Tensor(
[[[200. 102. -30.]
[ 96. 105. 6.]]
[[ 12. 13. 6.]
[ 1. 16. 9.]]], shape=(2, 2, 3), dtype=float32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370280.html
上一篇:替換超出一定范圍的張量元素