I have 3 tensors with shape (100,43,1024), (100,37,1024) and (100,42,1024). I want to make the second dimension of all these tensors to the maximum value of 2nd dimension i.e 43 in this case. Could you plz help me how to use pad function to make them of equal shape?
Asked
Active
Viewed 337 times
1
-
do u want to operate this padding inside a model? – Marco Cerliani Aug 14 '20 at 14:34
-
No I Have function that returns these tensors and I have to concatenate them but before that I have to make them of same dimension. – Aizayousaf Aug 14 '20 at 14:40
1 Answers
2
if you are operating with numpy array, you can zero pad them in this way:
# create your data
n_sample = 5
X = [np.random.uniform(0,1, (n_sample,43,1024)),
np.random.uniform(0,1, (n_sample,37,1024)),
np.random.uniform(0,1, (n_sample,42,1024))]
# find max dim
max_dim = np.max([x.shape[1] for x in X])
print(max_dim)
X_pad = []
for x in X:
X_pad.append(np.pad(x, ((0,0),(max_dim-x.shape[1],0),(0,0)), mode='constant')) # pre padding
# X_pad.append(np.pad(x, ((0,0),(0,max_dim-x.shape[1],(0,0)), mode='constant')) # post padding
# check padded shape
print([x.shape for x in X_pad])

Marco Cerliani
- 21,233
- 3
- 49
- 54