Deep Learning from scratch with pytorch (1/N)
from fastai.vision.all import *
path = untar_data(URLs.MNIST_SAMPLE)
Path.BASE_PATH = path
path.ls()
images = (path/'train'/'3').ls()
images
Image.open(images[0])
a = torch.stack([tensor(Image.open(o)) for o in (path/'train'/'3').ls()]).reshape(-1, 28*28)/255.
b = torch.stack([tensor(Image.open(o)) for o in (path/'train'/'7').ls()]).reshape(-1, 28*28)/255.
X = torch.cat([a, b])
y = tensor([1]*len(a) + [0]*len(b))
a = torch.stack([tensor(Image.open(o)) for o in (path/'valid'/'3').ls()]).reshape(-1, 28*28)/255.
b = torch.stack([tensor(Image.open(o)) for o in (path/'valid'/'7').ls()]).reshape(-1, 28*28)/255.
X_valid = torch.cat([a, b])
y_valid = tensor([1]*len(a) + [0]*len(b))
X.shape, y.shape, X_valid.shape, y_valid.shape
ds = list(zip(X, y))
dl = DataLoader(ds, batch_size=256, shuffle=True)
ds_valid = list(zip(X_valid, y_valid))
dl_valid = DataLoader(ds_valid, batch_size=256)
dls = DataLoaders(dl, dl_valid)
weights = torch.randn((28*28, 1)).requires_grad_()
bias = torch.randn(1).requires_grad_()
weights[:3], bias
X[:5]@weights + bias
W = torch.randn((28*28, 1)).requires_grad_()
b = torch.randn(1).requires_grad_()
lr = 1.
X, t = first(dl)
def doit():
y = (lambda x: 1/(1+torch.exp(-x)))(X@W + b)
loss = ((t-y)**2).mean()
loss.backward()
print(loss.item(), W.grad.mean(), b.grad)
for p in W, b:
p.data -= lr*p.grad
p.grad.zero_()
for _ in range(3):
doit()
W = torch.randn((28*28, 1)).requires_grad_()
b = torch.randn(1).requires_grad_()
lr = 1.
def accuracy():
y = (lambda x: 1/(1+torch.exp(-x)))(X_valid@W + b)
return ((y > 0.5)==t).float().mean()
for epoch in range(30):
for X, t in dl:
y = (lambda x: 1/(1+torch.exp(-x)))(X@W + b)
loss = ((t-y)**2).mean()
loss.backward()
for p in W, b:
p.data -= lr*p.grad
p.grad.zero_()
print(epoch, loss.item(), accuracy())