-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyTorch_5_Linear_Regression.py
More file actions
73 lines (55 loc) · 1.94 KB
/
PyTorch_5_Linear_Regression.py
File metadata and controls
73 lines (55 loc) · 1.94 KB
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
import torch
from torch.nn import Linear,Module
"""
# Define w = 2 and b = -1 for y = wx + b
w = torch.tensor(2.0,requires_grad = True)
b = torch.tensor(-1.0,requires_grad = True)
# Function forward(x) for prediction
def forward(x):
y_pred = w * x + b
return y_pred
# Predict y = 2x - 1 at x = 1
x = torch.tensor([[1.0]])
y_pred = forward(x)
print('Prediction : {}'.format(y_pred))
# Create x Tensor and check the shape of x tensor
x = torch.tensor([[1.0], [2.0]])
print('The shape of x: ', x.shape)
# Make the prediction of y = 2x - 1 at x = [1, 2]
y_pred = forward(x)
print('Prediction : {}'.format(y_pred))
#Make a prediction of y = 2x - 1 at x = [[1.0], [2.0], [3.0]]
x = torch.tensor([[1.0], [2.0], [3.0]])
y_pred = forward(x)
print('Prediction : {}'.format(y_pred))
torch.manual_seed(1)
# Create Linear Regression Model, and print out the parameters
model = Linear(in_features = 1, out_features = 1,bias = True)
print('Parameters w & b are {}'.format(list(model.parameters())))
print(model.state_dict())
print(model.state_dict().keys())
print(model.state_dict().values())
print(model.weight)
print(model.bias)
# Make the prediction at x = [[1.0]]
x = torch.tensor([[1.0]])
y_pred = model(x)
print('The prediction: {}'.format(y_pred))
# Make the prediction using linear model at x = [[1.0], [2.0]]
x = torch.tensor([[1.0], [2.0]])
y_pred = model(x)
print('The prediction: {}'.format(y_pred))
"""
class LR(Module):
def __init__(self,input_size,output_size):
super(LR,self).__init__()
self.linear = Linear(input_size,output_size)
def forward(self,x):
return self.linear(x)
model = LR(1,1)
print('Parameters are : {}'.format(list(model.parameters())))
print('Model is : {}'.format(model))
# Make the prediction using linear model at x = [[1.0], [2.0]]
x = torch.tensor([[1.0], [2.0]])
y_pred = model(x)
print('The prediction: {}'.format(y_pred))