我是靠谱客的博主 伶俐秀发,这篇文章主要介绍Pytorch_flatten()函数,现在分享给大家,希望可以做个参考。

Talk is cheap, show me the code.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
import torch t = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]) print(torch.flatten(t)) print(len(torch.flatten(t))) #tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) #12

        在什么机缘巧合的情况下我会想去用len(torch.flatten(t))求长度呢(虽然没有成功解决,但是我觉得这样去思考是好事情)?比方我在设计网络的时候最后这个全连接层的输入的时候,如果每次最后输入层我都得通过手算活着工具去计算cnn的输出feature map的size(虽然也不麻烦),但是我想着,那如果可以自己计算该多好,就想Keras里面经过flatten层后再接一个全连接层dense(num_class)这样,这也是pytorch与keras互补的地方,一个修改细节上的灵活,一个粗糙使用层面上的便捷。看下面这个函数我当时想的是用一个变了去取flatten的输出,但是网络的设计却是需要在_init_里面就设计好的,才继续调用foward(),逻辑上是不行的,目前看来还是只有更具公式手算设计网络或者用工具,我有介绍哦。

模型可视化与计算的一些工具_To be a better man-CSDN博客卷积神经网络(CNN)模型结构可视化工具卷积神经网络(CNN)模型结构可视化工具_sereasuesue的博客-CSDN博客_卷积神经网络软件ConvdrawJack Cui | ConvNetDrawConvdraw做的不够细,如果只是简单的画一下conv或者全连接层还是够的。如图深度学习中的网络可视化工具(非常轻量化)//netron深度学习中的网络可视化工具(非常轻量化)_nbxuwentao的博客-CSDN博客_深度学习网络可视化工具这个netron做的很好大家多多.https://blog.csdn.net/weixin_43332715/article/details/121846304

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Convolutional neural network (two convolutional layers) class ConvNet(nn.Module): def __init__(self, num_classes=2): super(ConvNet, self).__init__() self.layer1 = nn.Sequential( nn.Conv2d(3, 16, kernel_size=5, stride=1, padding=2),#一般Kernel_size=5,padding=2 nn.BatchNorm2d(16),#make feature's mean_value=1,variance=1,learn or fit better from good distribution nn.ReLU(),#standard activation fuction for cnn nn.MaxPool2d(kernel_size=2, stride=2))#demension_reduce self.layer2 = nn.Sequential( nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2)) self.fc = nn.Linear(56*56*32, num_classes) def forward(self, x): out = self.layer1(x) out = self.layer2(out) out = out.reshape(out.size(0), -1)#equal to flatten layer out out = self.fc(out) return out

最后

以上就是伶俐秀发最近收集整理的关于Pytorch_flatten()函数的全部内容,更多相关Pytorch_flatten()函数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部