I have a 3d numpy array having shape of (2, 128, 128)
and I want to add zeros to it so that it becomes (2, 162, 162)
.
I have this below code for padding 2d array
input = tf.constant([[1, 2], [3, 4]])
padding = tf.constant([[2, 2], [2, 2]])
# Printing the Input
print("Input: ", input)
print("Padding: ", padding)
# Generating padded Tensor
res = tf.pad(input, padding, mode ='CONSTANT')
But I want to know padding 3d array by adding zeros.
CodePudding user response:
You need to add one more rank to the padding tensor.
import tensorflow as tf
inp_tns = tf.random.uniform((2, 128, 128))
pad_tns = tf.constant([ [0, 0] , [17, 17], [17, 17] ])
# -----------padding: ^first_dim^
# ------------------------padding: ^second_dim^
# ---------------------------------------padding: ^third_dim^
# Printing the Input
print("Input: ", inp_tns)
print("Padding: ", pad_tns)
# Generating padded Tensor
res = tf.pad(inp_tns, pad_tns, mode ='CONSTANT', constant_values=0)
print(res.shape)
(2, 162, 162)
NB. Reference : tf.pad
CodePudding user response:
https://stackoverflow.com/a/48690064/16975978
I made like in shared link.
z = np.zeros((2, 5, 5))
o = np.ones((2, 3, 3))
z[:, :3, :3] = o
print(z)
Check the result