Raphael Petegrosso
Raphael Petegrosso

Reputation: 3870

TensorFlow - Tensor Reshape

I'm using TensorFlow and I have the following matrix:

U2 = tf.constant([[1,3],[0,1],[1,0]],dtype=tf.float32)

[[ 1.  3.]
 [ 0.  1.]
 [ 1.  0.]]

What's the easiest way to reshape this matrix so that it results in the tensor:

tf.constant([[[1,1],[0,0],[1,1]],[[3,3],[1,1],[0,0]]],dtype=tf.float32)

[[[ 1.  1.]
  [ 0.  0.]
  [ 1.  1.]]

 [[ 3.  3.]
  [ 1.  1.]
  [ 0.  0.]]]

Upvotes: 1

Views: 274

Answers (1)

David Wong
David Wong

Reputation: 748

This is one quick way to create the first submatrix using TensorFlow API:

U2 = tf.constant([[1,3],[0,1],[1,0]],dtype=tf.float32)
first_col = tf.unpack(U2, axis=1)[0]
repeated_cols = tf.tile(first_col, [2])
repeated_cols = tf.reshape(repeated_cols, [3,2])

Which would create

[[ 1.  1.]
  [ 0.  0.]
  [ 1.  1.]]

Upvotes: 1

Related Questions