- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
02_ Linear Model
展开查看详情
1 .
2 .
3 .
4 .
5 .
6 .
7 .
8 .
9 .
10 .ML/DL for Everyone with Lecture 2: Linear Model Sung Kim < hunkim+ml@gmail.com > HKUST Code: https://github.com/hunkim/PyTorchZeroToAll Slides: http://bit.ly/PyTorchZeroAll Videos: http://bit.ly/PyTorchVideo
11 .Linear import numpy as np import matplotlib.pyplot as plt x_data = [ 1.0 , 2.0 , 3.0 ] y_data = [ 2.0 , 4.0 , 6.0 ] w = 1.0 # a random guess: random value, 1.0 # our model for the forward pass def forward(x): return x * w # Loss function def loss(x, y): y_pred = forward(x) return (y_pred - y) * (y_pred - y) w_list = [] mse_list = [] for w in np.arange( 0.0 , 4.1 , 0.1 ): print ( "w=" , w) l_sum = 0 for x_val, y_val in zip (x_data, y_data): y_pred_val = forward(x_val) l = loss(x_val, y_val) l_sum += l print ( " " , x_val, y_val, y_pred_val, l) print ( "MSE=" , l_sum / 3 ) w_list.append(w) mse_list.append(l_sum / 3 ) plt.plot(w_list, mse_list) plt.ylabel( Loss ) plt.xlabel( w ) plt.show()
12 .Model & Loss w = 1.0 # a random guess: random value # our model for the forward pass def forward(x): return x * w # Loss function def loss(x, y): y_pred = forward(x) return (y_pred - y) * (y_pred - y)
13 .Hours (x) Points (y) 1 2 2 4 3 6 Linear Regression error? * The machine starts with a random guess , w=random value
14 .Training Loss (error) Hours, x Loss (w=0) Loss (w=1) Loss (w=2) Loss (w=3) Loss (w=4) 1 4 1 0 1 4 2 16 4 0 4 16 3 36 9 0 9 36 MSE=56/3=18.7 MSE=14/3=4.7 MSE=0 MSE=14/3=4.7 MSE=56/3=18.7 MSE, mean square error
15 .Training Loss (error) Hours, x Points, y Prediction, y^(w=0) Loss (w=0) 1 2 0 4 2 4 0 16 3 6 0 36 mean=56/3
16 .Model design What would be the best model for the data? Linear? Hours (x) Points (y) 1 2 2 4 3 6 4 ? Linear