在Pytorch中将三维张量转换为4D张量
我很难在PyTorch中找到有关整形的信息。Tensorflow很容易 我的张量有形状。大小[3480480]。 我想把它转换成一个4D张量,形状为[1,3480480]。 我该怎么做?您可以使用 例如:在Pytorch中将三维张量转换为4D张量,pytorch,Pytorch,我很难在PyTorch中找到有关整形的信息。Tensorflow很容易 我的张量有形状。大小[3480480]。 我想把它转换成一个4D张量,形状为[1,3480480]。 我该怎么做?您可以使用 例如: x = torch.zeros((4,4,4)) # Create 3D tensor x = x.unsqueeze(0) # Add dimension as the first axis (1,4,4,4) x = torch.zeros((4,4,4)) #
x = torch.zeros((4,4,4)) # Create 3D tensor
x = x.unsqueeze(0) # Add dimension as the first axis (1,4,4,4)
x = torch.zeros((4,4,4)) # Create 3D tensor
print(x[None].shape) # (1,4,4,4)
print(x[:,None,:,:].shape) # (4,1,4,4)
print(x[:,:,None,:].shape) # (4,4,1,4)
print(x[:,:,:,None].shape) # (4,4,4,1)
我见过一些人使用无索引来添加单一维度。例如:
x = torch.zeros((4,4,4)) # Create 3D tensor
x = x.unsqueeze(0) # Add dimension as the first axis (1,4,4,4)
x = torch.zeros((4,4,4)) # Create 3D tensor
print(x[None].shape) # (1,4,4,4)
print(x[:,None,:,:].shape) # (4,1,4,4)
print(x[:,:,None,:].shape) # (4,4,1,4)
print(x[:,:,:,None].shape) # (4,4,4,1)
就我个人而言,我更喜欢unsqueze,但熟悉这两种语言是很好的。API名称是给定的。