-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_for_pytorch.py
84 lines (71 loc) · 2.44 KB
/
test_for_pytorch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
class Data(Dataset):
def __init__(self, X_train, y_train):
# need to convert float64 to float32 else
# will get the following error
# RuntimeError: expected scalar type Double but found Float
self.X = torch.from_numpy(X_train.astype(np.float32))
# need to convert float64 to Long else
# will get the following error
# RuntimeError: expected scalar type Long but found Float
self.y = torch.from_numpy(y_train).type(torch.LongTensor)
self.len = self.X.shape[0]
def __getitem__(self, index):
return self.X[index], self.y[index]
def __len__(self):
return self.len
X, Y = make_classification(
n_samples=100, n_features=4, n_redundant=0,
n_informative=3, n_clusters_per_class=2, n_classes=3
)
X_train, X_test, Y_train, Y_test = train_test_split(
X, Y, test_size=0.33, random_state=42)
traindata = Data(X_train, Y_train)
batch_size = 4
trainloader = DataLoader(traindata, batch_size=batch_size,
shuffle=True, num_workers=2)
# number of features (len of X cols)
input_dim = 4
# number of hidden layers
hidden_layers = 25
# number of classes (unique of y)
output_dim = 3
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
#self.linear1 = nn.Linear(input_dim, hidden_layers)
self.linear1 = nn.Linear(input_dim, output_dim)
#self.linear2 = nn.Linear(hidden_layers, output_dim)
def forward(self, x):
x = torch.sigmoid(self.linear1(x))
#x = self.linear2(x)
return x
clf = Network()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(clf.parameters(), lr=0.1)
print("before training: ")
print(list(clf.parameters()))
epochs = 2
for epoch in range(epochs):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
# set optimizer to zero grad to remove previous epoch gradients
optimizer.zero_grad()
# forward propagation
outputs = clf(inputs)
loss = criterion(outputs, labels)
# backward propagation
loss.backward()
# optimize
optimizer.step()
running_loss += loss.item()
# display statistics
print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.5f}')
print("after training: ")
print(list(clf.parameters()))