使用Layernorm时,出现问题:
RuntimeError: Given normalized_shape=[16], expected input with shape [*, 16], but got input of size[24, 16, 64, 64]
直接在使用时扩展所需的维度即可
nn.LayerNorm([16,64,64])
代码示例:
import torch
import torch.nn as nn
data= torch.Tensor(64, 80, 3,3)
out_w, out_h = 1, 1
out_layer1 = nn.Sequential(
nn.Conv2d(80, 32,kernel_size=3,groups=2),
nn.LayerNorm([32,out_w,out_h]),
nn.Dropout(0.1)
)
out = out_layer1(data)
print(out.shape)
data= torch.Tensor(64, 80, 5,5)
out_w, out_h = 3, 3
out_layer1 = nn.Sequential(
nn.Conv2d(80, 32,kernel_size=3,groups=2),
nn.LayerNorm([32,out_w,out_h]),
nn.Dropout(0.1)
)
out = out_layer1(data)
print(out.shape)