torch.nn.Linear 笔记

举报
风吹稻花香 发表于 2021/06/04 22:43:17 2021/06/04
【摘要】 最多支持两维, 我准备用这个代替1*1的卷积核 import torch x = torch.randn(128, 20) # 输入的维度是(128,20)m = torch.nn.Linear(20, 50) # 20,30是指维度output = m(x) print('m.weight.shape: ', m.weight.shape)print('m.bia...

最多支持两维,

我准备用这个代替1*1的卷积核


  
  1. import torch
  2. x = torch.randn(128, 20) # 输入的维度是(128,20)
  3. m = torch.nn.Linear(20, 50) # 20,30是指维度
  4. output = m(x)
  5. print('m.weight.shape: ', m.weight.shape)
  6. print('m.bias.shape:', m.bias.shape)
  7. print('output.shape:', output.shape)
  8. # ans = torch.mm(input,torch.t(m.weight))+m.bias 等价于下面的
  9. ans = torch.mm(x, m.weight.t()) + m.bias
  10. print('ans.shape:', ans.shape)
  11. print(torch.equal(ans, output))

 


  
  1. class ClassHead(nn.Module):
  2. def __init__(self, inchannels=512, num_classes=2):
  3. super(ClassHead, self).__init__()
  4. self.num_classes = num_classes
  5. self.conv1x1 = nn.Conv2d(inchannels, self.num_classes, kernel_size=(1, 1), stride=1, padding=0)
  6. def forward(self, x):
  7. out = self.conv1x1(x)
  8. out = out.permute(0, 2, 3, 1).contiguous()
  9. return out.view(out.shape[0], -1, self.num_classes)

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/104598453

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。