-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPytroch_example.py
More file actions
66 lines (58 loc) · 2.41 KB
/
Pytroch_example.py
File metadata and controls
66 lines (58 loc) · 2.41 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from complexPyTorch.complexLayers import ComplexBatchNorm2d, ComplexConv2d, ComplexLinear
from complexPyTorch.complexFunctions import complex_relu, complex_max_pool2d
batch_size = 64
trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
train_set = datasets.MNIST('../data', train=True, transform=trans, download=True)
test_set = datasets.MNIST('../data', train=False, transform=trans, download=True)
train_loader = torch.utils.data.DataLoader(train_set, batch_size= batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_set, batch_size= batch_size, shuffle=True)
class ComplexNet(nn.Module):
def __init__(self):
super(ComplexNet, self).__init__()
self.conv1 = ComplexConv2d(1, 10, 5, 1)
self.bn = ComplexBatchNorm2d(10)
self.conv2 = ComplexConv2d(10, 20, 5, 1)
self.fc1 = ComplexLinear(4*4*20, 500)
self.fc2 = ComplexLinear(500, 10)
def forward(self,x):
x = self.conv1(x)
x = complex_relu(x)
x = complex_max_pool2d(x, 2, 2)
x = self.bn(x)
x = self.conv2(x)
x = complex_relu(x)
x = complex_max_pool2d(x, 2, 2)
x = x.view(-1,4*4*20)
x = self.fc1(x)
x = complex_relu(x)
x = self.fc2(x)
x = x.abs()
x = F.log_softmax(x, dim=1)
return x
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = ComplexNet().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
def train(model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device).type(torch.complex64), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % 100 == 0:
print('Train Epoch: {:3} [{:6}/{:6} ({:3.0f}%)]\tLoss: {:.6f}'.format(
epoch,
batch_idx * len(data),
len(train_loader.dataset),
100. * batch_idx / len(train_loader),
loss.item())
)
# Run training on 50 epochs
for epoch in range(50):
train(model, device, train_loader, optimizer, epoch)