Pytorch学习之nn.Model

Pytorch学习之nn.Model

import torch
import torch.nn as nn

import torch.nn.functional as F

官网的例子

class Mdeol(nn.Module):  # 继承nn.Module这个类

    def __init__(self):  # 初始化函数
        super(Mdeol, self).__init__()  # 这个是调用父类初始化函数
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.cinv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):  # 向前传播函数 ===>经网络的主要骨架就在这里
        # 定义计算图
        x = self.conv1(x)
        x = F.relu(x)
        x = self.cinv2(x)
        x = F.relu(x)
        return x

自己定义一个

class qingfeng(nn.Module):
    # 第二种初始化方法code===>Genarate===>overrid methods
    def __init__(self):
        super().__init__()
        # 在这里我们并没有运用任何层,别疑惑

    def forward(self, inputs):
        output = inputs + 1
        return output

对神将网络进行实例化

qingfeng = qingfeng()
x = torch.tensor([[1.0, 2.0, 3.0], [4, 5, 6]])
output = qingfeng(x)
print(output)

tensor([[2., 3., 4.],
        [5., 6., 7.]])
# 向量化运算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值