Day22-pytorch(5)简单示范regression模型

先import各种会用到的套件
https://ithelp.ithome.com.tw/upload/images/20210904/20140416ExUeliHPAo.png

使用sklearn的datasets套件建立要regression的资料集
https://ithelp.ithome.com.tw/upload/images/20210904/20140416KikrDlEINW.png

pytorch做regression时,y资料的dimension需改成2,shape为(资料个数,1)
使用reshape来改变成想要的shape
https://ithelp.ithome.com.tw/upload/images/20210904/20140416K6z5uKSGjR.png

对特徵值做正规画以利训练model
https://ithelp.ithome.com.tw/upload/images/20210904/20140416D2dgM0QEom.png

须将资料型态转为tensor,不然pytorch写出来的模型不会里你
https://ithelp.ithome.com.tw/upload/images/20210904/20140416aQc7DnkuQJ.png

建立Dataset
https://ithelp.ithome.com.tw/upload/images/20210904/20140416Su2t50h2P7.png

建立DataLoader
https://ithelp.ithome.com.tw/upload/images/20210904/20140416Xf3jYxeGQP.png

建立model
https://ithelp.ithome.com.tw/upload/images/20210904/20140416RPU4Dxx5EZ.png

设置loss function、optimizer、epoch、n_batch
critirion就是loss function,使用MSE
optimizer使用adam
epoch设置资料要训练几次
n_batch为dataloader的长度,也就是每一个epoch里面能分成几个组合
我们在dataloader时设置batch_size为100
资料集总共有1000个,所以n_batch等於1000/100=10
https://ithelp.ithome.com.tw/upload/images/20210904/20140416ulqe6HtWXA.png

开始训练
最外面的回圈range为epoch,表示会根据epoch设计的次数来训练资料
里面的回圈使用了enumerate後方参数放入data_loader,因batch_size设为100
j会从0~99(batch_size-1)
samples会获得100(batch_size)笔特徵值资料,也就是x_data
labels会获得100(batch_size)笔目标值资料,也就是y_data
https://ithelp.ithome.com.tw/upload/images/20210904/20140416903J3SBk9j.png
print出的训练过程
https://ithelp.ithome.com.tw/upload/images/20210904/20140416Qof4prdkyN.png

送上colab连结,可自行在上面多做点练习更加熟悉pytorch
https://colab.research.google.com/drive/14fN4RhEF-jIjkjuifJDYyaVLgKYRKyuV?usp=sharing


<<:  Day-09 Logistic Regression 实作(修正版)

>>:  Day10_HTML语法7

资安学习路上-网站常见漏洞与 Injection的爱恨情仇1

讲师 : 社长 林子婷(飞飞) 时间 : 19:00-21:00(授课时间共2小时) 授课内容:网...

30天学习笔记 介绍-day 25-View Animation

View Animation可以对view做透明度、缩放、平移、旋转等动画。 xml动画文件需再re...

[Day24]创建Table及捞取资料

创建migration迁移档案 首先先使用artisan指令: make:migration 创建一...

[Day2]PHP的资料型态02

PHP的资料型态 Array数组 PHP中的array 实际上是一个有序映射。映射是一种把 valu...

[Android Studio 30天自我挑战] TableLayout元件对齐方式

TableLayout为表格布局,顾名思义就是利用表格方式来布局 TableLayout里面是透过T...