-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyTorch_14_MultipleLinearRegression.py
More file actions
64 lines (48 loc) · 1.3 KB
/
PyTorch_14_MultipleLinearRegression.py
File metadata and controls
64 lines (48 loc) · 1.3 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
import torch
from torch import nn
torch.manual_seed(1)
#set the weight and bias
w = torch.tensor([[2.0],[3.0]],requires_grad=True)
b = torch.tensor([[1.0]],requires_grad=True)
#define the prediction function
def forward(x):
y_pred = torch.mm(x,w) + b
return y_pred
#calculate y_pred
x = torch.tensor([[1.0,2.0]])
y_pred = forward(x)
print(y_pred)
#Sample tensor X
X = torch.tensor([[1.0,1.0],[1.0,2.0],[1.0,3.0]])
#Make a prediction of X
y_pred = forward(X)
print(y_pred)
#Make a linear regression model using buitin function
model = nn.Linear(2,1)
print(list(model.parameters()))
#make a prediction of x
y_pred = model(x)
print(y_pred)
#make a prediciton of X
y_pred = model(X)
print(y_pred)
#create linear regression class
class MLR(nn.Module):
#contructor
def __init__(self,input_size,output_size):
super(MLR,self).__init__()
self.linear = nn.Linear(in_features = input_size, out_features = output_size)
#prediction function
def forward(self,x):
y_pred = self.linear(x)
return y_pred
model = MLR(2,1)
#print model parameters
print(list(model.parameters()))
print(model.state_dict())
#make a prediction of x
y_pred = model(x)
print(y_pred)
#make a prediciton of X
y_pred = model(X)
print(y_pred)