pythonimagetensorflow

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.

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.

After doing the all the above stuff I want to combine all three 2D matrices into the single 3D matrix.

For a tensorflow it would be a 3D tensor.

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]

How can I do that? any help is highly appreciated.


Solution

  • 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.

    Doing the same without the additional brackets around the r,g,b tensors will try to concat them to one large 2D matrix