├── README.md ├── class10卷积神经网络高阶 └── MINT_CNN.py ├── class1线性模型 ├── linear_model.py └── linear_model2.py ├── class2梯度下降算法 ├── GradientDescent.py └── GradientDescent2.py ├── class3BP ├── BP.py └── BP2.py ├── class4使用pytorch实现线性回归 ├── linear_nn.py └── linear_nn2.py ├── class5逻辑斯蒂回归 └── LogisticRegression.py ├── class6多维特征处理 └── multiplyFeature.py ├── class7加载数据集 └── Loader.py ├── class8多分类问题 └── multiply.py ├── class9卷积神经网络基础 ├── MINST.py ├── MINS_GPU.py ├── conv.py ├── maxpoling.py ├── padding.py └── stride.py ├── data ├── diabetes.csv.gz ├── mnist │ └── MNIST │ │ ├── processed │ │ ├── test.pt │ │ └── training.pt │ │ └── raw │ │ ├── t10k-images-idx3-ubyte │ │ ├── t10k-images-idx3-ubyte.gz │ │ ├── t10k-labels-idx1-ubyte │ │ ├── t10k-labels-idx1-ubyte.gz │ │ ├── train-images-idx3-ubyte │ │ ├── train-images-idx3-ubyte.gz │ │ ├── train-labels-idx1-ubyte │ │ └── train-labels-idx1-ubyte.gz ├── names_test.csv.gz └── names_train.csv.gz ├── dataset └── mnist │ └── MNIST │ └── raw │ └── train-images-idx3-ubyte.gz ├── dats └── mnist │ └── MNIST │ ├── processed │ ├── test.pt │ └── training.pt │ └── raw │ ├── t10k-images-idx3-ubyte │ ├── t10k-images-idx3-ubyte.gz │ ├── t10k-labels-idx1-ubyte │ ├── t10k-labels-idx1-ubyte.gz │ ├── train-images-idx3-ubyte │ ├── train-images-idx3-ubyte.gz │ ├── train-labels-idx1-ubyte │ └── train-labels-idx1-ubyte.gz └── 课件 ├── Lecture_01_Overview.pdf ├── Lecture_02_Linear_Model.pdf ├── Lecture_03_Gradient_Descent.pdf ├── Lecture_04_Back_Propagation.pdf ├── Lecture_05_Linear_Regression_with_PyTorch.pdf ├── Lecture_06_Logistic_Regression.pdf ├── Lecture_07_Multiple_Dimension_Input.pdf ├── Lecture_08_Dataset_and_Dataloader.pdf ├── Lecture_09_Softmax_Classifier.pdf ├── Lecture_10_Basic_CNN.pdf ├── Lecture_11_Advanced_CNN.pdf ├── Lecture_12_Basic_RNN.pdf ├── Lecture_13_RNN_Classifier.pdf ├── diabetes.csv.gz ├── names_test.csv.gz └── names_train.csv.gz /README.md: -------------------------------------------------------------------------------- 1 | ## Pytorch实践课程 2 | 3 | > 官网:https://pytorch.org/tutorials/beginner/pytorch_with_examples.html#id14 4 | 5 | 6 | 7 | ### 第1讲——线性回归 8 | 9 | - 在训练过程中绘制图像 10 | 11 | 使用 `visdom`:https://github.com/fossasia/visdom 12 | 13 | 14 | 15 | ### 第2讲——梯度下降法 16 | 17 | - 使用随机梯度下降 18 | 19 | - 性能和复杂度折中,小批量方式mini-batch 20 | 21 | - 训练失败可能是学习率太大 22 | - 一般我们选择随机梯度下降 23 | 24 | 25 | 26 | ### 第3讲——BP 27 | 28 | - 矩阵的求导:http://www.math.uwaterloo.ca/~hwolkowi/matrixcookbook.pdf 29 | 30 | - 要及时对梯度清零 31 | 32 | 33 | 34 | ### 第4讲——用Pytorch实现线性回归 35 | 36 | - 不同优化器的作用:https://blog.csdn.net/weixin_44841652/article/details/105068509 37 | - 四步骤 38 | 39 | ![image-20210204102329528](https://i.loli.net/2021/02/04/CX8oJKzT7QvbMFe.png) 40 | 41 | - 使用模板 42 | 43 | ### 第5讲 ——逻辑斯蒂回归 44 | 45 | - 分类问题的思路 46 | 47 | ``` 48 | 离散,求出每一个类的概率,然后取其中分类概率最大的类属作为分类结果 49 | ``` 50 | 51 | - 几类sigmod函数 52 | 53 | ![image-20210204104257546](https://i.loli.net/2021/02/04/PzJdCpqlWByIkij.png) 54 | 55 | - 逻辑回归需要新增激活函数 56 | 57 | ![image-20210204110144950](https://i.loli.net/2021/02/04/EIWzsM4U9QlKSC6.png) 58 | 59 | - 相应损失函数书的计算有所变化 60 | 61 | ![image-20210204110210074](https://i.loli.net/2021/02/04/bzfuClaqQsMWe9V.png) 62 | 63 | - BCELoss - Binary CrossEntropyLoss (二维) 64 | 65 | ``` 66 | BCELoss 是CrossEntropyLoss的一个特例,只用于二分类问题,而CrossEntropyLoss可以用于二分类,也可以用于多分类。 67 | 如果是二分类问题,建议BCELoss 68 | ``` 69 | 70 | 71 | 72 | 73 | 74 | 75 | ### 第6讲——多维特征的输入 76 | 77 | - 数据输入 78 | 79 | ```python 80 | # prepare dataset 81 | xy = np.loadtxt('../data/diabetes.csv', delimiter=',', dtype=np.float32) 82 | x_data = torch.from_numpy(xy[:, :-1]) # 取出前-1列 Feature 83 | y_data = torch.from_numpy(xy[:, [-1]]) # 取最后一列 label,一定要使用[-1],否则取出的不是一个矩阵,而是一个list 84 | ``` 85 | 86 | 87 | 88 | - 层的输入关系 89 | 90 | ![image-20210204162551707](https://i.loli.net/2021/02/04/5xWuhJIRc4EiqTA.png) 91 | 92 | ![image-20210204162748125](https://i.loli.net/2021/02/04/5gSUpTkfbleRv1n.png) 93 | 94 | - 不同的激活函数:https://pytorch.org/docs/stable/nn.html#non-linear-activations-weighted-sum-nonlinearity 95 | 96 | image-20210204162825792 97 | 98 | 99 | 100 | ### 第7讲——加载数据集 101 | 102 | - DataSet 是抽象类,不能实例化对象,主要是用于构造我们的数据集 103 | - DataLoader 需要获取DataSet提供的索引[i]和len;用来帮助我们加载数据,比如说做shuffle(提高数据集的随机性),batch_size,能拿出Mini-Batch进行训练。它帮我们自动完成这些工作。DataLoader可实例化对象。 104 | - 使用min-batch可以利用并行性,提高训练效率,但是准确率不如一次性加载。 105 | 106 | 107 | 108 | > 代码说明 109 | 110 | 1、需要mini_batch 就需要import DataSet和DataLoader 111 | 112 | 2、继承DataSet的类需要重写init,getitem,len魔法函数。分别是为了加载数据集,获取数据索引,获取数据总量。 113 | 114 | 3、DataLoader对数据集先打乱(shuffle),然后划分成mini_batch。 115 | 116 | 4、len函数的返回值 除以 batch_size 的结果就是每一轮epoch中需要迭代的次数。 117 | 118 | 5、inputs, labels = data中的inputs的shape是[32,8],labels 的shape是[32,1]。也就是说mini_batch在这个地方体现的 119 | 120 | ```python 121 | # 准备数据集 122 | class DiabetesDataset(Dataset): # 抽象类DataSet 123 | def __init__(self, filepath): 124 | xy = np.loadtxt(filepath, delimiter=',', dtype=np.float32) 125 | self.len = xy.shape[0] # shape(多少行,多少列) 126 | self.x_data = torch.from_numpy(xy[:, :-1]) 127 | self.y_data = torch.from_numpy(xy[:, [-1]]) 128 | 129 | def __getitem__(self, index): 130 | return self.x_data[index], self.y_data[index] 131 | 132 | def __len__(self): 133 | return self.len 134 | 135 | # dataset对象 136 | dataset = DiabetesDataset('../data/diabetes.csv') 137 | 138 | # 使用DataLoader加载数据 139 | train_loader = DataLoader(dataset=dataset, # dataSet对象 140 | batch_size=32, # 每个batch的条数 141 | shuffle=True, # 是否打乱 142 | num_workers=4) # 多线程一般设置为4和8 143 | ``` 144 | 145 | 146 | 147 | 148 | 149 | ### 第8讲——多分类 150 | 151 | - 处理多分类问题的时候,由于各Feature的分类概率很大,我们要引入一个`Softmax`进行归一化 152 | 153 | ![image-20210204181519314](https://i.loli.net/2021/02/04/kpGO2WhzQsUBYJM.png) 154 | 155 | ```python 156 | y_pred = np.exp(z) / np.exp(z).sum() 157 | ``` 158 | 159 | **CrossEntropyLoss <==> LogSoftmax + NLLLoss** 160 | 161 | - 计算交叉熵 162 | 163 | ![image-20210204181807558](https://i.loli.net/2021/02/04/lx1gHZdTLKUtmyn.png) 164 | 165 | 166 | 167 | ```python 168 | import torch 169 | y = torch.LongTensor([0]) # 必须要用LongTensor 170 | z = torch.Tensor([[0.2, 0.1, -0.1]]) 171 | criterion = torch.nn.CrossEntropyLoss() 172 | loss = criterion(z, y) 173 | print(loss) 174 | ``` 175 | 176 | > 手写数字 177 | 178 | ![image-20210204191252515](https://i.loli.net/2021/02/04/ISCunqsaTcOXNzk.png) 179 | 180 | ``` 181 | 如果没有卷积神经网络保存,我们这里使用的是全连接的方法,直接将其转化为一长串的一维张量 182 | ``` 183 | 184 | 185 | 186 | ### 第9讲——卷积神经网络CNN 187 | 188 | - 图像描述 189 | 190 | ``` 191 | 图像描述:C×W×H 192 | 左上角为o 上侧为W, 左侧为H 193 | ``` 194 | 195 | ![image-20210204193544835](https://i.loli.net/2021/02/04/8XM24SLqazKQClW.png) 196 | 197 | - 卷积运算 198 | 199 | ![image-20210204194005417](https://i.loli.net/2021/02/04/sWiE8LnwoNgSy7t.png) 200 | 201 | - 通过卷积核得到m×W×H的输出 202 | 203 | ![image-20210204194754403](https://i.loli.net/2021/02/04/mFPp28ShrZxugo4.png) 204 | 205 | - 实例 206 | 207 | ```python 208 | import torch 209 | in_channels, out_channels= 5, 10 # 输入输出通道 210 | width, height = 100, 100 # 输入图像的宽高 211 | kernel_size = 3 # 卷积核的大小 212 | batch_size = 1 # batch的个数 213 | # 输入 214 | # [B×C×W×H] 215 | input = torch.randn(batch_size, # 随机生成一个张量输入,模拟图像 216 | in_channels, # input = (1,5,100,100) 217 | width, # 对输入没有要求,除了in_channels必须要为5 否则卷积层不起作用 218 | height) 219 | 220 | # 定义卷积 221 | conv_layer = torch.nn.Conv2d( in_channels, # 输入通道 222 | out_channels, # 输出通道 223 | kernel_size=kernel_size)# 卷积核 224 | 225 | output = conv_layer(input) # 做卷积 226 | 227 | print(input.shape) 228 | print(output.shape) 229 | print(conv_layer.weight.shape) 230 | 231 | >> 232 | torch.Size([1, 5, 100, 100]) 233 | torch.Size([1, 10, 98, 98]) 234 | torch.Size([10, 5, 3, 3]) 235 | ``` 236 | 237 | - padding 238 | 239 | ![image-20210204202024225](https://i.loli.net/2021/02/04/lvX7WqYATUuGjdx.png) 240 | 241 | - stride 242 | 243 | ![image-20210204202624019](https://i.loli.net/2021/02/04/wALIMe3GDT9cBu5.png) 244 | 245 | - maxpooling 246 | 247 | ![image-20210204202830953](https://i.loli.net/2021/02/04/scjVfJ7UqvRMGBe.png) 248 | 249 | ```python 250 | maxpooling_layer = torch.nn.MaxPool2d(kernel_size=2) 251 | ``` 252 | 253 | - 一个卷积神经网络样例 254 | 255 | ![image-20210204203451222](https://i.loli.net/2021/02/04/BKWY7XnsOAU1QDN.png) 256 | 257 | - 全连接神经网络 258 | 259 | ![image-20210204203643264](https://i.loli.net/2021/02/04/ArB5MlT3zij4Q2U.png) 260 | 261 | 262 | 263 | > 迁移GPU 264 | 265 | - 指定device 266 | 267 | ![image-20210204211300547](https://i.loli.net/2021/02/04/ZwRHAhQzCEeGp4N.png) 268 | 269 | - 迁移输入: 270 | 271 | ![image-20210204211407945](https://i.loli.net/2021/02/04/J2s59VfQq1Kx6ky.png) 272 | 273 | 274 | 275 | ### 第10讲——卷积神经网络高阶CNN 276 | 277 | - 梯度消失 278 | 279 | 梯度趋近于0,权重无法得到有效更新 280 | 281 | - 对于很复杂的网络,我们可以用新的类去封装它 282 | - 如果有不同的运行分支,我们可以用cat拼接它 283 | 284 | 285 | 286 | 287 | 288 | ### 第11讲——循环神经网络RNN 289 | 290 | - 常常用于处理具有序列关系的问题 291 | 292 | ``` 293 | 天气预测 294 | 自然语言 295 | ``` 296 | 297 | - what ‘s RNN 298 | 299 | ![image-20210204215941018](https://i.loli.net/2021/02/04/4rTM3W9UAc7C2SN.png) 300 | 301 | 302 | 303 | 304 | 305 | ### 第13讲——循环神经网络RNN高阶 -------------------------------------------------------------------------------- /class10卷积神经网络高阶/MINT_CNN.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torchvision import transforms 4 | from torchvision import datasets 5 | from torch.utils.data import DataLoader 6 | import torch.nn.functional as F 7 | import torch.optim as optim 8 | 9 | # prepare dataset 10 | 11 | batch_size = 64 12 | transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) # 归一化,均值和方差 13 | 14 | train_dataset = datasets.MNIST(root='../data/mnist/', train=True, download=True, transform=transform) 15 | train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size) 16 | test_dataset = datasets.MNIST(root='../data/mnist/', train=False, download=True, transform=transform) 17 | test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size) 18 | 19 | # design model using class 20 | class InceptionA(nn.Module): 21 | def __init__(self, in_channels): 22 | super(InceptionA, self).__init__() 23 | self.branch1x1 = nn.Conv2d(in_channels, 16, kernel_size=1) 24 | 25 | self.branch5x5_1 = nn.Conv2d(in_channels, 16, kernel_size=1) 26 | self.branch5x5_2 = nn.Conv2d(16, 24, kernel_size=5, padding=2) 27 | 28 | self.branch3x3_1 = nn.Conv2d(in_channels, 16, kernel_size=1) 29 | self.branch3x3_2 = nn.Conv2d(16, 24, kernel_size=3, padding=1) 30 | self.branch3x3_3 = nn.Conv2d(24, 24, kernel_size=3, padding=1) 31 | 32 | self.branch_pool = nn.Conv2d(in_channels, 24, kernel_size=1) 33 | 34 | def forward(self, x): 35 | branch1x1 = self.branch1x1(x) 36 | 37 | branch5x5 = self.branch5x5_1(x) 38 | branch5x5 = self.branch5x5_2(branch5x5) 39 | 40 | branch3x3 = self.branch3x3_1(x) 41 | branch3x3 = self.branch3x3_2(branch3x3) 42 | branch3x3 = self.branch3x3_3(branch3x3) 43 | 44 | branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1) 45 | branch_pool = self.branch_pool(branch_pool) 46 | 47 | outputs = [branch1x1, branch5x5, branch3x3, branch_pool] 48 | return torch.cat(outputs, dim=1) # b,c,w,h c对应的是dim=1 49 | 50 | class Net(nn.Module): 51 | def __init__(self): 52 | super(Net, self).__init__() 53 | self.conv1 = nn.Conv2d(1, 10, kernel_size=5) 54 | self.conv2 = nn.Conv2d(88, 20, kernel_size=5) # 88 = 24x3 + 16 55 | 56 | self.incep1 = InceptionA(in_channels=10) # 与conv1 中的10对应 57 | self.incep2 = InceptionA(in_channels=20) # 与conv2 中的20对应 58 | 59 | self.mp = nn.MaxPool2d(2) 60 | self.fc = nn.Linear(1408, 10) 61 | 62 | 63 | def forward(self, x): 64 | in_size = x.size(0) 65 | x = F.relu(self.mp(self.conv1(x))) 66 | x = self.incep1(x) 67 | x = F.relu(self.mp(self.conv2(x))) 68 | x = self.incep2(x) 69 | x = x.view(in_size, -1) 70 | x = self.fc(x) 71 | 72 | return x 73 | 74 | model = Net() 75 | 76 | # construct loss and optimizer 77 | criterion = torch.nn.CrossEntropyLoss() 78 | optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) 79 | 80 | # training cycle forward, backward, update 81 | 82 | 83 | def train(epoch): 84 | running_loss = 0.0 85 | for batch_idx, data in enumerate(train_loader, 0): 86 | inputs, target = data 87 | optimizer.zero_grad() 88 | 89 | outputs = model(inputs) 90 | loss = criterion(outputs, target) 91 | loss.backward() 92 | optimizer.step() 93 | 94 | running_loss += loss.item() 95 | if batch_idx % 300 == 299: 96 | print('[%d, %5d] loss: %.3f' % (epoch+1, batch_idx+1, running_loss/300)) 97 | running_loss = 0.0 98 | 99 | 100 | def test(): 101 | correct = 0 102 | total = 0 103 | with torch.no_grad(): 104 | for data in test_loader: 105 | images, labels = data 106 | outputs = model(images) 107 | _, predicted = torch.max(outputs.data, dim=1) 108 | total += labels.size(0) 109 | correct += (predicted == labels).sum().item() 110 | print('accuracy on test set: %d %% ' % (100*correct/total)) 111 | 112 | 113 | if __name__ == '__main__': 114 | for epoch in range(10): 115 | train(epoch) 116 | test() -------------------------------------------------------------------------------- /class1线性模型/linear_model.py: -------------------------------------------------------------------------------- 1 | # 绘图 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # 数据集 6 | x_data = [1.0, 2.0, 3.0] 7 | y_data = [2.0, 4.0, 6.0] 8 | 9 | # 模型 前馈 10 | def forward(x): 11 | return x*w 12 | # 损失函数 13 | def loss(x, y): 14 | y_pred = forward(x) 15 | return (y_pred - y)**2 16 | 17 | # 保存权重和损失 18 | w_list = [] 19 | mse_list = [] 20 | 21 | # 穷举法 22 | for w in np.arange(0.0, 4.1, 0.1): 23 | print("w=", w) 24 | l_sum = 0 25 | # 将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表 26 | for x_val, y_val in zip(x_data, y_data): 27 | # 计算预测 28 | y_pred_val = forward(x_val) 29 | # 计算损失 30 | loss_val = loss(x_val, y_val) 31 | # 求和 32 | l_sum += loss_val 33 | print('\t', x_val, y_val, y_pred_val, loss_val) 34 | print('MSE=', l_sum/3) 35 | # 添加到列表 36 | w_list.append(w) 37 | mse_list.append(l_sum/3) 38 | 39 | # 绘制图形 40 | plt.plot(w_list,mse_list) 41 | plt.ylabel('Loss') 42 | plt.xlabel('w') 43 | plt.show() -------------------------------------------------------------------------------- /class1线性模型/linear_model2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from mpl_toolkits.mplot3d import Axes3D 4 | 5 | #这里设函数为y=3x+2 6 | x_data = [1.0,2.0,3.0] 7 | y_data = [5.0,8.0,11.0] 8 | 9 | # 前馈 10 | def forward(x): 11 | return x * w + b 12 | # 损失 13 | def loss(x,y): 14 | y_pred = forward(x) 15 | return (y_pred-y)*(y_pred-y) 16 | 17 | # 记录训练参数 18 | mse_list = [] 19 | W=np.arange(0.0,4.1,0.1) 20 | B=np.arange(0.0,4.1,0.1) 21 | 22 | # 生成网格采样点 23 | [w,b]=np.meshgrid(W,B) 24 | 25 | # 训练 26 | l_sum = 0 27 | for x_val, y_val in zip(x_data, y_data): 28 | y_pred_val = forward(x_val) # 自动计算了整个列表,不用循环 29 | print(y_pred_val) 30 | loss_val = loss(x_val, y_val) 31 | l_sum += loss_val 32 | 33 | # 绘图 34 | fig = plt.figure() 35 | ax = Axes3D(fig) 36 | ax.plot_surface(w, b, l_sum/3) 37 | plt.show() 38 | -------------------------------------------------------------------------------- /class2梯度下降算法/GradientDescent.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | 3 | 4 | # 梯度下降算法 5 | # prepare the training set 6 | # 数据集 7 | x_data = [1.0, 2.0, 3.0] 8 | y_data = [2.0, 4.0, 6.0] 9 | 10 | # initial guess of weight 11 | # 初始权重 12 | w = 1.0 13 | 14 | # 前馈 15 | # define the model linear model y = w*x 16 | def forward(x): 17 | return x*w 18 | 19 | # 定义损失代价函数 20 | #define the cost function MSE 21 | def cost(xs, ys): 22 | cost = 0 23 | for x, y in zip(xs,ys): 24 | y_pred = forward(x) 25 | cost += (y_pred - y)**2 26 | return cost / len(xs) 27 | 28 | # 定义梯度,更新w朝着梯度的最优方向搜索 29 | # define the gradient function gd 30 | def gradient(xs,ys): 31 | grad = 0 32 | for x, y in zip(xs,ys): 33 | grad += 2*x*(x*w - y) 34 | return grad / len(xs) 35 | 36 | # 记录 37 | epoch_list = [] 38 | cost_list = [] 39 | print('predict (before training)', 4, forward(4)) 40 | 41 | # 训练 42 | for epoch in range(100): 43 | cost_val = cost(x_data, y_data) 44 | grad_val = gradient(x_data, y_data) 45 | # 更新权值 46 | w-= 0.01 * grad_val # 0.01 learning rate 47 | print('epoch:', epoch, 'w=', w, 'loss=', cost_val) 48 | epoch_list.append(epoch) 49 | cost_list.append(cost_val) 50 | 51 | # 绘图 52 | print('predict (after training)', 4, forward(4)) 53 | plt.plot(epoch_list,cost_list) 54 | plt.ylabel('cost') 55 | plt.xlabel('epoch') 56 | plt.show() -------------------------------------------------------------------------------- /class2梯度下降算法/GradientDescent2.py: -------------------------------------------------------------------------------- 1 | # 随机梯度下降 2 | import matplotlib.pyplot as plt 3 | 4 | x_data = [1.0, 2.0, 3.0] 5 | y_data = [2.0, 4.0, 6.0] 6 | 7 | w = 1.0 8 | 9 | def forward(x): 10 | return x*w 11 | 12 | # calculate loss function 13 | def loss(x, y): 14 | y_pred = forward(x) 15 | return (y_pred - y)**2 16 | 17 | # 定义随机梯度,不是整体的损失 18 | # define the gradient function sgd 19 | def gradient(x, y): 20 | return 2*x*(x*w - y) 21 | 22 | epoch_list = [] 23 | loss_list = [] 24 | print('predict (before training)', 4, forward(4)) 25 | for epoch in range(100): 26 | for x,y in zip(x_data, y_data): 27 | # 每一样本都进行梯度计算和权值更新 28 | grad = gradient(x,y) 29 | w = w - 0.01*grad # update weight by every grad of sample of training set 30 | print("\tgrad:", x, y,grad) 31 | l = loss(x,y) 32 | print("progress:",epoch,"w=",w,"loss=",l) 33 | epoch_list.append(epoch) 34 | loss_list.append(l) 35 | 36 | # 绘图 37 | print('predict (after training)', 4, forward(4)) 38 | plt.plot(epoch_list,loss_list) 39 | plt.ylabel('loss') 40 | plt.xlabel('epoch') 41 | plt.show() -------------------------------------------------------------------------------- /class3BP/BP.py: -------------------------------------------------------------------------------- 1 | # BP反向传播 2 | import torch 3 | 4 | # 数据集 5 | x_data = [1.0, 2.0, 3.0] 6 | y_data = [2.0, 4.0, 6.0] 7 | 8 | # 定义torch 9 | w = torch.Tensor([1.0]) # w的初始值为1.0 10 | w.requires_grad = True # 需要的计算梯度 11 | 12 | # 前馈和损失的计算实际上就构建了计算图 13 | # 前馈 14 | def forward(x): 15 | return x*w # w是一个Tensor 16 | 17 | # 损失 18 | def loss(x,y): 19 | y_pred = forward(x) 20 | return (y_pred - y)**2 21 | 22 | # 计算训练前的预测值(输出) 23 | print("predict (before training)", 4, forward(4).item()) 24 | 25 | # 开始训练 26 | for epoch in range(100): 27 | for x,y in zip(x_data,y_data): 28 | l = loss(x,y) # l是一个张量,tensor主要是在建立计算图 forward 29 | l.backward() # 使用backward自动计算计算图的反向传播 30 | w.data = w.data - 0.01*w.grad.data # 更新权重 31 | 32 | # 更新权重之后要清零梯度 33 | w.grad.data.zero_() 34 | print('progress:', epoch, l.item()) 35 | 36 | # 预测 37 | print("predict (after training)", 4, forward(4).item()) 38 | -------------------------------------------------------------------------------- /class3BP/BP2.py: -------------------------------------------------------------------------------- 1 | # 二次模型y=w1x²+w2x+b,损失函数loss=(ŷ-y)² 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import torch 5 | 6 | x_data = [1.0,2.0,3.0] 7 | y_data = [2.0,4.0,6.0] 8 | 9 | w1 = torch.Tensor([1.0]) #初始权值 10 | w1.requires_grad = True #计算梯度,默认不计算 11 | 12 | w2 = torch.Tensor([1.0]) 13 | w2.requires_grad = True 14 | 15 | b = torch.Tensor([1.0]) 16 | b.requires_grad = True 17 | 18 | # 前馈 19 | def forward(x): 20 | return w1 * x**2 + w2 * x + b 21 | 22 | # 损失函数 23 | def loss(x,y):#构建计算图 24 | y_pred = forward(x) 25 | return (y_pred-y) **2 26 | 27 | print('Predict (befortraining)',4,forward(4)) 28 | 29 | for epoch in range(100): 30 | l = loss(1, 2) 31 | for x,y in zip(x_data,y_data): 32 | l = loss(x, y) 33 | l.backward() 34 | print('\tgrad:',x,y,w1.grad.item(),w2.grad.item(),b.grad.item()) 35 | w1.data = w1.data - 0.01*w1.grad.data #注意这里的grad是一个tensor,所以要取他的data 36 | w2.data = w2.data - 0.01 * w2.grad.data 37 | b.data = b.data - 0.01 * b.grad.data 38 | 39 | # 释放梯度 40 | w1.grad.data.zero_() 41 | w2.grad.data.zero_() 42 | b.grad.data.zero_() 43 | 44 | print('Epoch:',epoch,l.item()) 45 | 46 | print('Predict(after training)',4,forward(4).item()) 47 | -------------------------------------------------------------------------------- /class4使用pytorch实现线性回归/linear_nn.py: -------------------------------------------------------------------------------- 1 | # 线性回归 2 | import torch 3 | # 1.准备数据集 4 | # x,y是矩阵,3行1列 也就是说总共有3个数据,每个数据只有1个特征 5 | x_data = torch.Tensor([[1.0], [2.0], [3.0]]) 6 | y_data = torch.Tensor([[2.0], [4.0], [6.0]]) 7 | 8 | # 2.设计模型类 9 | class LinearModel(torch.nn.Module): 10 | # 构造 11 | def __init__(self): 12 | super(LinearModel, self).__init__() # 父类 13 | # (1,1)是指输入x和输出y的特征维度,这里数据集中的x和y的特征都是1维的 14 | # 该线性层需要学习的参数是w和b 获取w/b的方式分别是~linear.weight/linear.bias 15 | self.linear = torch.nn.Linear(1, 1) 16 | 17 | # 前馈 18 | def forward(self, x): 19 | y_pred = self.linear(x) 20 | return y_pred 21 | 22 | # 可以被直接调用 23 | model = LinearModel() 24 | 25 | 26 | # 3.构造损失函数和优化器 27 | # criterion = torch.nn.MSELoss(size_average = False) 28 | criterion = torch.nn.MSELoss(reduction = 'sum') # 使用MSE也可以自己设计 29 | optimizer = torch.optim.SGD(model.parameters(), lr = 0.01) # 优化器 / model.parameters()自动完成参数的初始化操作 30 | 31 | # 3.循环计算前馈 反向传播 更新权值 32 | for epoch in range(100): 33 | y_pred = model(x_data) # forward:predict 34 | loss = criterion(y_pred, y_data) # forward: loss 35 | print(epoch, loss.item()) 36 | 37 | optimizer.zero_grad() # 梯度会积累 所以及时清0 38 | loss.backward() # backward: autograd 自动计算梯度 39 | optimizer.step() # update 参数 即更新w和b的值 40 | 41 | print('w = ', model.linear.weight.item()) 42 | print('b = ', model.linear.bias.item()) 43 | 44 | x_test = torch.Tensor([[4.0]]) 45 | y_test = model(x_test) 46 | print('y_pred = ', y_test.data) -------------------------------------------------------------------------------- /class4使用pytorch实现线性回归/linear_nn2.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | x_data = torch.Tensor([[1.0],[2.0],[3.0]]) 4 | y_data = torch.Tensor([[2.0],[4.0],[6.0]]) 5 | 6 | class LinearModel(torch.nn.Module): 7 | def __init__(self): #构造函数 8 | super(LinearModel,self).__init__() 9 | self.linear = torch.nn.Linear(1,1) #构造对象,并说明输入输出的维数,第三个参数默认为true,表示用到b 10 | def forward(self, x): 11 | y_pred = self.linear(x) #可调用对象,计算y=wx+b 12 | return y_pred 13 | 14 | model = LinearModel() #实例化模型 15 | 16 | criterion = torch.nn.MSELoss(size_average=False) 17 | #model.parameters()会扫描module中的所有成员,如果成员中有相应权重,那么都会将结果加到要训练的参数集合上 18 | optimizer = torch.optim.SGD(model.parameters(),lr=0.01)#lr为学习率 19 | 20 | for epoch in range(1000): 21 | y_pred = model(x_data) 22 | loss = criterion(y_pred,y_data) 23 | print(epoch,loss.item()) 24 | 25 | optimizer.zero_grad() 26 | loss.backward() 27 | optimizer.step() 28 | 29 | print('w=',model.linear.weight.item()) 30 | print('b=',model.linear.bias.item()) 31 | 32 | x_test = torch.Tensor([[4.0]]) 33 | y_test = model(x_test) 34 | print('y_pred = ', y_test.data) 35 | -------------------------------------------------------------------------------- /class5逻辑斯蒂回归/LogisticRegression.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn.functional as F 3 | 4 | # prepare dataset 5 | x_data = torch.Tensor([[1.0], [2.0], [3.0]]) 6 | y_data = torch.Tensor([[0], [0], [1]]) 7 | 8 | #design model using class 9 | class LogisticRegressionModel(torch.nn.Module): 10 | def __init__(self): 11 | super(LogisticRegressionModel, self).__init__() 12 | self.linear = torch.nn.Linear(1,1) 13 | 14 | def forward(self, x): 15 | y_pred = F.sigmoid(self.linear(x)) 16 | # y_pred = torch.sigmoid(self.linear(x)) 17 | return y_pred 18 | 19 | model = LogisticRegressionModel() 20 | 21 | # construct loss and optimizer 22 | # 默认情况下,loss会基于element平均,如果size_average=False的话,loss会被累加 23 | # 二分类的交叉熵 24 | criterion = torch.nn.BCELoss(size_average = False) 25 | optimizer = torch.optim.SGD(model.parameters(), lr = 0.01) 26 | 27 | # training cycle forward, backward, update 28 | for epoch in range(1000): 29 | y_pred = model(x_data) 30 | loss = criterion(y_pred, y_data) 31 | print(epoch, loss.item()) 32 | 33 | optimizer.zero_grad() 34 | loss.backward() 35 | optimizer.step() 36 | 37 | print('w = ', model.linear.weight.item()) 38 | print('b = ', model.linear.bias.item()) 39 | 40 | x_test = torch.Tensor([[4.0]]) 41 | y_test = model(x_test) 42 | print('y_pred = ', y_test.data) -------------------------------------------------------------------------------- /class6多维特征处理/multiplyFeature.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import matplotlib.pyplot as plt 4 | 5 | # prepare dataset 6 | xy = np.loadtxt('../data/diabetes.csv', delimiter=',', dtype=np.float32) 7 | x_data = torch.from_numpy(xy[:, :-1]) # 取出前-1列 Feature 8 | y_data = torch.from_numpy(xy[:, [-1]]) # 取最后一列 label 9 | 10 | # design model using class 11 | 12 | 13 | class Model(torch.nn.Module): 14 | def __init__(self): 15 | super(Model, self).__init__() 16 | self.linear1 = torch.nn.Linear(8, 6) # 输入数据x的特征是8维,x有8个特征 17 | self.linear2 = torch.nn.Linear(6, 4) 18 | self.linear3 = torch.nn.Linear(4, 1) 19 | self.sigmoid = torch.nn.Sigmoid() # 将其看作是网络的一层,而不是简单的函数使用 20 | 21 | def forward(self, x): 22 | x = self.sigmoid(self.linear1(x)) 23 | x = self.sigmoid(self.linear2(x)) 24 | x = self.sigmoid(self.linear3(x)) # y hat 25 | return x 26 | 27 | 28 | model = Model() 29 | 30 | # construct loss and optimizer 31 | # criterion = torch.nn.BCELoss(size_average = True) 32 | criterion = torch.nn.BCELoss(reduction='mean') 33 | optimizer = torch.optim.SGD(model.parameters(), lr=0.1) 34 | 35 | epoch_list = [] 36 | loss_list = [] 37 | # training cycle forward, backward, update 38 | for epoch in range(100): 39 | y_pred = model(x_data) 40 | loss = criterion(y_pred, y_data) 41 | print(epoch, loss.item()) 42 | epoch_list.append(epoch) 43 | loss_list.append(loss.item()) 44 | 45 | optimizer.zero_grad() 46 | loss.backward() 47 | 48 | optimizer.step() 49 | 50 | 51 | plt.plot(epoch_list, loss_list) 52 | plt.ylabel('loss') 53 | plt.xlabel('epoch') 54 | plt.show() -------------------------------------------------------------------------------- /class7加载数据集/Loader.py: -------------------------------------------------------------------------------- 1 | # 数据加载 2 | import torch 3 | import numpy as np 4 | from torch.utils.data import Dataset 5 | from torch.utils.data import DataLoader 6 | 7 | # 准备数据集 8 | class DiabetesDataset(Dataset): # 抽象类DataSet 9 | def __init__(self, filepath): 10 | xy = np.loadtxt(filepath, delimiter=',', dtype=np.float32) 11 | self.len = xy.shape[0] # shape(多少行,多少列) 12 | self.x_data = torch.from_numpy(xy[:, :-1]) 13 | self.y_data = torch.from_numpy(xy[:, [-1]]) 14 | 15 | def __getitem__(self, index): 16 | return self.x_data[index], self.y_data[index] 17 | 18 | def __len__(self): 19 | return self.len 20 | 21 | # dataset对象 22 | dataset = DiabetesDataset('../data/diabetes.csv') 23 | 24 | # 使用DataLoader加载数据 25 | train_loader = DataLoader(dataset=dataset, # dataSet对象 26 | batch_size=32, # 每个batch的条数 27 | shuffle=True, # 是否打乱 28 | num_workers=4) # 多线程一般设置为4和8 29 | 30 | 31 | # design model using class 32 | class Model(torch.nn.Module): 33 | def __init__(self): 34 | super(Model, self).__init__() 35 | self.linear1 = torch.nn.Linear(8, 6) 36 | self.linear2 = torch.nn.Linear(6, 4) 37 | self.linear3 = torch.nn.Linear(4, 1) 38 | self.sigmoid = torch.nn.Sigmoid() 39 | 40 | def forward(self, x): 41 | x = self.sigmoid(self.linear1(x)) 42 | x = self.sigmoid(self.linear2(x)) 43 | x = self.sigmoid(self.linear3(x)) 44 | return x 45 | 46 | 47 | model = Model() 48 | 49 | # construct loss and optimizer 50 | criterion = torch.nn.BCELoss(reduction='mean') 51 | optimizer = torch.optim.SGD(model.parameters(), lr=0.01) 52 | 53 | # training cycle forward, backward, update 54 | if __name__ == '__main__': 55 | for epoch in range(100): 56 | for i, data in enumerate(train_loader, 0): # train_loader 是先shuffle后mini_batch 57 | inputs, labels = data # 取出一个batch 58 | y_pred = model(inputs) 59 | loss = criterion(y_pred, labels) 60 | print(epoch, i, loss.item()) 61 | 62 | optimizer.zero_grad() 63 | loss.backward() 64 | # 更新 65 | optimizer.step() -------------------------------------------------------------------------------- /class8多分类问题/multiply.py: -------------------------------------------------------------------------------- 1 | import torch 2 | # 用于数据集的加载 3 | from torchvision import transforms 4 | from torchvision import datasets 5 | from torch.utils.data import DataLoader 6 | # 使用激活函数relu 7 | import torch.nn.functional as F 8 | # 优化器 9 | import torch.optim as optim 10 | 11 | # 准备数据集 12 | batch_size = 64 13 | # 归一化,均值和方差 14 | transform = transforms.Compose([transforms.ToTensor(), # Convert the PIL Image to Tensor. 15 | transforms.Normalize((0.1307,), (0.3081,))]) 16 | 17 | train_dataset = datasets.MNIST(root='../data/mnist/', train=True, download=True, transform=transform) 18 | train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size) 19 | 20 | test_dataset = datasets.MNIST(root='../data/mnist/', train=False, download=True, transform=transform) 21 | test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size) 22 | 23 | # 设计模型类 24 | class Net(torch.nn.Module): 25 | def __init__(self): 26 | super(Net, self).__init__() 27 | # 5 层 28 | self.l1 = torch.nn.Linear(784, 512) 29 | self.l2 = torch.nn.Linear(512, 256) 30 | self.l3 = torch.nn.Linear(256, 128) 31 | self.l4 = torch.nn.Linear(128, 64) 32 | self.l5 = torch.nn.Linear(64, 10) 33 | 34 | # 前馈 35 | def forward(self, x): 36 | x = x.view(-1, 784) # -1其实就是自动获取mini_batch,使用wiew展开张量为向量 37 | x = F.relu(self.l1(x)) 38 | x = F.relu(self.l2(x)) 39 | x = F.relu(self.l3(x)) 40 | x = F.relu(self.l4(x)) 41 | return self.l5(x) # 最后一层不做激活,不进行非线性变换 42 | 43 | 44 | model = Net() 45 | 46 | # 构造损失函数和优化器 47 | criterion = torch.nn.CrossEntropyLoss() # 交叉熵 48 | optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) # SGD优化器 49 | 50 | # 训练 51 | # training cycle forward, backward, update 52 | def train(epoch): 53 | running_loss = 0.0 54 | for batch_idx, data in enumerate(train_loader, 0): 55 | inputs, target = data 56 | optimizer.zero_grad() 57 | 58 | outputs = model(inputs) 59 | loss = criterion(outputs, target) 60 | loss.backward() 61 | optimizer.step() 62 | 63 | # 每300个数据打印一次损失率 64 | running_loss += loss.item() 65 | if batch_idx % 300 == 299: 66 | print('[%d, %5d] loss: %.3f' % (epoch+1, batch_idx+1, running_loss/300)) 67 | running_loss = 0.0 68 | # 预测 69 | def test(): 70 | correct = 0 71 | total = 0 72 | with torch.no_grad(): 73 | for data in test_loader: 74 | images, labels = data 75 | # 预测 结果为一个batch长度×Feature个数的张量 76 | outputs = model(images) 77 | # torch.max的返回值有两个,第一个是每一行的最大值是多少,第二个是每一行最大值的下标(索引)是多少。 78 | _, predicted = torch.max(outputs.data, dim=1) # dim = 1 列是第0个维度,行是第1个维度 79 | total += labels.size(0) 80 | correct += (predicted == labels).sum().item() # 张量之间的比较运算 81 | print('accuracy on test set: %d %% ' % (100*correct/total)) 82 | 83 | 84 | if __name__ == '__main__': 85 | for epoch in range(10): 86 | train(epoch) 87 | test() -------------------------------------------------------------------------------- /class9卷积神经网络基础/MINST.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torchvision import transforms 3 | from torchvision import datasets 4 | from torch.utils.data import DataLoader 5 | import torch.nn.functional as F 6 | import torch.optim as optim 7 | 8 | # prepare dataset 9 | 10 | batch_size = 64 11 | transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) 12 | 13 | train_dataset = datasets.MNIST(root='../data/mnist/', train=True, download=False, transform=transform) 14 | train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size) 15 | test_dataset = datasets.MNIST(root='../dats/mnist/', train=False, download=False, transform=transform) 16 | test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size) 17 | 18 | # design model using class 19 | class Net(torch.nn.Module): 20 | def __init__(self): 21 | super(Net, self).__init__() 22 | self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5) 23 | self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=5) 24 | self.pooling = torch.nn.MaxPool2d(2) 25 | self.fc = torch.nn.Linear(320, 10) 26 | 27 | 28 | def forward(self, x): 29 | # flatten data from (n,1,28,28) to (n, 784) 30 | batch_size = x.size(0) 31 | x = F.relu(self.pooling(self.conv1(x))) 32 | x = F.relu(self.pooling(self.conv2(x))) 33 | x = x.view(batch_size, -1) # -1 此处自动算出的是320 34 | x = self.fc(x) 35 | return x 36 | 37 | 38 | model = Net() 39 | 40 | # construct loss and optimizer 41 | criterion = torch.nn.CrossEntropyLoss() 42 | optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) 43 | 44 | # training cycle forward, backward, update 45 | 46 | 47 | def train(epoch): 48 | running_loss = 0.0 49 | for batch_idx, data in enumerate(train_loader, 0): 50 | inputs, target = data 51 | optimizer.zero_grad() 52 | 53 | outputs = model(inputs) 54 | loss = criterion(outputs, target) 55 | loss.backward() 56 | optimizer.step() 57 | 58 | running_loss += loss.item() 59 | if batch_idx % 300 == 299: 60 | print('[%d, %5d] loss: %.3f' % (epoch+1, batch_idx+1, running_loss/300)) 61 | running_loss = 0.0 62 | 63 | 64 | def test(): 65 | correct = 0 66 | total = 0 67 | with torch.no_grad(): 68 | for data in test_loader: 69 | images, labels = data 70 | outputs = model(images) 71 | _, predicted = torch.max(outputs.data, dim=1) 72 | total += labels.size(0) 73 | correct += (predicted == labels).sum().item() 74 | print('accuracy on test set: %d %% ' % (100*correct/total)) 75 | 76 | 77 | if __name__ == '__main__': 78 | for epoch in range(10): 79 | train(epoch) 80 | test() -------------------------------------------------------------------------------- /class9卷积神经网络基础/MINS_GPU.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torchvision import transforms 3 | from torchvision import datasets 4 | from torch.utils.data import DataLoader 5 | import torch.nn.functional as F 6 | import torch.optim as optim 7 | import matplotlib.pyplot as plt 8 | 9 | # prepare dataset 10 | 11 | batch_size = 64 12 | transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) 13 | 14 | train_dataset = datasets.MNIST(root='../dataset/mnist/', train=True, download=False, transform=transform) 15 | train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size) 16 | test_dataset = datasets.MNIST(root='../dataset/mnist/', train=False, download=False, transform=transform) 17 | test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size) 18 | 19 | # design model using class 20 | 21 | 22 | class Net(torch.nn.Module): 23 | def __init__(self): 24 | super(Net, self).__init__() 25 | self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5) 26 | self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=5) 27 | self.pooling = torch.nn.MaxPool2d(2) 28 | self.fc = torch.nn.Linear(320, 10) 29 | 30 | 31 | def forward(self, x): 32 | # flatten data from (n,1,28,28) to (n, 784) 33 | 34 | batch_size = x.size(0) 35 | x = F.relu(self.pooling(self.conv1(x))) 36 | x = F.relu(self.pooling(self.conv2(x))) 37 | x = x.view(batch_size, -1) # -1 此处自动算出的是320 38 | # print("x.shape",x.shape) 39 | x = self.fc(x) 40 | 41 | return x 42 | 43 | 44 | model = Net() 45 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 46 | model.to(device) 47 | 48 | # construct loss and optimizer 49 | criterion = torch.nn.CrossEntropyLoss() 50 | optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) 51 | 52 | # training cycle forward, backward, update 53 | 54 | 55 | def train(epoch): 56 | running_loss = 0.0 57 | for batch_idx, data in enumerate(train_loader, 0): 58 | inputs, target = data 59 | inputs, target = inputs.to(device), target.to(device) 60 | optimizer.zero_grad() 61 | 62 | outputs = model(inputs) 63 | loss = criterion(outputs, target) 64 | loss.backward() 65 | optimizer.step() 66 | 67 | running_loss += loss.item() 68 | if batch_idx % 300 == 299: 69 | print('[%d, %5d] loss: %.3f' % (epoch+1, batch_idx+1, running_loss/300)) 70 | running_loss = 0.0 71 | 72 | 73 | def test(): 74 | correct = 0 75 | total = 0 76 | with torch.no_grad(): 77 | for data in test_loader: 78 | images, labels = data 79 | images, labels = images.to(device), labels.to(device) 80 | outputs = model(images) 81 | _, predicted = torch.max(outputs.data, dim=1) 82 | total += labels.size(0) 83 | correct += (predicted == labels).sum().item() 84 | print('accuracy on test set: %d %% ' % (100*correct/total)) 85 | return correct/total 86 | 87 | 88 | if __name__ == '__main__': 89 | epoch_list = [] 90 | acc_list = [] 91 | 92 | for epoch in range(10): 93 | train(epoch) 94 | acc = test() 95 | epoch_list.append(epoch) 96 | acc_list.append(acc) 97 | 98 | plt.plot(epoch_list,acc_list) 99 | plt.ylabel('accuracy') 100 | plt.xlabel('epoch') 101 | plt.show() 102 | 103 | -------------------------------------------------------------------------------- /class9卷积神经网络基础/conv.py: -------------------------------------------------------------------------------- 1 | import torch 2 | in_channels, out_channels= 5, 10 # 输入输出通道 3 | width, height = 100, 100 # 输入图像的宽高 4 | kernel_size = 3 # 卷积核的大小 5 | batch_size = 1 # batch的个数 6 | 7 | input = torch.randn(batch_size, # 随机生成一个张量输入,模拟图像 8 | in_channels, # input = (1,5,100,100) 9 | width, # 对输入没有要求,除了in_channels必须要为5 否则卷积层不起作用 10 | height) 11 | 12 | # 定义卷积 13 | conv_layer = torch.nn.Conv2d( in_channels, # 输入通道 14 | out_channels, # 输出通道 15 | kernel_size=kernel_size)# 卷积核 16 | 17 | output = conv_layer(input) # 做卷积 18 | 19 | print(input.shape) 20 | print(output.shape) 21 | print(conv_layer.weight.shape) 22 | -------------------------------------------------------------------------------- /class9卷积神经网络基础/maxpoling.py: -------------------------------------------------------------------------------- 1 | import torch 2 | input = [3,4,6,5, 3 | 2,4,6,8, 4 | 1,6,7,8, 5 | 9,7,4,6, 6 | ] 7 | input = torch.Tensor(input).view(1, 1, 4, 4) 8 | maxpooling_layer = torch.nn.MaxPool2d(kernel_size=2) 9 | output = maxpooling_layer(input) 10 | print(output) 11 | -------------------------------------------------------------------------------- /class9卷积神经网络基础/padding.py: -------------------------------------------------------------------------------- 1 | import torch 2 | input = [3,4,6,5,7, 3 | 2,4,6,8,2, 4 | 1,6,7,8,4, 5 | 9,7,4,6,2, 6 | 3,7,5,4,1] 7 | 8 | # 使用view固定为一个张量B×C×W×H 9 | input = torch.Tensor(input).view(1, 1, 5, 5) 10 | 11 | # 定义卷积 12 | conv_layer = torch.nn.Conv2d(1, 1, kernel_size=3, padding=1, bias=False) 13 | 14 | # 将一个向量转化为张量赋值给kernel卷积核 15 | kernel = torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1, 1, 3, 3) 16 | 17 | # 卷积权重的初始化 18 | conv_layer.weight.data = kernel.data 19 | 20 | # 卷积 21 | output = conv_layer(input) 22 | print(output) 23 | -------------------------------------------------------------------------------- /class9卷积神经网络基础/stride.py: -------------------------------------------------------------------------------- 1 | import torch 2 | input = [3,4,6,5,7, 3 | 2,4,6,8,2, 4 | 1,6,7,8,4, 5 | 9,7,4,6,2, 6 | 3,7,5,4,1] 7 | 8 | # 使用view固定为一个张量B×C×W×H 9 | input = torch.Tensor(input).view(1, 1, 5, 5) 10 | 11 | # 定义卷积 12 | conv_layer = torch.nn.Conv2d(1, 1, kernel_size=3, stride=2, padding=1, bias=False) 13 | 14 | # 将一个向量转化为张量赋值给kernel卷积核 15 | kernel = torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1, 1, 3, 3) 16 | 17 | # 卷积权重的初始化 18 | conv_layer.weight.data = kernel.data 19 | 20 | # 卷积 21 | output = conv_layer(input) 22 | print(output) 23 | -------------------------------------------------------------------------------- /data/diabetes.csv.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/diabetes.csv.gz -------------------------------------------------------------------------------- /data/mnist/MNIST/processed/test.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/processed/test.pt -------------------------------------------------------------------------------- /data/mnist/MNIST/processed/training.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/processed/training.pt -------------------------------------------------------------------------------- /data/mnist/MNIST/raw/t10k-images-idx3-ubyte: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/raw/t10k-images-idx3-ubyte -------------------------------------------------------------------------------- /data/mnist/MNIST/raw/t10k-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/raw/t10k-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /data/mnist/MNIST/raw/t10k-labels-idx1-ubyte: -------------------------------------------------------------------------------- 1 | '                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             -------------------------------------------------------------------------------- /data/mnist/MNIST/raw/t10k-labels-idx1-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/raw/t10k-labels-idx1-ubyte.gz -------------------------------------------------------------------------------- /data/mnist/MNIST/raw/train-images-idx3-ubyte: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/raw/train-images-idx3-ubyte -------------------------------------------------------------------------------- /data/mnist/MNIST/raw/train-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/raw/train-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /data/mnist/MNIST/raw/train-labels-idx1-ubyte: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/raw/train-labels-idx1-ubyte -------------------------------------------------------------------------------- /data/mnist/MNIST/raw/train-labels-idx1-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/mnist/MNIST/raw/train-labels-idx1-ubyte.gz -------------------------------------------------------------------------------- /data/names_test.csv.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/names_test.csv.gz -------------------------------------------------------------------------------- /data/names_train.csv.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/data/names_train.csv.gz -------------------------------------------------------------------------------- /dataset/mnist/MNIST/raw/train-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dataset/mnist/MNIST/raw/train-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /dats/mnist/MNIST/processed/test.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/processed/test.pt -------------------------------------------------------------------------------- /dats/mnist/MNIST/processed/training.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/processed/training.pt -------------------------------------------------------------------------------- /dats/mnist/MNIST/raw/t10k-images-idx3-ubyte: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/raw/t10k-images-idx3-ubyte -------------------------------------------------------------------------------- /dats/mnist/MNIST/raw/t10k-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/raw/t10k-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /dats/mnist/MNIST/raw/t10k-labels-idx1-ubyte: -------------------------------------------------------------------------------- 1 | '                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             -------------------------------------------------------------------------------- /dats/mnist/MNIST/raw/t10k-labels-idx1-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/raw/t10k-labels-idx1-ubyte.gz -------------------------------------------------------------------------------- /dats/mnist/MNIST/raw/train-images-idx3-ubyte: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/raw/train-images-idx3-ubyte -------------------------------------------------------------------------------- /dats/mnist/MNIST/raw/train-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/raw/train-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /dats/mnist/MNIST/raw/train-labels-idx1-ubyte: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/raw/train-labels-idx1-ubyte -------------------------------------------------------------------------------- /dats/mnist/MNIST/raw/train-labels-idx1-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/dats/mnist/MNIST/raw/train-labels-idx1-ubyte.gz -------------------------------------------------------------------------------- /课件/Lecture_01_Overview.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_01_Overview.pdf -------------------------------------------------------------------------------- /课件/Lecture_02_Linear_Model.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_02_Linear_Model.pdf -------------------------------------------------------------------------------- /课件/Lecture_03_Gradient_Descent.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_03_Gradient_Descent.pdf -------------------------------------------------------------------------------- /课件/Lecture_04_Back_Propagation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_04_Back_Propagation.pdf -------------------------------------------------------------------------------- /课件/Lecture_05_Linear_Regression_with_PyTorch.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_05_Linear_Regression_with_PyTorch.pdf -------------------------------------------------------------------------------- /课件/Lecture_06_Logistic_Regression.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_06_Logistic_Regression.pdf -------------------------------------------------------------------------------- /课件/Lecture_07_Multiple_Dimension_Input.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_07_Multiple_Dimension_Input.pdf -------------------------------------------------------------------------------- /课件/Lecture_08_Dataset_and_Dataloader.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_08_Dataset_and_Dataloader.pdf -------------------------------------------------------------------------------- /课件/Lecture_09_Softmax_Classifier.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_09_Softmax_Classifier.pdf -------------------------------------------------------------------------------- /课件/Lecture_10_Basic_CNN.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_10_Basic_CNN.pdf -------------------------------------------------------------------------------- /课件/Lecture_11_Advanced_CNN.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_11_Advanced_CNN.pdf -------------------------------------------------------------------------------- /课件/Lecture_12_Basic_RNN.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_12_Basic_RNN.pdf -------------------------------------------------------------------------------- /课件/Lecture_13_RNN_Classifier.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/Lecture_13_RNN_Classifier.pdf -------------------------------------------------------------------------------- /课件/diabetes.csv.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/diabetes.csv.gz -------------------------------------------------------------------------------- /课件/names_test.csv.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/names_test.csv.gz -------------------------------------------------------------------------------- /课件/names_train.csv.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DelinQu/pytorch-prev/e60ec5f055cb1beb914a48314f800e09a827564f/课件/names_train.csv.gz --------------------------------------------------------------------------------