- 任务1 PyTorch张量计算与Numpy的转换
- 任务2 PyTorch梯度计算和梯度下降过程
- 任务3 PyTorch全连接层原理和使用
- 任务4 PyTorchPyTorch激活函数原理和使用
- 任务5 PyTorch卷积层原理和使用
- 任务6 PyTorch常见的损失函数和优化器使用
- 任务7 PyTorch池化层和归一化层
- 步骤1:配置本地Notebook环境,或使用天池DSW:https://dsw-dev.data.aliyun.com/#/
- 步骤2:学习Pytorch的基础语法,并成功执行以下代码
- 基础pytorch教程:https://zhuanlan.zhihu.com/p/25572330
- 官方教程:https://pytorch.org/tutorials/beginner/basics/intro.html
代码:
c = np.ones((3,3)) d = torch.from_numpy(c) d.size() d
结果:
- 步骤1:学习自动求梯度原理,https://pytorch.org/tutorials/beginner/basics/autogradqs_tutorial.html
- 步骤2:学习随机梯度下降原理,https://www.cnblogs.com/BYRans/p/4700202.html
- 步骤3:
- 使用numpy创建一个y=10*x+4+noise(0,1)的数据,其中x是0到100的范围,以0.01进行等差数列
- 使用pytroch定义w和b,并使用随机梯度下降,完成回归拟合。
源码:
X = np.linspace(start=0, stop=100, num=10000) y = 3*X + 4 + np.random.uniform(0, 1, size=(10000,)) X = torch.from_numpy(X) X = X.to(torch.float32) y = torch.from_numpy(y) y = y.to(torch.float32) w = Variable(torch.randn(1), requires_grad=True) b = Variable(torch.randn(1), requires_grad=True) prediction = w*X + b plt.scatter(X.data.numpy(), y.data.numpy()) plt.plot(X.data.numpy(), prediction.data.numpy(), 'r-', lw=2) plt.show() for epoch in range(500): for i, (trn_x, trn_y) in enumerate(zip(X, y)): z = w * trn_x + b loss = torch.nn.functional.mse_loss(z, trn_y) loss.backward() w.data -= 1e-5*w.grad.data b.data -= 1e-5*b.grad.data l1 = w.grad.data.zero_() l2 = b.grad.data.zero_() if (epoch+1)%50 == 0: print(loss) prediction = w*X + b plt.scatter(X.data.numpy(), y.data.numpy()) plt.plot(X.data.numpy(), prediction.data.numpy(), 'r-', lw=2) plt.show()
结果,从上往下分别是:拟合前,训练过程和拟合后:
- 步骤1:学习全连接网络原理,https://blog.csdn.net/xiaodong_11/article/details/82015456
- 步骤2:在pytorch中使用矩阵乘法实现全连接层
- 步骤3:在pytorch中使用nn.Linear层
- 步骤1:学习激活函数的原理,https://zhuanlan.zhihu.com/p/https://zhuanlan.zhihu.com/p/8842993488429934
- 步骤2:在pytorch中手动实现上述激活函数
实现源码:
def elu(x, alpha=1.0, inplace=False): # ELU(x)=max(0,x) + min(0,α∗(exp(x)−1)) zeros = torch.zeros_like(x) left = torch.cat([x, zeros], dim=0) right = alpha * (torch.exp(x) - 1) right = torch.cat([zeros, right]) result = torch.max(left, dim=0)[0] + torch.min(right, dim=0)[0] return result def leaky_relu(x, negative_slope=0.01,inplace=False): # LeakyReLU(x) = max(0,x) + negative_slope∗min(0,x) zeros = torch.zeros_like(x) cat_mat = torch.cat([x, zeros], dim=0) result = torch.max(cat_mat, dim=0)[0] + negative_slope*torch.min(cat_mat, dim=0)[0] return result def p_relu(x, num_parameters=1, init=0.25): # PReLU(x)=max(0,x)+a∗min(0,x) zeros = torch.zeros_like(x) cat_mat = torch.cat([x, zeros], dim=0) result = torch.max(cat_mat, dim=0)[0] + init*torch.min(cat_mat, dim=0)[0] return result def relu(x, inplace=False): # ReLU(x)=max(0,x) zeros = torch.zeros_like(x) cat_mat = torch.cat([x, zeros], dim=0) result = torch.max(cat_mat, dim=0)[0] return result def relu6(x, inplace=False): # ReLU6(x)=min(max(0,x),6) mat_zero = torch.zeros_like(x) x = torch.cat([x, mat_zero], dim=0) result = torch.max(x, dim=0)[0] result = torch.cat([torch.unsqueeze(result, dim=0), mat_zero+6], dim=0) result = torch.min(result, dim=0)[0] return result def selu(x, inplace=True): # SELU(x) = scale∗(max(0, x) + min(0, α∗(exp(x)−1))) alpha = 1.6732632423543772848170429916717 scale = 1.0507009873554804934193349852946 zeros = torch.zeros_like(x) left = torch.cat([x, zeros], dim=0) right = alpha * (torch.exp(x) - 1) right = torch.cat([zeros, right]) result = torch.max(left, dim=0)[0] + torch.min(right, dim=0)[0] result *= scale return result def celu(x, alpha=1.0,inplace=False): if not alpha: raise Exception(f"{alpha}不能为0") # CELU(x)=max(0,x)+min(0,α∗(exp(x/α)−1)) zeros = torch.zeros_like(x) left = torch.cat([x, zeros], dim=0) right = alpha * (torch.exp(x / alpha) - 1) right = torch.cat([zeros, right]) result = torch.max(left, dim=0)[0] + torch.min(right, dim=0)[0] return result def sigmoid(x): # Sigmoid(x) = 1 / (1 + exp(-x)) return 1 / (1 + torch.exp(-1 * x)) def log_sigmoid(x): # LogSigmoid(x) = log(1 / (1 + exp(-x))) return torch.log(sigmoid(x)) def tanh(x): # Tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)) # solution 1: # return torch.tanh(x) # solution 2: return (torch.exp(x) - torch.exp(-x)) / (torch.exp(x) + torch.exp(-x)) def tanh_shrink(x): return x - tanh(x) def soft_plus(x, beta=1,threshold=20): # if not beta: raise return 1/beta*torch.log(1+torch.exp(beta*x)) def soft_shrink(x: Tensor, lambd=0.5): x.apply_(lambda x_: x_-lambd if x_ > lambd else x_+lambd if x_ < -lambd else 0) return x任务5 PyTorch卷积层原理和使用
-
步骤1:理解卷积层的原理和具体使用
- https://blog.csdn.net/qq_37385726/article/details/81739179
- https://www.cnblogs.com/zhangxiann/p/13584415.html
-
步骤2:计算下如下卷积层的参数量
- 步骤1:学习损失函数的细节,https://www.cnblogs.com/wanghui-garcia/p/10862733.html
- 步骤2:学习优化器的使用,https://pytorch.org/docs/stable/optim.html
- 步骤3:设置不同的优化器和学习率,重复任务2的回归过程
- 损失函数MSE、优化器SGD、学习率0.1
- 损失函数MSE、优化器SGD、学习率0.5
- 损失函数MSE、优化器SGD、学习率0.01
- 步骤1:使用pytroch代码实现2d pool中的mean-pooling、max-pooling
- https://pytorch.org/docs/stable/nn.html#pooling-layers
- https://blog.csdn.net/shanglianlm/article/details/85313924
- 步骤2:学习归一化的原理,https://blog.csdn.net/qq_23981335/article/details/106572171
未完待续。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)