91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

卷積網絡架構-LeNet-5

資源截圖

代碼片段和文件信息

import?torch
import?random
import?math
import?numpy?as?np
import?torch.nn?as?nn
import?torch.nn.functional?as?F
from?torchvision?import?datasets?transforms
import?matplotlib.pyplot?as?plt


class?MyLinear(nn.Module):
????def?__init__(self?in_features?out_features?bias=True):
????????super(MyLinear?self).__init__()??#?和自定義模型一樣,第一句話就是調用父類的構造函數
????????self.in_features?=?in_features
????????self.out_features?=?out_features
????????#?參數定義
????????self.weight?=?nn.Parameter(torch.Tensor(in_features?out_features))??#?使用Parameter來定義
????????if?bias:
????????????self.bias?=?nn.Parameter(torch.Tensor(out_features))??#?使用Parameter來定義
????????else:
????????????self.register_parameter(‘bias‘?None)
????????#?參數初始化
????????self.reset_parameters()

????def?reset_parameters(self):
????????stdv?=?1.?/?math.sqrt(self.weight.size(1))
????????self.weight.data.uniform_(-stdv?stdv)
????????if?self.bias?is?not?None:
????????????self.bias.data.uniform_(-stdv?stdv)

????def?forward(self?x):
????????y?=?x.mm(self.weight)?+?self.bias
????????return?y


class?MyConv(nn.Module):
????def?__init__(self?in_channels?out_channels?kernel_size?stride?padding?bias):
????????super(MyConv?self).__init__()

????????self.in_channels?self.out_channels?=?in_channels?out_channels??#?輸入通道數輸出通道數
????????self.kernel_size?self.stride?self.padding?=?kernel_size?stride?padding??#?卷積核的大小{k_W=k_H},卷積的步長;?padding的大小
????????self.bias_true?=?bias??#?偏置項?{Ture?False}

????????#?卷積核的數量(輸出通道數),輸入通道數,卷積核的大小:HW
????????self.weight?=?nn.Parameter(torch.Tensor(out_channels?in_channels?kernel_size?kernel_size))

????????if?self.bias_true?==?True:
????????????#?偏置項:?偏置項數量?=?卷積核的數量,所以需要out_channels個偏置參數
????????????self.bias?=?nn.Parameter(torch.Tensor(out_channels?1))

????????else:
????????????self.register_parameter(‘bias‘?None)
????????#?參數初始化
????????self.reset_parameters()

????def?forward(self?x):
????????#?Padding?補0操作???在x?左右上下?分別填充self.padding個0
????????ZeroPad?=?nn.ZeroPad2d(padding=(self.padding?self.padding?self.padding?self.padding))
????????x?=?ZeroPad(x)??#?Padding?操作

????????conv_x?=?self.conv_compute(x)??#?卷積操作

????????return?conv_x

????def?conv_compute(self?x):

????????#?x:??padding?后的?data
????????(batch?in_c?H?W)?=?x.shape??#?圖片的batchsiez,通道數,高,寬

????????(out_c?in_c?kernel_size_H?kernel_size_W)?=?self.weight.shape??#?卷積核的數量,通道數?卷積核的大小:HW

????????new_H?=?int(1?+?(H?-?kernel_size_H)?/?self.stride)??#?卷積后得到的圖片高
????????new_W?=?int(1?+?(W?-?kernel_size_W)?/?self.stride)??#?卷積后得到的圖片寬

????????x_unfold?=?F.unfold(x?(kernel_size_H?kernel_size_W)
????????????????????????????stride=self.stride)??#?x_unfold:(batch?in_c*k_H*k_W?L)????L:?(new_H*new_W)
????????w?=?self.weight

????????#??1.?(batch?L?in_c*k_H*k_W)?matmul?(in_c*k_H*k_Wout_c)-->??(batch?L?out_c)
????????#??2.?out_unf:?(batch?L?out_c)-->?(batch?out_c?L)
????????out_unf?=?x_unfold.transpose(1?2).m

評論

共有 條評論