初识格式
导入torch,torch.nn,torch.nn.functional(没用上)
import torch
import torch.nn as nn
import torch.nn.funcitional as F
nn.modules 需要重写__init__方法,注释如下:
"""Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:: 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)) Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool """
class OneplusOne(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):
out = x + 1
# x = F.relu(self.conv1(x))
# return F.relu(self.conv2(x))
return out
Mynn = OneplusOne()
one = torch.tensor(1.0)
two = Mynn(one)
print(two)
super()可以集成父类的方法
卷积、池化、激活、线性层
Conv2d:
Args: in_channels (int): Number of channels in the input image out_channels (int): Number of channels produced by the convolution kernel_size (int or tuple): Size of the convolving kernel stride (int or tuple, optional): Stride of the convolution. Default: 1 padding (int, tuple or str, optional): Padding added to all four sides of the input. Default: 0 padding_mode (str, optional): ``'zeros'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'zeros'`` dilation (int or tuple, optional): Spacing between kernel elements. Default: 1 groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1 bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``Examples: >>> # With square kernels and equal stride >>> m = nn.Conv2d(16, 33, 3, stride=2) >>> # non-square kernels and unequal stride and with padding >>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2)) >>> # non-square kernels and unequal stride and with padding and dilation >>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1)) >>> input = torch.randn(20, 16, 50, 100) >>> output = m(input)
- in_channels,第一个参数为输入通道数,例如3通道的图片就是3
- out_channels,第二个参数为输出channel,通道数变多代表使用了不同的多个kernel进行卷积
- kernel_size,表示卷积核的大小
- stride代表卷积核滑动的步长
- padding是否在原图周边添加pad
- padding_mode调整填入的数字
MaxPool2d:
Args: kernel_size: the size of the window to take a max over stride: the stride of the window. Default value is :attr:`kernel_size` padding: Implicit negative infinity padding to be added on both sides dilation: a parameter that controls the stride of elements in the window return_indices: if ``True``, will return the max indices along with the outputs. Useful for :class:`torch.nn.MaxUnpool2d` later ceil_mode: when True, will use `ceil` instead of `floor` to compute the output shapeExamples:: >>> # pool of square window of size=3, stride=2 >>> m = nn.MaxPool2d(3, stride=2) >>> # pool of non-square window >>> m = nn.MaxPool2d((3, 2), stride=(2, 1)) >>> input = torch.randn(20, 16, 50, 32) >>> output = m(input)
- kernel_size,池化层的大小
- stride,步长
- ceil_mode,如果模式为True,则会在除不尽的地方仍然选择最大项
Relu:
大于0的为原数,小于0为0
Sigmoid:
sigmoid函数
Linear:
Args: in_features: size of each input sample out_features: size of each output sample bias: If set to ``False``, the layer will not learn an additive bias. Default: ``True``Examples:: >>> m = nn.Linear(20, 30) >>> input = torch.randn(128, 20) >>> output = m(input) >>> print(output.size()) torch.Size([128, 30])
线性层可以将一维数据通过全连接输出
- in_features,输入参数长度
- out_features,输出参数长度
- bias,偏置
import torch
import torch.nn as nn
from torch.nn import Conv2d, MaxPool2d, ReLU, Sigmoid, Linear
from torchvision import datasets
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader
class TestConv(nn.Module):
def __init__(self):
super(TestConv, self).__init__()
self.conv1 = Conv2d(3, 6, 3, stride=1)
self.maxpool1 = MaxPool2d(3, ceil_mode=True)
self.sigmoid = Sigmoid()
self.relu = ReLU()
# 64*3*32*32 = 196608
self.linear = Linear(196608, 10)
def forward(self, x):
a = self.conv1(x)
b = self.maxpool1(x)
c = self.sigmoid(x)
# one_dimension_data = torch.reshape(x, [1, 1, 1, -1])
one_dimension_data = torch.flatten(x)
d = self.linear(one_dimension_data)
return a, b, c, d
trans_compose = transforms.Compose(
[
transforms.ToTensor()
]
)
if __name__ == "__main__":
test_dataset = datasets.CIFAR10("dataset", False, trans_compose, download=True)
test_loader = DataLoader(test_dataset, 64, True, drop_last=True) # return img and label
writer = SummaryWriter("CIFAR10_logs")
count = 0
model = TestConv()
for data, _ in test_loader:
# shape:[batchsize:64, channels:6, 30, 30]
img_conv, img_maxpool, img_sigmoid, img_linear = model.forward(data)
# shape:[?, 3, 30, 30]
img_reshape = torch.reshape(img_conv, [-1, 3, 30, 30])
writer.add_images("original", data, count)
writer.add_images("after_conv", img_reshape, count)
writer.add_images("after_maxpool", img_maxpool, count)
writer.add_images("after_sigmoid", img_sigmoid, count)
if count == 0:
print("original data shape:", data.shape)
print("conv shape:", img_conv.shape)
print("reshape shape:", img_reshape.shape)
print("maxpool shape:", img_maxpool.shape) # 32/3=10...1 ceil:10+1=11
print("sigmoid shape:", img_sigmoid.shape)
print("linear shape:", img_linear.shape)
count += 1
writer.close()