This repository was archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_model_torch.py
65 lines (47 loc) · 1.84 KB
/
linear_model_torch.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
import torch
class ModelNumberQuestions(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(in_features=1, out_features=1)
def forward(self, tensor_number_tasks):
return self.linear(tensor_number_tasks)
class MSELoss(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, estimates, actuals):
sqr_diff = (estimates - actuals) ** 2
return sqr_diff.mean()
def train_parameters_linear_regression(
list_number_tasks,
list_number_questions,
learning_rate=0.02,
number_training_steps=200,
):
"""
Instantiate ModelNumberQuestions model and optimises the parameters of the model, given the dataset
of list_number_tasks and list_number_questions.
Args:
list_number_tasks (List[float]): of size n where n is the number of questions (it is also the number of tasks)
list_number_questions (List[float]): of size n where n is the number of questions (it is also the number of tasks)
learning_rate (float):
number_training_steps (int):
Returns:
trained network (ModelNumberQuestions)
"""
net = ModelNumberQuestions()
mse = MSELoss()
optimiser = torch.optim.SGD(net.parameters(), lr=learning_rate)
for i in range(number_training_steps):
optimiser.zero_grad()
estimates = net(list_number_tasks)
loss = mse(estimates, list_number_questions)
loss.backward()
optimiser.step()
for param in list(net.named_parameters()):
print(param)
return net
list_number_tasks = torch.Tensor([1, 2, 4, 4, 5, 6, 6, 6, 8, 8, 9, 10]).view(-1, 1)
list_number_questions = torch.Tensor(
[5, 11, 21, 22, 26, 31, 32, 31, 41, 42, 48, 52]
).view(-1, 1)
train_parameters_linear_regression(list_number_tasks, list_number_questions)