Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

linear.py 2.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. #%%
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. #%%
  5. x_train = np.array([[3.3], [4.4], [6.71], [4.168], [9.779], [3.1]], dtype = np.float32)
  6. y_train = np.array([[1.6], [3.5], [6.2], [23.2], [13.2], [5.3]], dtype = np.float32)
  7. #%%
  8. plt.plot(x_train, y_train, 'ro', label = 'Original data')
  9. plt.show()
  10. #%%
  11. import torch
  12. X_train = torch.from_numpy(x_train)
  13. Y_train = torch.from_numpy(y_train)
  14. print('requires_grad for X_train: ', X_train.requires_grad) # False, because it's not needed - only variables that are trained during the training phase need autograd on
  15. print('requires_grad for Y_train:', Y_train.requires_grad)
  16. #%%
  17. input_size = 1 # each data point is a single feature i.e.: represented by a single value
  18. hidden_size = 100 # handcraft a neural network with a single layer with 100 neurons and ReLU activation
  19. output_size = 1 # only one output
  20. learning_rate = 1e-6 # the factor by which we adjust our weights for every epoch; calculate gradients in backward pass to manually adjust the weights
  21. w1 = torch.rand(input_size, hidden_size, requires_grad=True) # turn on gradients during training
  22. w1.size()
  23. #%%
  24. w2 = torch.rand(hidden_size, output_size, requires_grad=True)
  25. w2.size()
  26. #%%
  27. for iter in range(1, 301):
  28. y_pred = X_train.mm(w1).clamp(min=0).mm(w2) # mm: matrix multiply the activation function; clamp forces all negative values to 0 (ReLU); then matrix multiply with the second weight
  29. loss = (y_pred - Y_train).pow(2).sum() # mean square error loss, sum all the square of the differences between actual and predicted values
  30. if iter % 50 == 0:
  31. print(iter, loss.item())
  32. loss.backward() # use autograd for an automated way of implementing the backward pass through the neural network; computes gradient of loss with respect to all tensors which have requires_grad=True
  33. # tweak the nn to make better predictions
  34. with torch.no_grad(): # don't need gradients when we adjust the model
  35. w1 -= learning_rate * w1.grad
  36. w2 -= learning_rate * w2.grad
  37. w1.grad.zero_()
  38. w2.grad.zero_()
  39. #%%
  40. print('w1: ', w1) # final model parameters
  41. print('w1: ', w1)
  42. #%%
  43. x_train_tensor = torch.from_numpy(x_train)
  44. x_train_tensor
  45. #%%
  46. predicted_in_tensor = x_train_tensor.mm(w1).clamp(min=0).mm(w2)
  47. predicted_in_tensor
  48. #%%
  49. predicted = predicted_in_tensor.detach().numpy() # detach the tensor from the current graph so no gradients are computed on the new tensor
  50. predicted
  51. #%%
  52. plt.plot(x_train, y_train, 'ro', label = 'Original data')
  53. plt.plot(x_train, predicted, label = 'Fitted line ')
  54. plt.legend()
  55. plt.show()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...