简体   繁体   English

如何将我的三个2D张量组合成张量流中的单个3D张量?

[英]How can I combine my three 2D tensors into a single 3D tensor in tensor flow?

Hello I am a newbie with the tensorflow and currently, I am working with colour Images and it's PCAS. 您好,我是tensorflow的新手,目前,我正在使用彩色图像及其PCAS。

I have extracted PCAS in a form of "Red","Green" and "Blue" and also computed the weights which are associated with "Red","Green" and "Blue" components. 我已经以“红色”,“绿色”和“蓝色”的形式提取了PCAS,还计算了与“红色”,“绿色”和“蓝色”组件相关的权重。

After doing the all the above stuff I want to combine all three 2D matrices into the single 3D matrix. 完成上述所有操作后,我想将所有三个2D矩阵合并为一个3D矩阵。

For a tensorflow it would be a 3D tensor. 对于张量流,它将是3D张量。

def multi(h0,ppca,mu,i,scope=None):

with tf.variable_scope(scope or"multi"):
        return tf.matmul(ppca[:,:,0],h0[i,:,:,0]) + tf.reshape(mu[:,0],[4096,1]) , tf.matmul(ppca[:,:,1],h0[i,:,:,1]) + tf.reshape(mu[:,1],[4096,1]) ,tf.matmul(ppca[:,:,2],h0[i,:,:,2]) + tf.reshape(mu[:,2],[4096,1]) 

So from the above function, I will get all three different 2D tensors and want to combine those 2D tensors to single 3D tensor which has dimensions [4096,1,3] 因此,从上述功能中,我将获得所有三个不同的2D张量,并希望将这些2D张量组合为尺寸为[4096,1,3]的单个3D张量。

How can I do that? 我怎样才能做到这一点? any help is highly appreciated. 非常感谢您的帮助。

You need to concat them like this: 您需要像这样连接它们:

three_d_image = tf.concat(0, [[r], [g], [b]])

This tells tensorflow to concat them along the x dimension and treat each tensor as a matrix. 这告诉张量流沿x维度合并它们,并将每个张量视为矩阵。

Doing the same without the additional brackets around the r,g,b tensors will try to concat them to one large 2D matrix 在不使用r,g,b张量周围的额外括号的情况下进行相同操作将尝试将它们连接到一个大型2D矩阵

A clean, easy way to do it is using the tf.stack operation (tf.pack in older versions of tensorflow), it concatenats all tensors along a new dimension. 一种干净,简单的方法是使用tf.stack操作(在旧版本的tensorflow中为tf.pack),它将所有张量沿着一个新维度连接在一起。 If you want your new dimension to be after all previous, you need to set the axis argument to the number of dimensions of your tensors. 如果要使新尺寸在所有尺寸之后,则需要将axis参数设置为张量的尺寸数。

    three_d_image = tf.stack([r,g,b], axis=2)

one of the solutions is that you can add one more empty dimension to your 2Ds so you will have 3 matrices of 3D dimension [4096,1,1] then you can concat these 3 matrices by axis 2 tf.concat(2,matrices) gives you [4096,1,3] 解决方案之一是您可以向2D添加一个空维度,因此您将拥有3个3D维度的矩阵[4096,1,1],然后可以按轴2 tf.concat(2,matrices)这3个矩阵。给你[4096,1,3]

the second solution can be concat of axis 1, tf.concat(1,matrices) then reshape it to 3D 第二个解决方案可以是轴1的concat, tf.concat(1,matrices)然后将其重塑为3D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM