GET _search
{
"query": {
"match_all": {}
}
}
GET /
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
!pip3 install torch torchvision |
GET blogs_fixed/_search
{
"query": {
"query_string" : {
"query" : "(content:elasticsearch AND content:engineering) OR (title:elasticsearch AND title:engineering)"
}
}
}
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
test_acc = 0.0 | |
for i, (images, labels) in enumerate(testloader, 0): | |
images = images.to(device) | |
labels = labels.to(device) | |
outputs = model(images) | |
test_acc += get_accuracy(outputs, labels, BATCH_SIZE) | |
print('Test Accuracy: %.2f'%( test_acc/i)) |
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
## train the model | |
for epoch in range(num_epochs): | |
train_running_loss = 0.0 | |
train_acc = 0.0 | |
## commence training | |
model = model.train() | |
## training step | |
for i, (images, labels) in enumerate(trainloader): |
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
## utility function to compute accuracy | |
def get_accuracy(output, target, batch_size): | |
''' Obtain accuracy for training round ''' | |
corrects = (torch.max(output, 1)[1].view(target.size()).data == target.data).sum() | |
accuracy = 100.0 * corrects/batch_size | |
return accuracy.item() |
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
learning_rate = 0.001 | |
num_epochs = 5 | |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | |
model = MyModel() | |
model = model.to(device) | |
criterion = nn.CrossEntropyLoss() | |
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) |
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
## test the model with 1 batch | |
model = MyModel() | |
for images, labels in trainloader: | |
print("batch size:", images.shape) | |
out = model(images) | |
print(out.shape) | |
break |
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
## the model | |
class MyModel(nn.Module): | |
def __init__(self): | |
super(MyModel, self).__init__() | |
self.d1 = nn.Linear(28 * 28, 128) | |
self.dropout = nn.Dropout(p=0.2) | |
self.d2 = nn.Linear(128, 10) | |
def forward(self, x): | |
x = x.flatten(start_dim = 1) |
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
for images, labels in trainloader: | |
print("Image batch dimensions:", images.shape) | |
print("Image label dimensions:", labels.shape) | |
break |