Files
pyRBM/mnist_test.ipynb
T

7.8 MiB

In [1]:
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from rbm.model import Model
from rbm.entity import Entity, EntityParams, TrainingParams
from rbm.matrix import Mat, np, rms_error_accu
from rbm.torch import Optimizer
import math
In [2]:
transform = transforms.Compose([
    transforms.ToTensor()
])                              
In [3]:
train_data = torchvision.datasets.MNIST(root='./data', train=True, transform=transform, download=True)
test_data = torchvision.datasets.MNIST(root='./data', train=False, transform=transform, download=True)

train_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True, num_workers=2)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=32, shuffle=True, num_workers=2)
In [4]:
print(len(train_data))
60000
In [5]:
print(list(train_data[1][0].size()[1:3]))
[28, 28]
In [6]:
N = 1000
size = 784
train_images = np.zeros(shape=(N,size))
for i in range(N):
    image = train_data[i][0]
    flat_array = torch.flatten(image)
    train_images[i, :] = np.asarray(flat_array.numpy())
In [7]:
print(len(train_images))
1000
In [8]:
n_side = int(math.sqrt(len(train_images)))
fig, axes = plt.subplots(n_side, n_side, figsize=(30,30))
index = 0             
for x in range(n_side):
    for y in range(n_side):
        inp = train_images[index]
        img = np.reshape(inp, (28, 28))
        axes[x,y].imshow(np.asnumpy(img))
        axes[x,y].axis('off')
        index += 1

plt.show()
In [9]:
class TestModel(Model):
	def __init__(self, name: str, work_dir: str = '.'):
		super().__init__(name, work_dir)
		self.unit1 = Entity((28*28, 64), EntityParams(do_gaussian_visible=False, do_gaussian_hidden=False), TrainingParams(learning_rate=0.04, momentum=0.9, num_epochs=1000, mini_batch_size=100, weight_decay=0.001))

	def forward(self, x: Mat):
		x = self.unit1.forward(x)
		return x

	def backward(self, x: Mat):
		x = self.unit1.reconstruct(x)
		return x
In [17]:
work_dir = "results"
prj_name = "mnist_test"
prj_root = "/home/jens/work/repos/Rbm"

# Create model
model = TestModel(prj_name, "results")

# Init state
model.init(0.01)

# load state
model.load()

# Train
model.train(train_images)

# save state
model.save()
results/mnist_test-0-state.npz loaded successfully!
-------------------------------------------
progress              : 0%
err_rms              : 0.011356038512141804
-------------------------------------------
progress              : 10%
err_rms              : 0.01135521322027721
-------------------------------------------
progress              : 20%
err_rms              : 0.011354518147126277
-------------------------------------------
progress              : 30%
err_rms              : 0.011354073570211533
-------------------------------------------
progress              : 40%
err_rms              : 0.011353207054901795
-------------------------------------------
progress              : 50%
err_rms              : 0.011352709591381106
-------------------------------------------
progress              : 60%
err_rms              : 0.011351742639205005
-------------------------------------------
progress              : 70%
err_rms              : 0.011351880712575353
-------------------------------------------
progress              : 80%
err_rms              : 0.011351487334222213
-------------------------------------------
progress              : 90%
err_rms              : 0.011350818512135022
-------------------------------------------
progress              : 100%
err_rms              : 0.011350062308766317
-------------------------------------------
progress              : 100%
err_rms_total              : 0.01134996001927703
results/mnist_test-0-state.npz saved successfully!
In [11]:
# Plot reconstructions
n_side = int(math.sqrt(len(train_images)))
fig, axes = plt.subplots(n_side, n_side, figsize=(30,30))
index = 0             
for x in range(n_side):
    for y in range(n_side):
        inp = train_images[index]
        recon = model.backward(model.forward(inp))
        recon -= np.min(recon)
        recon = recon / np.max(recon)
        img = np.reshape(recon, (28, 28))
        axes[x,y].imshow(np.asnumpy(img))
        axes[x,y].axis('off')
        index += 1

plt.show()
In [16]:
# Plot weights
weights = model.unit1.state.w_hv
n, n_hid = weights.shape
w = np.reshape(weights, (28, 28, n_hid))
print(w.shape)
n_side = int(math.sqrt(n_hid))
fig, axes = plt.subplots(n_side, n_side, figsize=(30,30))

index = 0
for x in range(n_side):
    for y in range(n_side):
        inp = np.asnumpy(w[:,:, index])
        axes[x,y].imshow(inp)
        axes[x,y].axis('off')
        index += 1

plt.show()
(28, 28, 64)
In [13]:
# torch style
In [14]:
model.load()
optimizer = Optimizer(model.unit1)
report_interval = 1000
next_ep = report_interval
optimizer.zero_grad()
for epoch in range(10000):
    running_loss = 0.0

    optimizer.step(train_images)

    if epoch >= next_ep:
        print(f'Training epoch {epoch}...')
        next_ep += report_interval
        # Update final status
        err_rms = rms_error_accu(train_images - model.unit1.reconstruct(model.unit1.forward(train_images)))
        print(err_rms)

model.save()
results/mnist_test-0-state.npz loaded successfully!
Training epoch 1000...
0.014285686454631998
Training epoch 2000...
0.013066340676582817
Training epoch 3000...
0.012460130506494138
Training epoch 4000...
0.012086910269319613
Training epoch 5000...
0.01183957724592736
Training epoch 6000...
0.011642336678180334
Training epoch 7000...
0.01152941933961517
Training epoch 8000...
0.011473569264871109
Training epoch 9000...
0.011426436552050402
results/mnist_test-0-state.npz saved successfully!
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: