-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearRegression.py
59 lines (44 loc) · 1.7 KB
/
LinearRegression.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
import os
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torch.autograd import Variable
img_size = 784
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001
# MNIST dataset (images and labels)
train_dataset = torchvision.datasets.MNIST(root='../../data',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = torchvision.datasets.MNIST(root='../../data',
train=False,
transform=transforms.ToTensor())
# Data loader (input pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
# Linear regression model
model = nn.Linear(img_size, 1)
# Loss and optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# Training
total_step = len(train_loader)
for epoch in range(num_epochs):
for images, labels in (train_loader):
# Reshape images to (batch_size, input_size)
images = images.reshape(-1, 28 * 28)
# Forward pass
outputs = model(images)
loss = criterion(torch.squeeze(outputs), labels.float())
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()