神经网络的基本骨架--nn.Module的使用

神经网络的基本骨架--nn.Module的使用_第1张图片

import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

输入x经过一次卷积,再经过一次非线性,再经过一次卷积,再经过一次非线性,才得到输出

import torch
from torch import nn


class XuZhenyu(nn.Module):

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

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

xzy = XuZhenyu()
x = torch.tensor(1.0)
output = xzy(x)
print(output)

 结果:

神经网络的基本骨架--nn.Module的使用_第2张图片

debug调试

神经网络的基本骨架--nn.Module的使用_第3张图片 

你可能感兴趣的:(深度学习,神经网络,深度学习,pytorch)