生活随笔
收集整理的這篇文章主要介紹了
李宏毅机器学习Homework1(代码简洁版)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
李宏毅機器學習Homework1
- 題意大概是(我具體沒怎么聽,覺得有問題的小伙伴可以提醒我一句),給你前四天的數據,根據第5天的身體狀況預測第五天的test_positive,具體數據b站有,懶得放了
- 因為電腦沒有GPU所以我懶得管那些GPU的設置了,然后代碼主要是訓練模塊,測試模塊實際上就是把網絡設置成推理模式,然后自己跑,很簡單的我就不寫了
- 這個問題單純是練手的,我也沒看老師的代碼是怎么做的,反正訓練效果我感覺差不多就行了,就不多說廢話,調整超參數那些自己可以嘗試搞一搞
- 最后大家感興趣的話可以關注我的個人公眾號右轉的第二排架子,里面(將)有相關內容
import torch
import torch
.nn
as nn
import torch
.nn
.functional
as F
import torch
.optim
as optim
import csv
import numpy
as np
import matplotlib
.pyplot
as plttrain_path
= 'covid.train.csv'
test_path
= 'covid.test.csv'class Net(nn
.Module
):def __init__(self
, input_dim
, output_dim
):super(Net
, self
).__init__
()self
.fc1
= nn
.Linear
(input_dim
, 100)self
.fc2
= nn
.Linear
(100, 50)self
.fc3
= nn
.Linear
(50, 30)self
.fc4
= nn
.Linear
(30, output_dim
)def forward(self
, state
):output
= F
.relu
((self
.fc1
(state
)))output
= F
.relu
((self
.fc2
(output
)))output
= F
.relu
((self
.fc3
(output
)))output
= self
.fc4
(output
)return output
class Covid19_Dataset:def __init__(self
, train_path
, test_path
):self
.train_path
= train_pathself
.test_path
= test_path_
, self
.train_data
= self
.load_data
(train_path
)_
, self
.test_data
= self
.load_data
(test_path
) '''*******************************************************************id : line 1location : line 2 - 38day1 : line 39 - 54day2 : line 55 - 70day3 : line 71 - 86day4 : line 87 - 102if train_data:day5 : line 103 - 118if test_data: id + 116day5 : line 103 - 117 (need to predict 'tested_positve' according to relevant parameters)*******************************************************************'''def load_data(self
, filepath
):with open(filepath
, 'r') as f
:reader
= csv
.reader
(f
)index
= next(reader
)data
= np
.array
([*reader
]).astype
(np
.float64
)return index
, data
def sample_data(self
, Batch_size
, data
):index
= np
.random
.choice
(len(data
), size
= Batch_size
, replace
= False) sample_data
= []for sample_index
in index
:sample_data
.append
(data
[sample_index
])return np
.array
(sample_data
)if __name__
== '__main__':Batch_size
= 16input_dim
= 116output_dim
= 1learning_rate
= 0.001net
= Net
(input_dim
, output_dim
)dataset
= Covid19_Dataset
(train_path
, test_path
)criterion
= nn
.MSELoss
(reduction
= 'mean')optimizer
= optim
.Adam
(net
.parameters
(), lr
= learning_rate
)Episode
= np
.arange
(1, 1000)Episode_loss
= []for epoch
in Episode
:sample_train_data
= dataset
.sample_data
(Batch_size
, dataset
.train_data
)sample_input_data
= torch
.FloatTensor
(sample_train_data
[:, 1:117])sample_label_data
= torch
.FloatTensor
(sample_train_data
[:, -1]).unsqueeze
(1)net
.eval()predict_data
= net
(sample_input_data
)net
.train
()loss
= criterion
(predict_data
, sample_label_data
)Episode_loss
.append
(loss
.item
())optimizer
.zero_grad
()loss
.backward
()optimizer
.step
()Episode_loss
= np
.array
(Episode_loss
)plt
.plot
(Episode
, Episode_loss
)plt
.show
()
總結
以上是生活随笔為你收集整理的李宏毅机器学习Homework1(代码简洁版)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。