神经网络的基本骨架

发布时间 2023-08-13 15:48:45作者: ydky

基本骨架

1.基本介绍

torch.nn官网

torcn.nn是专门为神经网络设计的模块化接口,可以用来定义和运行神经网络(Container为基本的框架模块)。

nn.Module官网(Base class for all neural network modules.)

nn.Module(torch.nn->Containers->Module)是nn中十分重要的类,包含网络各层的定义及forward方法,在用户自定义神经网络时,需要继承自nn.Module类(官网给出了示例)。

下图是对上面的示例进行解释:

2.搭建自己的神经网络

import torch
from torch import nn

class Base(nn.Module):#该神经网络实现了加一的功能
    def __init__(self):
        super(Base, self).__init__()

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

base = Base()
x = torch.tensor(1.0)
output = base(x)
print(output)