This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
learning_rate = 0.2 | |
epochs = 10000 | |
X_train = X_train.float() | |
Y_train = Y_train.long() | |
loss_arr = [] | |
acc_arr = [] | |
for epoch in range(epochs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
torch.manual_seed(0) | |
weights1 = torch.randn(2, 2) / math.sqrt(2) | |
weights1.requires_grad_() | |
bias1 = torch.zeros(2, requires_grad=True) | |
weights2 = torch.randn(2, 4) / math.sqrt(2) | |
weights2.requires_grad_() | |
bias2 = torch.zeros(4, requires_grad=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def accuracy(y_hat, y): | |
pred = torch.argmax(y_hat, dim=1) | |
return (pred == y).float().mean() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def loss_fn(y_hat, y): | |
return -(y_hat[range(y.shape[0]), y].log()).mean() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = torch.rand(2,4) | |
print(a) | |
print(a.exp()) | |
print(a.exp().sum(-1)) | |
print(a.exp().sum(-1).shape) | |
print(a.exp().sum(-1).unsqueeze(-1)) | |
print(a.exp().sum(-1).unsqueeze(-1).shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def model(x): | |
a1 = torch.matmul(x, weights1) + bias1 # (N, 2) x (2, 2) -> (N, 2) | |
h1 = a1.sigmoid() # (N, 2) | |
a2 = torch.matmul(h1, weights2) + bias2 # (N, 2) x (2, 4) -> (N, 4) | |
h2 = a2.exp()/a2.exp().sum(-1).unsqueeze(-1) # (N, 4) | |
return h2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X_train, Y_train, X_val, Y_val = map(torch.tensor, (X_train, Y_train, X_val, Y_val)) | |
print(X_train.shape, Y_train.shape) | |
//torch.Size([750, 2]) torch.Size([750]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X_train, X_val, Y_train, Y_val = train_test_split(data, labels, stratify=labels, random_state=0) | |
print(X_train.shape, X_val.shape, labels.shape) | |
//(750, 2) (250, 2) (1000,) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","yellow","green"]) | |
plt.scatter(data[:,0], data[:,1], c=labels, cmap=my_cmap) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data, labels = make_blobs(n_samples=1000, centers=4, n_features=2, random_state=0) | |
print(data.shape, labels.shape) | |
//(1000, 2) (1000,) |