簡體   English   中英

如何在Tensorflow中擴展維度

[英]How to expand dimensions in Tensorflow

我有這個張量 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)>

我想創建一個掩碼以添加到另一個形狀為(2, 18, 1000)的張量。 也就是說,我想將每個數字擴展到 1000 個,例如,在張量 A 中,將每個 0 更改為 1000 個零的維度,並在每個 1 中,將每個更改為 1000 個 1 的維度。 我該怎么做?

編輯

基本上,我想要做的是將張量 A 從形狀(2, 18)轉換為形狀(2, 18, 1000) ,這 1000 個值為 0 或 1

只需使用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 教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM