This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
mnist_example.py
93 lines (60 loc) · 2.58 KB
/
mnist_example.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import numpy as np
from sklearn.linear_model import LogisticRegression
import torch
import torchvision.datasets
import torchvision.models
import torchvision.transforms
from rbm import RBM
########## CONFIGURATION ##########
BATCH_SIZE = 64
VISIBLE_UNITS = 784 # 28 x 28 images
HIDDEN_UNITS = 128
CD_K = 2
EPOCHS = 10
DATA_FOLDER = 'data/mnist'
CUDA = torch.cuda.is_available()
CUDA_DEVICE = 0
if CUDA:
torch.cuda.set_device(CUDA_DEVICE)
########## LOADING DATASET ##########
print('Loading dataset...')
train_dataset = torchvision.datasets.MNIST(root=DATA_FOLDER, train=True, transform=torchvision.transforms.ToTensor(), download=True)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE)
test_dataset = torchvision.datasets.MNIST(root=DATA_FOLDER, train=False, transform=torchvision.transforms.ToTensor(), download=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=BATCH_SIZE)
########## TRAINING RBM ##########
print('Training RBM...')
rbm = RBM(VISIBLE_UNITS, HIDDEN_UNITS, CD_K, use_cuda=CUDA)
for epoch in range(EPOCHS):
epoch_error = 0.0
for batch, _ in train_loader:
batch = batch.view(len(batch), VISIBLE_UNITS) # flatten input data
if CUDA:
batch = batch.cuda()
batch_error = rbm.contrastive_divergence(batch)
epoch_error += batch_error
print('Epoch Error (epoch=%d): %.4f' % (epoch, epoch_error))
########## EXTRACT FEATURES ##########
print('Extracting features...')
train_features = np.zeros((len(train_dataset), HIDDEN_UNITS))
train_labels = np.zeros(len(train_dataset))
test_features = np.zeros((len(test_dataset), HIDDEN_UNITS))
test_labels = np.zeros(len(test_dataset))
for i, (batch, labels) in enumerate(train_loader):
batch = batch.view(len(batch), VISIBLE_UNITS) # flatten input data
if CUDA:
batch = batch.cuda()
train_features[i*BATCH_SIZE:i*BATCH_SIZE+len(batch)] = rbm.sample_hidden(batch).cpu().numpy()
train_labels[i*BATCH_SIZE:i*BATCH_SIZE+len(batch)] = labels.numpy()
for i, (batch, labels) in enumerate(test_loader):
batch = batch.view(len(batch), VISIBLE_UNITS) # flatten input data
if CUDA:
batch = batch.cuda()
test_features[i*BATCH_SIZE:i*BATCH_SIZE+len(batch)] = rbm.sample_hidden(batch).cpu().numpy()
test_labels[i*BATCH_SIZE:i*BATCH_SIZE+len(batch)] = labels.numpy()
########## CLASSIFICATION ##########
print('Classifying...')
clf = LogisticRegression()
clf.fit(train_features, train_labels)
predictions = clf.predict(test_features)
print('Result: %d/%d' % (sum(predictions == test_labels), test_labels.shape[0]))