我有這個張量 A:
<tf.Tensor: shape=(2, 18), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1.,
1., 1.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0.]], dtype=float32)>
我想創建一個蒙版以添加到另一個帶有 shape 的張量(2, 18, 1000)。也就是說,我想將每個數字擴展為 1000 個,例如,在張量 A 中,將每個 0 更改為 1000 個零的維度,并在每個 1 中將每個數字更改為 1000 個 1 的維度。我怎么能做到?
編輯
基本上,我想要做的是將張量 A 從一個形狀轉換為另一個(2, 18)形狀(2, 18, 1000),這 1000 個值是 0 或 1
uj5u.com熱心網友回復:
只需使用tf.expand_dims
import tensorflow as tf
a = tf.constant([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1.,
1., 1.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0.]])
b = tf.random.normal(shape=[2, 18, 1000])
c = tf.expand_dims(a, axis=2) b
c.shape
# TensorShape([2, 18, 1000])
關于廣播參見,例如, 這個 numpy 教程
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/426823.html
