簡體   English   中英

如何在張量流中操縱多維張量?

[英]How to manipulate multidimensional tensor in tensorflow?

我有一個形狀的張量[?,128,128,128,5] 。這代表一個有5個可能類的3D圖像。

我想添加子張量[?,:,:,:,2][?,:,:,:,3]的內部[?,:,:,:,4]這在目前是全零。

然后我想將這些先前的子張量[?,:,:,:,2][?,:,:,:,3]設置為零。 我怎么能這樣做?

謝謝你的幫助 !

如果我理解正確,我想你想要這樣的東西:

import tensorflow as tf

img = tf.placeholder(tf.float32, [None, 128, 128, 128, 5])
s = tf.shape(img)
img2 = tf.concat([img[..., :2],
                  tf.zeros([s[0], s[1], s[2], s[3], 2], img.dtype),
                  tf.reduce_sum(img[..., 2:], axis=-1, keepdims=True)], axis=-1)

編輯:根據評論,如果您想要保持最后一個軸的第一個和最后一個索引不變,將第二個和第三個索引聚合到第四個索引並用零替換第二個和第三個索引,那么你會做一些事情像這樣:

import tensorflow as tf

img = tf.placeholder(tf.float32, [None, 128, 128, 128, 5])
z = tf.expand_dims(tf.zeros(tf.shape(img)[:-1], img.dtype), axis=-1)
img2 = tf.concat([img[..., :1],  # New 1st index is the same as before
                  z,  # New 2nd index is zeros
                  z,  # New 3rd index is zeros
                  # New 4th index is sum of 2nd, 3rd and 4th indices
                  tf.reduce_sum(img[..., 1:4], axis=-1, keepdims=True)],
                  # New last index is the same as before
                  img[..., -1:]], axis=-1)

暫無
暫無

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

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