-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNN.py
119 lines (95 loc) · 2.65 KB
/
NN.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# implementing the whole pytorch thing using the toch.nn module
import torch
import torch.nn as nn
import numpy as np
# Inputs (temp , rainfall, humidity)
inputs = np.array([
[ 68, 99, 53],
[ 89, 66, 98],
[ 52, 56, 119],
[102, 87, 76],
[105, 106, 93],
[ 55, 102, 101],
[ 64, 55, 80],
[ 73, 78, 77],
[ 78, 59, 115],
[ 56, 89, 67],
[ 84, 79, 91],
[ 55, 115, 75],
[ 89, 90, 87],
[113, 77, 97],
[ 96, 55, 86],
],dtype='float32')
# Targets (apples and oranges)!!
targets = np.array([
[ 86, 59],
[102, 61],
[ 98, 55],
[ 95, 52],
[119, 85],
[ 59, 85],
[ 87, 73],
[ 57, 80],
[106, 57],
[ 68, 66],
[ 69, 118],
[102, 88],
[114, 83],
[ 80, 58],
[ 66, 100],
],dtype='float32')
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
## Dataset and Dataloader:
from torch.utils.data import TensorDataset
# define a dataset
train_ds = TensorDataset(inputs,targets)
# This allows us to rows of input and target as tuples
# print(train_ds[0:3])
# basically it lets you access some slice of a data
from torch.utils.data import DataLoader
# Define a dataloader:
# will solit our data into batches
batch_size = 5 # perbatch 5 data points, generally about 100 rakhenge
train_dl = DataLoader(train_ds,batch_size=batch_size,shuffle=True)
# for xb,yb in train_dl:
# print(xb)
# print(yb,'\n')
## Model :
# Defining the model:
model = nn.Linear(3,3), # 3 inputs and 2 outputs
# print(model.weight)
# print(model.bias)
# Both the parameters have set weights and biases to true.
# print(list(model.parameters()))# This method lists all the parameters contained in our model
# Generating the predictions:
# preds = model(inputs)
# print(preds)
# Define the Loss function:
loss_fn = nn.MSELoss()
# loss = loss_fn(model(inputs),targets)
# print(loss)
## Optimizers:
lr = 1.5e-3
# define the optimizer:
opt = torch.optim.SGD(model.parameters(),lr=lr)
## Train the model:
def train(num_epochs,model,loss_fn,opt,train_dl):
for epoch in range(num_epochs):
for xb,yb in train_dl:
# Generate the predictions:
pred = model(xb)
# Calculate the loss:
loss = loss_fn(pred,yb)
# Calculate the gradients:
loss.backward()
# Update the parms:
opt.step()
# resret the gradients to zero:
opt.zero_grad()
# Print the progress:
if((epoch+1)%10==0):
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1,num_epochs,loss.item()))
train(100,model,loss_fn,opt,train_dl)
print(model(inputs),'\n')
print(targets)