Last active
April 3, 2019 07:51
-
-
Save FishOfPrey/43ae80b18f3595756ab042974107d321 to your computer and use it in GitHub Desktop.
Deep Learning and Natural Language Processing
This file contains hidden or 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(123) | |
# TODO: Generate 2 clusters of 100 2d vectors, each one distributed normally, using | |
# only two calls of randn() | |
classApoints = | |
classBpoints = | |
# TODO: Add the vector [1.0,3.0] to the first cluster and [3.0,1.0] to the second. | |
classApoints += | |
classBpoints += | |
# TODO: Concatenate these two clusters along dimension 0 so that the points | |
# distributed around [1.0, 3.0] all come first | |
inputs = | |
# TODO: Create a tensor of target values, 0 for points for the first cluster and | |
# 1 for the points in the second cluster. Make sure that these are LongTensors. | |
classA = | |
classB = | |
targets = torch.cat([classA, classB]) | |
# TODO: Set the random seed to 123 using manual_seed | |
# TODO: Initialize a Linear layer to output scores | |
# for each class given the 2d examples | |
model = | |
# TODO: Define your loss function | |
loss_fn = | |
# TODO: Initialize an SGD optimizer with learning rate 0.1 | |
optimizer = | |
# Train the model for 100 epochs | |
n_epochs = 100 | |
losses = [] | |
for _ in range(n_epochs): | |
optimizer.zero_grad() | |
preds = model(inputs) | |
loss = TODO | |
losses.append(loss) | |
loss.backward() | |
optimizer.step() | |
print(f'Anwswer to Exercise 6: Loss after {n_epochs} epochs: {losses[-1]}') | |
iterations = np.arange(len(losses)) | |
_ = plt.plot(iterations, losses, '', iterations, losses, '-') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment