在 PyTorch 中,可以使用 torchsummary
庫(kù)來(lái)實(shí)現(xiàn)對(duì) PyTorch 模型的結(jié)構(gòu)及參數(shù)統(tǒng)計(jì)的輸出,其可以方便我們查看每層輸入、輸出的維度以及參數(shù)數(shù)量等信息。
安裝 torchsummary
庫(kù):
pip install torchsummary
使用方法如下:
import torch
from torchsummary import summary
# 定義一個(gè)模型
model = torch.nn.Sequential(
)
# 打印模型所有層的參數(shù)統(tǒng)計(jì)
summary(model, (3, 32, 32))
其中,model
是需要查看的模型,(3, 32, 32)
表示模型的輸入維度,即 C = 3,H = 32,W = 32。運(yùn)行后可以看到所有層輸入、輸出的維度以及參數(shù)數(shù)量等信息。
有個(gè)大坑
AttributeError: ‘xxx’ object has no attribute ‘size’
AttributeError: ‘int’ object has no attribute ‘size’
類似錯(cuò)誤文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-563799.html
pip uninstall torchsummary
pip install torch-summary
解決問(wèn)題文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-563799.html
到了這里,關(guān)于Pytorch模型如何查看每層輸入維度輸出維度的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!