import pandas as pd
import numpy as np
df = pd.DataFrame(
data=np.random.beta(1, 1, size=[40, 3]),
columns=['x', 'y', 'z'],
index=['A', 'B', 'C', 'D'] * 10,
)
df['species'] = [np.random.choice(['penguin', 'giraffe', 'elephant']) for _ in df.index]
df['captive'] = [True if i == 'A' or i == 'D' else False for i in df.index]
df
embedding = torch.eye(10 + 1)
plt.imshow(embedding)
plt.xticks(ticks=range(embedding.shape[0]))
plt.yticks(ticks=range(embedding.shape[0]))
plt.show()
def number_to_vector(number):
if number > embedding.shape[0] or number < 0:
raise Exception
return embedding[number]
def vector_to_number(vector):
return torch.argmax(vector).item()
number_to_vector(10)
vector_to_number(number_to_vector(10))
features = [1, 2, 3, 4]
targets = [2, 4, 6, 8]
features = torch.vstack([number_to_vector(x) for x in features])
targets = torch.vstack([number_to_vector(x) for x in targets])
for (x, y) in zip(features, targets):
print("feature:", vector_to_number(x), "=", x, ",", "target:", vector_to_number(y), "=", y)
feature: 1 = tensor([0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) , target: 2 = tensor([0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.])
feature: 2 = tensor([0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]) , target: 4 = tensor([0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.])
feature: 3 = tensor([0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]) , target: 6 = tensor([0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.])
feature: 4 = tensor([0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]) , target: 8 = tensor([0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.])
model = torch.nn.Sequential(OrderedDict([
('linear', torch.nn.Linear(features.shape[1], targets.shape[1])),
('activation', torch.nn.Sigmoid()),
]))
model_graph = make_dot(
model(features[0]),
params=dict(model.named_parameters())
)
# Okay deepnote is not quite there yet, have to hack things a bit to show SVG
class Graph():
def __init__(self, graph):
self.graph = graph
def _repr_html_(self):
return self.graph.pipe(format="svg").decode('utf-8')
Graph(model_graph)
losses = train_model(
model=model,
optimizer=torch.optim.SGD(model.parameters(), lr=1e-1), # lr = learning_rate
loss_fn=mean_squared_error,
epochs=1000,
plot_interval=200
)
plt.plot(losses)
plt.title("Loss")
plt.xlabel("Epoch")
plt.show()
/opt/venv/lib/python3.7/site-packages/torch/autograd/__init__.py:132: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
allow_unreachable=True) # allow_unreachable flag
embedding
"Vector of all our numbers"
values = torch.Tensor(range(len(embedding)))
print(values)
plt.imshow(values.view([1, -1]))
plt.yticks([])
plt.xticks(list(range(len(embedding))))
plt.show()
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
def predict(x: int or torch.Tensor):
if not isinstance(x, torch.Tensor):
x = number_to_vector(x)
model.eval()
with torch.no_grad():
y_pred = model(x)
fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=(18, 3))
axes[0].imshow(x.view([1, -1]), vmin=0, vmax=1)
axes[0].set_title("X")
x_im = axes[1].imshow(y_pred.view([1, -1]), vmin=0, vmax=1)
axes[1].set_title("Y_predicted")
plt.yticks([])
for ax in axes:
ax.set_xticks(range(len(x)))
fig.colorbar(x_im, ax=axes.ravel().tolist(), cax=fig.add_axes([0.95, 0.3, 0.03, 0.38]))
plt.show()
plt.close(fig)
"Compute the actual values of the vectors"
input_value = torch.dot(x, values).item()
output_value = torch.dot(y_pred, values).item()
print("Total input value:", input_value, ",", "total output value:", output_value)
"Evaluate the model for our entire dataset & compare with targets"
for (x, y) in zip(features, targets):
predict(x)
Total input value: 1.0 , total output value: 3.482379913330078
Total input value: 2.0 , total output value: 5.228498458862305
Total input value: 3.0 , total output value: 6.952465057373047
Total input value: 4.0 , total output value: 8.838418006896973
predict(5)
Total input value: 5.0 , total output value: 5.405344009399414
model2 = torch.nn.Sequential(
torch.nn.Linear(1, 1, bias=False),
# torch.nn.Sigmoid()
)
loss_fn2 = torch.nn.MSELoss(reduction="mean")
optimizer2 = torch.optim.SGD(model2.parameters(), lr=1e-1)
# features2 = torch.Tensor([[1], [2], [3], [4], [5]])
# targets2 = torch.Tensor([[2], [4], [6], [8], [10]])
features2 = torch.Tensor([[1], [2]])
targets2 = torch.Tensor([[2], [4]])
losses2 = []
for t in range(5):
loss = 0
for (x, y) in zip(features2, targets2):
y_pred = model2(x)
loss += loss_fn2(y_pred, y)
optimizer2.zero_grad()
loss.backward()
optimizer2.step()
losses2.append(loss)
plt.plot(losses2)
plt.show()
model2(torch.Tensor([5]))
model2(torch.Tensor([20]))
model2(torch.Tensor([420]))
units = 16
model3 = torch.nn.Sequential(
torch.nn.Linear(1, units),
torch.nn.Sigmoid(),
torch.nn.Linear(units, units),
torch.nn.Sigmoid(),
torch.nn.Linear(units, 1),
)
loss_fn3 = torch.nn.MSELoss(reduction="sum")
optimizer3 = torch.optim.SGD(model3.parameters(), lr=0.1)
features3 = torch.Tensor([[1], [2], [3], [4], [5]])
targets3 = torch.Tensor([[2], [4], [6], [8], [10]])
# features3 = torch.Tensor([[1], [2]])
# targets3 = torch.Tensor([[2], [4]])
losses3 = []
for t in range(150):
loss = 0
for (x, y) in zip(features3, targets3):
y_pred = model3(x)
loss += loss_fn3(y_pred, y)
optimizer3.zero_grad()
loss.backward()
optimizer3.step()
losses3.append(loss)
plt.plot(losses3)
plt.show()
losses3[-1]
model3(torch.Tensor([2]))