Skip to content

Instantly share code, notes, and snippets.

View omarsar's full-sized avatar
🐙

Elvis Saravia omarsar

🐙
View GitHub Profile
!pip3 install torch torchvision
@omarsar
omarsar / eng1-milan.md
Created September 30, 2019 09:41
milan-eng1-module1.md
GET _search
{
  "query": {
    "match_all": {}
  }
}


GET /
@omarsar
omarsar / bool_query_with_or.md
Created September 16, 2019 14:52
Bool Query with OR and AND
GET blogs_fixed/_search
{
    "query": {
        "query_string" : {
            "query" : "(content:elasticsearch AND content:engineering) OR (title:elasticsearch AND title:engineering)"
        }
    }
}
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))
## 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):
## 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()
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)
## 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
## 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)
for images, labels in trainloader:
print("Image batch dimensions:", images.shape)
print("Image label dimensions:", labels.shape)
break