print("Hello world!")
Hello world!
from tinygrad.core import Tensor
from tinygrad.nn import SimpleMLP
from tinygrad.losses import MSELoss, MaxMarginLoss
from tinygrad.optimizers import SimpleSGD
import random
import numpy as np
from sklearn.datasets import make_blobs, make_moons
import matplotlib.pyplot as plt
np.random.seed(1337)
random.seed(1337)
x, y = make_moons(n_samples=100, noise=0.1)
y = y*2 - 1 # make y be -1 or 1
plt.figure(figsize=(5,5))
plt.scatter(x[:,0], x[:,1], c=y, s=20, cmap='jet')
plt.show()
# now we initialize the model
# for demo purposes we initialize a 2, 16, 16, 1 model
model = SimpleMLP(2, 1, [16, 16])
#temporary fix to make last neuron nonlin = False
for i, layer in enumerate(model.layers):
if i == len(model.layers) - 1:
for n in layer.neurons:
n.nonlin = False
model.summary()
print("No of model parameters: ", len(model.parameters()))
SimpleMLP( Linear(ins:2 outs:16 num_parameters:48) Linear(ins:16 outs:16 num_parameters:272) Linear(ins:16 outs:1 num_parameters:17) ) No of model parameters: 337
epochs = 100
lr = 0.005
# now we define the loss function and optimizers
loss_fn = MaxMarginLoss
optim = SimpleSGD(model.parameters(), lr=lr)
X = [list(map(Tensor, _x)) for _x in x] #minor preprocessing
losslist = [] #to store losses
for i in range(epochs):
preds = list(map(model, X))
loss = loss_fn(preds, y)
model.zero_grad()
loss.backward()
lr = 1.0 - .9*i/epochs #manually change lr because schedulers havent been implemented
optim.lr = lr
optim.step()
if i%5 == 0:
print("For epoch: {}, loss: {}".format(i, loss.data))
losslist.append(loss.data)
For epoch: 0, loss: 0.8862514464368222 For epoch: 5, loss: 0.3039345997419534 For epoch: 10, loss: 0.23570270563284787 For epoch: 15, loss: 0.21692423765783492 For epoch: 20, loss: 0.1520994943457478 For epoch: 25, loss: 0.11578019365064655 For epoch: 30, loss: 0.08059487803507807 For epoch: 35, loss: 0.10226911924959595 For epoch: 40, loss: 0.06718934359576106 For epoch: 45, loss: 0.05021609346959929 For epoch: 50, loss: 0.07339858866572174 For epoch: 55, loss: 0.014531426535936509 For epoch: 60, loss: 0.013640569867793939 For epoch: 65, loss: 0.00821577637588291 For epoch: 70, loss: 0.013711454799957803 For epoch: 75, loss: 0.010083769334297063 For epoch: 80, loss: 0.0035957132908880254 For epoch: 85, loss: 0.00016450233490777455 For epoch: 90, loss: 0.00042543722337187175 For epoch: 95, loss: 0.0
plt.xlabel("epochs")
plt.ylabel("Loss")
plt.plot(losslist, range(len(losslist)))
[<matplotlib.lines.Line2D at 0x7ff09f2d42d0>]
#visualize decision boundary
h = 0.25
x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Xmesh = np.c_[xx.ravel(), yy.ravel()]
inputs = [list(map(Tensor, xrow)) for xrow in Xmesh]
scores = list(map(model, inputs))
Z = np.array([s.data > 0 for s in scores])
Z = Z.reshape(xx.shape)
fig = plt.figure()
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.scatter(x[:, 0], x[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()