import torch
import numpy as np
from torch.utils.data import dataset
from torch.utils.data import dataloader
import matplotlib.pyplot as plt
# prepare dataset
class
diabetesdataset
(dataset)
: def __init__
(self, filepath)
: xy = np.
loadtxt
(filepath, delimiter=
' ', dtype=np.float32)
self.len = xy.shape[
0] # shape
(多少行,多少列)
self.x_data = torch.
from_numpy
(xy[:,
:-1]
) self.y_data = torch.
from_numpy
(xy[:,
[-1]
])def __getitem__
(self, index)
:return self.x_data[index]
, self.y_data[index]
def __len__
(self)
:return self.len
data_set =
diabetesdataset
('000.txt'
)train_loader =
dataloader
(dataset=data_set, batch_size=
32, shuffle=true)
# data_set 要載入的資料
# batch_size 表示每個mini-bach含樣本的個數
# shuffle=true 表示要打亂樣本順序
# num_workers 多執行緒
# design model using class
class
model
(torch.nn.module)
: def __init__
(self)
:super
(model, self)
.__init__()
self.linear1 = torch.nn.
linear(9
,6) self.linear2 = torch.nn.
linear(6
,4) self.linear3 = torch.nn.
linear(4
,1) self.sigmoid = torch.nn.
sigmoid()
def forward
(self, x)
: x = self.
sigmoid
(self.
linear1
(x))
x = self.
sigmoid
(self.
linear2
(x))
x = self.
sigmoid
(self.
linear3
(x))
return x
model =
model()
# construct loss and optimizer
criterion = torch.nn.
bceloss
(reduction=
'mean'
)optimizer = torch.optim.
sgd(model.
parameters()
, lr=
0.01
)loss_sum =
# training cycle forward, backward, update
for epoch in range
(100):
for i,
(inputs, labels) in enumerate
(train_loader,0)
: # train_loader 是先shuffle後mini_batch
# dataloader將輸入與標籤放在乙個元組裡
y_pred =
model
(inputs)
loss =
criterion
(y_pred, labels)
print
(epoch, i, loss.
item()
) # 梯度清零,反向傳播
optimizer.
zero_grad()
loss.
backward()
# 更新
optimizer.
step()
if i ==13:
loss_sum.
(loss.
item()
)x =
range
(100
)y = loss_sum
plt.
plot
(x, y)
plt.
xlabel
('epoch'
)plt.
ylabel
('loss'
)plt.
grid
() # 生成網格
plt.
show
()
取每次訓練的最後乙個loss作圖
取每次epoch的平均loss畫圖
for epoch in range
(100):
l_loss =
0for i,
(inputs, labels) in enumerate
(train_loader,0)
: # train_loader 是先shuffle後mini_batch
# dataloader將輸入與標籤放在乙個元組裡
y_pred =
model
(inputs)
loss =
criterion
(y_pred, labels)
print
(epoch, i, loss.
item()
) # 梯度清零,反向傳播
optimizer.
zero_grad()
loss.
backward()
# 更新
SqlDataReader和DataSet的選擇
幾乎在每次面試的時候都會被問道這個問題。使用.net開發當中讀取資料應該採用sqldatareader 還是dataset以及對這兩者的理解。先來談談我本人對這兩者的理解 1 sqldatareader 提供一種從sql server 資料庫讀取行的只進流的方式。無法繼承此類。白話解釋就是 使用這個...
pytorch使用記錄
1.resnet需要載入預訓練模型,但是最後一層fc層的輸出數不是1000。解決方案 先載入預訓練模型,然後取除了最後一層的所有層,再加一層自己需要的在後面。2.model.children 和model.modules 的區別。model.modules 和model.children 均為迭代器...
將DataReader轉換成DataSet
import namespace system.data import namespace system.data.oledb html head runat server script language vb runat server function convertdatareadertodat...