np.newaxis,tensor.squeeze()的作用比较相似,增加维度。
np.hstack,torch.hstack,torch.cat([tensor,tensor],dim=1) 比较相似,连接tensor。
import torch
import numpy as np
if __name__ == '__main__':
print(torch.__version__)
# labels = torch.tensor([0, 1, 2, 0, 1, 2, 0, 1, 2])
a = np.array([1,2,3,4])
a_new = np.array([5,6,7,8])[:,np.newaxis]
b = a[:,np.newaxis]
print(b)
b_new = np.hstack([b,a_new])
print(b_new)
c = torch.tensor([1,2,3,4])
c_new = torch.tensor([5,6,7,8]).unsqueeze(1)
d = c.unsqueeze(1)
print(d)
d_new = torch.hstack([d,c_new])
print(d_new)
e = torch.cat([d, c_new],dim=1)
print(e)
f = e.reshape([2,4])
print(e,f)
[[1]
[2]
[3]
[4]]
[[1 5]
[2 6]
[3 7]
[4 8]]
tensor([[1],
[2],
[3],
[4]])
tensor([[1, 5],
[2, 6],
[3, 7],
[4, 8]])
tensor([[1, 5],
[2, 6],
[3, 7],
[4, 8]])
tensor([[1, 5],
[2, 6],
[3, 7],
[4, 8]]) tensor([[1, 5, 2, 6],
[3, 7, 4, 8]])