当前位置:网站首页>【Pytorch】nn.Linear,nn.Conv

【Pytorch】nn.Linear,nn.Conv

2022-08-11 06:28:00 二进制人工智能

nn.Linear

nn.Conv1d

nn.Conv1dkernel_size=1时,效果与nn.Linear相同,不过输入数据格式不同:
https://blog.csdn.net/l1076604169/article/details/107170146

import torch


def count_parameters(model):
    """Count the number of parameters in a model."""
    return sum([p.numel() for p in model.parameters()])


conv = torch.nn.Conv1d(3, 32, kernel_size=1)
print(count_parameters(conv))
# 128

linear = torch.nn.Linear(3, 32)
print(count_parameters(linear))
# 128

print(conv.weight.shape)
# torch.Size([32, 3, 1])
print(linear.weight.shape)
# torch.Size([32, 3])

# use same initialization
linear.weight = torch.nn.Parameter(conv.weight.squeeze(2))
linear.bias = torch.nn.Parameter(conv.bias)

tensor = torch.randn(128, 256, 3)   # [batch, feature_num,feature_size]
permuted_tensor = tensor.permute(0, 2, 1).clone().contiguous()  # [batch, feature_size,feature_num]

out_linear = linear(tensor)
print(out_linear.mean())
# tensor(0.0344, grad_fn=<MeanBackward0>)
print(out_linear.shape)
# torch.Size([128, 256, 32])


out_conv = conv(permuted_tensor)
print(out_conv.mean())
# tensor(0.0344, grad_fn=<MeanBackward0>)
print(out_conv.shape)
# torch.Size([128, 32, 256])


nn.Conv2d

nn.Conv3d

原网站

版权声明
本文为[二进制人工智能]所创,转载请带上原文链接,感谢
https://binaryai.blog.csdn.net/article/details/126254680