时间:2021-05-22
Pytorch官方目前无法像tensorflow, caffe那样直接给出shape信息,详见
https://github.com/pytorch/pytorch/pull/3043
以下代码算一种workaround。由于CNN, RNN等模块实现不一样,添加其他模块支持可能需要改代码。
例如RNN中bias是bool类型,其权重也不是存于weight属性中,不过我们只关注shape够用了。
该方法必须构造一个输入调用forward后(model(x)调用)才可获取shape
以pytorch版CRNN为例,输出shape如下
{"Conv2d-1": {"input_shape": [1, 1, 32, 128],"output_shape": [1, 64, 32, 128],"trainable": true,"nb_params": 576},"ReLU-2": {"input_shape": [1, 64, 32, 128],"output_shape": [1, 64, 32, 128],"nb_params": 0},"MaxPool2d-3": {"input_shape": [1, 64, 32, 128],"output_shape": [1, 64, 16, 64],"nb_params": 0},"Conv2d-4": {"input_shape": [1, 64, 16, 64],"output_shape": [1, 128, 16, 64],"trainable": true,"nb_params": 73728},"ReLU-5": {"input_shape": [1, 128, 16, 64],"output_shape": [1, 128, 16, 64],"nb_params": 0},"MaxPool2d-6": {"input_shape": [1, 128, 16, 64],"output_shape": [1, 128, 8, 32],"nb_params": 0},"Conv2d-7": {"input_shape": [1, 128, 8, 32],"output_shape": [1, 256, 8, 32],"trainable": true,"nb_params": 294912},"BatchNorm2d-8": {"input_shape": [1, 256, 8, 32],"output_shape": [1, 256, 8, 32],"trainable": true,"nb_params": 256},"ReLU-9": {"input_shape": [1, 256, 8, 32],"output_shape": [1, 256, 8, 32],"nb_params": 0},"Conv2d-10": {"input_shape": [1, 256, 8, 32],"output_shape": [1, 256, 8, 32],"trainable": true,"nb_params": 589824},"ReLU-11": {"input_shape": [1, 256, 8, 32],"output_shape": [1, 256, 8, 32],"nb_params": 0},"MaxPool2d-12": {"input_shape": [1, 256, 8, 32],"output_shape": [1, 256, 4, 33],"nb_params": 0},"Conv2d-13": {"input_shape": [1, 256, 4, 33],"output_shape": [1, 512, 4, 33],"trainable": true,"nb_params": 1179648},"BatchNorm2d-14": {"input_shape": [1, 512, 4, 33],"output_shape": [1, 512, 4, 33],"trainable": true,"nb_params": 512},"ReLU-15": {"input_shape": [1, 512, 4, 33],"output_shape": [1, 512, 4, 33],"nb_params": 0},"Conv2d-16": {"input_shape": [1, 512, 4, 33],"output_shape": [1, 512, 4, 33],"trainable": true,"nb_params": 2359296},"ReLU-17": {"input_shape": [1, 512, 4, 33],"output_shape": [1, 512, 4, 33],"nb_params": 0},"MaxPool2d-18": {"input_shape": [1, 512, 4, 33],"output_shape": [1, 512, 2, 34],"nb_params": 0},"Conv2d-19": {"input_shape": [1, 512, 2, 34],"output_shape": [1, 512, 1, 33],"trainable": true,"nb_params": 1048576},"BatchNorm2d-20": {"input_shape": [1, 512, 1, 33],"output_shape": [1, 512, 1, 33],"trainable": true,"nb_params": 512},"ReLU-21": {"input_shape": [1, 512, 1, 33],"output_shape": [1, 512, 1, 33],"nb_params": 0},"LSTM-22": {"input_shape": [33, 1, 512],"0": {"output_shape": [33, 1, 512]},"1": {"0": {"output_shape": [2, 1, 256]},"1": {"output_shape": [2, 1, 256]}},"nb_params": 0},"Linear-23": {"input_shape": [33, 512],"output_shape": [33, 256],"trainable": true,"nb_params": 131072},"BidirectionalLSTM-24": {"input_shape": [33, 1, 512],"output_shape": [33, 1, 256],"nb_params": 0},"LSTM-25": {"input_shape": [33, 1, 256],"0": {"output_shape": [33, 1, 512]},"1": {"0": {"output_shape": [2, 1, 256]},"1": {"output_shape": [2, 1, 256]}},"nb_params": 0},"Linear-26": {"input_shape": [33, 512],"output_shape": [33, 3755],"trainable": true,"nb_params": 1922560},"BidirectionalLSTM-27": {"input_shape": [33, 1, 256],"output_shape": [33, 1, 3755],"nb_params": 0}}以上这篇pytorch中获取模型input/output shape实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1.一般的模型构造、训练、测试流程#模型构造inputs=keras.Input(shape=(784,),name='mnist_input')h1=laye
回文数就是指整数倒过来和原整数相等。Example1:Input:121Output:trueExample2:Input:-121Output:falseEx
defdemo_input_and_output():input=yield'whatistheinput?'yield'inputis:%s'%inp
keras根据层名称来初始化网络defget_model(input_shape1=[75,75,3],input_shape2=[1],weights=Non
1模型定义 和TF很像,Pytorch也通过继承父类来搭建模型,同样也是实现两个方法。在TF中是__init__()和call(),在Pytorch中则是__