xSooDx
xSooDx

Reputation: 493

Resize tensor and interpolate with zeros

I want to resize a 2x2 tensor to a 4x4 tensor, but have the new values as zeros.

[[1,2],
 [3,4]]

becomes

[[1,0,2,0],
 [0,0,0,0],
 [3,0,4,0],
 [0,0,0,0]]

I could not find a proper way to do this.

Upvotes: 0

Views: 162

Answers (1)

GPhilo
GPhilo

Reputation: 19163

Define a sparse tensor with the values you have and convert it back to dense:

a = tf.constant([[1,2],[3,4]]) # your input tensor

indices = tf.constant( [[0,0],[0,2],[2,0],[2,2]], dtype=tf.int64 ) # define this as appropriate
values = tf.reshape(a, [-1]) # flatten the input
sparse_tensor = tf.SparseTensor(indices, values, [4,4])
res = tf.sparse_tensor_to_dense(sparse_tensor)

with tf.Session() as sess:
    print(sess.run(res))

prints

array([[1, 0, 2, 0],
       [0, 0, 0, 0],
       [3, 0, 4, 0],
       [0, 0, 0, 0]], dtype=int64)

Upvotes: 1

Related Questions