我是靠谱客的博主 清新星月,最近开发中收集的这篇文章主要介绍tensorlflow和pytorch打印模型参数tensorflowpytorch还有torchkeras也可以打印参数torchkeras,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

tensorflow

import tensorflow.keras.backend as K
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint, EarlyStopping, ReduceLROnPlateau,History
from tensorflow.keras.layers import Dense, Input,Layer,InputSpec
from tensorflow.keras.models import Model,load_model
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.models import Sequential
# Set the input shape

## 建立模型
# Create the model
model = Sequential()
model.add(Dense(124, input_shape=(784,), activation='relu'))
model.add(Dense(32, activation='linear'))
print(model.summary())

在这里插入图片描述

pytorch

安装torchsummary

pip install torchsummary

example

from torchsummary import summary
import torch.nn as nn
model=nn.Sequential(nn.Linear(784,124),
                    nn.Linear(124,32))

print(summary(model,input_size=(1,784),batch_size=1))
print(784*124+124)
print(124*32+32)

在这里插入图片描述

还有torchkeras也可以打印参数

torchsummary

import time
import torch.nn as nn
import torch.nn.functional as F
class FC(nn.Module):
    def __init__(self):  
        super().__init__()  
        self.liner_1 = nn.Linear(40 * 40, 120)  
        self.liner_2 = nn.Linear(120, 84)  
        self.liner_3 = nn.Linear(84, 2)  
    def forward(self, input):
        x = input.view(-1, 40 * 40) 
        x = F.relu(self.liner_1(x))
        x = F.relu(self.liner_2(x))
        x = self.liner_3(x)
        return x
    
model = FC()
print(model)
summary(model,(3,40,40))

在这里插入图片描述

torchkeras

# pip install torchkeras
import torch
from torch import nn
from torchkeras import summary
 
def create_net():
    net = nn.Sequential()
    net.add_module('linear1', nn.Linear(15, 20))
    net.add_module('relu1', nn.ReLU())
    net.add_module('linear2', nn.Linear(20, 1))
    net.add_module('sigmoid', nn.Sigmoid())
    return net
 
# 创建模型
net = create_net()
# 使用torchkeras中的summary函数打印模型结构和参数
print(summary(net, input_shape=(15, )))

在这里插入图片描述

最后

以上就是清新星月为你收集整理的tensorlflow和pytorch打印模型参数tensorflowpytorch还有torchkeras也可以打印参数torchkeras的全部内容,希望文章能够帮你解决tensorlflow和pytorch打印模型参数tensorflowpytorch还有torchkeras也可以打印参数torchkeras所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部