Files
pyRBM/mnist_test.ipynb
T
2026-01-05 19:42:01 +01:00

7.5 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 [32]:
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 [33]:
print(len(train_images))
1000
In [34]:
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 [59]:
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.01, momentum=0.9, num_epochs=1000, mini_batch_size=100, weight_decay=0.01))

	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 [62]:
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.016514231819613166
-------------------------------------------
progress              : 10%
err_rms              : 0.01639843007138556
-------------------------------------------
progress              : 20%
err_rms              : 0.016256438179798025
-------------------------------------------
progress              : 30%
err_rms              : 0.01628389074954646
-------------------------------------------
progress              : 40%
err_rms              : 0.01628875342034373
-------------------------------------------
progress              : 50%
err_rms              : 0.01626984655734547
-------------------------------------------
progress              : 60%
err_rms              : 0.016189959113389853
-------------------------------------------
progress              : 70%
err_rms              : 0.015919747384763892
-------------------------------------------
progress              : 80%
err_rms              : 0.015955189199063118
-------------------------------------------
progress              : 90%
err_rms              : 0.01578382948216496
-------------------------------------------
progress              : 100%
err_rms              : 0.016054731900309323
-------------------------------------------
progress              : 100%
err_rms_total              : 0.01609967068562382
results/mnist_test-0-state.npz saved successfully!
In [63]:
# 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 [64]:
# 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 [28]:
# torch style
In [29]:
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...
8.96825598925981e-05
Training epoch 2000...
7.411453872389954e-05
Training epoch 3000...
6.385617246522372e-05
Training epoch 4000...
5.592441719548036e-05
Training epoch 5000...
5.010598438712846e-05
Training epoch 6000...
4.5392183534269646e-05
Training epoch 7000...
4.1286520993939346e-05
Training epoch 8000...
3.827315722993446e-05
Training epoch 9000...
3.5197840010566715e-05
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 [ ]: