Task1:线性回归;Softmax与分类模型;多层感知机
2020-03-05 18:35
260 查看
1.线性回归(用来预测连续值) 计算损失函数平均值
#q1: += 前面的变量要提前定义好 #q2: #注释 #定义损失函数: import torch def squared_loss(y_hat, y): return (y_hat - y.view(y_hat.size())) ** 2 / 2 y_h = torch.tensor([2.33,1.07,1.23]) y = torch.tensor([3.14,0.98,1.32]) result = 0 #下面 += 需要事先定义变量 result += squared_loss(y_h,y) 'mean loss: %.3f' % result.mean()2.softmax(也是线性单层模型,用来预测离散值,如图像分类)
softmax([100, 101, 102])
q1: .exp()不能处理LONG类型,要转换为flot(可以通过3.表示浮点型) import torch def softmax(X): X_exp = X.exp() partition = X_exp.sum(dim=1, keepdim=True) # print("X size is ", X_exp.size()) # print("partition size is ", partition, partition.size()) return X_exp / partition # 这里应用了广播机制 x = torch.tensor([[1.,2,2],[2,3,5]],dtype=torch.float64) res = softmax(x) print(res)3.多层感知机(含隐藏层,但是联立后依旧等价于单层神经网络)
激活函数(全连接层间引入的非线性变换),ReLU较为常用;Sigmoid和tanh可以缩小数值的区间,但是可能会引起梯度消失,慎用!
下面是常用的激活函数:
ReLU: 小于0部分清零,大于0部分不变
Sigmoid: 变换到 0~1 之间
tanh(双曲正切): 变换到 -1~1 之间
#q1:tensor变量有什么函数,如x.type,x.shape[1](取一列),x.view()(resize形状) #q2:print函数如何同时输出注释和变量 print('a:', a) #定义模型参数: import torch pixel = 256*256 print(pixel) num_inputs, num_outputs, num_hiddens = pixel, 10, 1000 W1 = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_hiddens)), dtype=torch.float) b1 = torch.zeros(num_hiddens, dtype=torch.float) W2 = torch.tensor(np.random.normal(0, 0.01, (num_hiddens, num_outputs)), dtype=torch.float) b2 = torch.zeros(num_outputs, dtype=torch.float) print("type:") print(len(W1), type(W1), W1.type, W1.size()) print("all weight number:") print((W1.size()[0]*W1.size()[1]+W2.size()[0]*W2.size()[1])) params = [W1, b1, W2, b2] for param in params: param.requires_grad_(requires_grad=True)
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 《动手学深度学习》笔记 Task01 线性回归;Softmax与分类模型、多层感知机
- 动手学深度学习(PyTorch)task01:线性回归;Softmax与分类模型、多层感知机
- Task1.0 学习笔记线性回归;Softmax与分类模型、多层感知机
- ElitesAI·动手学深度学习PyTorch版学习笔记-线性回归;Softmax与分类模型、多层感知机
- 《动手学深度学习》笔记 Task01:线性回归;Softmax与分类模型、多层感知机
- 动手学深度学习-线性回归;Softmax与分类模型;多层感知机
- task01:线性回归;Softmax与分类模型;多层感知机;(主要回顾下概念)
- 动手学深度学习PyTorch版 | (1)线性回归、softmax与分类、多层感知机
- 动手学深度学习 线性回归-Softmax与分类模型-多层感知机
- 深度学习softmax与多层感知机分类模型
- 线性回归、Softmax与分类模型、多层感知机
- 机器学习(二)广义线性模型:逻辑回归与Softmax分类
- Keras 多层感知机 多类别的 softmax 分类模型代码
- 初学ML笔记N0.1——线性回归,分类与逻辑斯蒂回归,通用线性模型
- 分类的线性模型:概率判别式模型之逻辑回归LR
- 分类算法(一):广义线性模型之线性回归、逻辑回归
- 初学ML笔记N0.1——线性回归,分类与逻辑斯蒂回归,通用线性模型
- 动手学深度学习(线性回归+softmax与分类模型+多层感知机)
- 初学ML笔记N0.1——线性回归,分类与逻辑斯蒂回归,通用线性模型
- python实现感知机线性分类模型示例代码