Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: Reverse flattening of a tensor

OS: Kubuntu 14.04, Tensorflow version = 0.12, Python version = 2.7

After my last layer the output of my neural net has the shape[batch, height, width, 2] which is [64, 32, 256, 2] in my case.

After that I use

output = tf.contrib.layers.flatten(input=output)

which produces a Tensor with the shape[64, 16384]

For evaluation purposes I would like to reverse this flattening in a different function to get the original [64, 32, 256, 2] Tensor back.

How would one do that?

like image 673
Peter111 Avatar asked Mar 29 '18 10:03

Peter111


People also ask

How do you flatten a tensor in TensorFlow?

To flatten the tensor, we're going to use the TensorFlow reshape operation. So tf. reshape, we pass in our tensor currently represented by tf_initial_tensor_constant, and then the shape that we're going to give it is a -1 inside of a Python list.

How do you reverse a tensor in Python?

flip(tensor_a) will reverse all tensor, and torch. flip(tensor_a, dims=(1,)) will reverse every row, like [1, 2, 3] --> [3, 2, 1] .

How do you flatten a tensor?

A tensor can be flattened into a one-dimensional tensor by reshaping it using the method torch. flatten(). This method supports both real and complex-valued input tensors. It takes a torch tensor as its input and returns a torch tensor flattened into one dimension.


1 Answers

Simply reshape to the shape you want: tf.reshape(output, [-1, 32, 256, 2]).

like image 76
GPhilo Avatar answered Oct 25 '22 21:10

GPhilo