Skip to content

Instantly share code, notes, and snippets.

View omarsar's full-sized avatar
🐙

Elvis Saravia omarsar

🐙
View GitHub Profile
@omarsar
omarsar / data_mining_fall_2018.md
Last active October 17, 2018 05:15
Instructions for Data Mining Lab Session 1

Computing Resources

  • Operating system: Preferably Linux or MacOS. If you have Windows, things may crash unexpectedly (try installing a virtual machine if you need to)
  • RAM: Minimum 8GB
  • Disk space: Mininium 16GB

Software Requirements

In this lab session we are going to be using Python as our main programming language. If you are not familiar with it, I recommend you start with this free Python course offered by Codecademy.

Here is a list of the required programs and libraries necessary for this lab session. (Please install them before coming to our lab session on Tuesday; this will save us a lot of time, plus these include some of the same libraries you may need for your first assignment).

tensor([[0.6741],
[0.9680],
[0.9973],
[0.9993]])
tensor([[ 0.9889],
[-0.4191],
[ 0.8347],
[-0.0384]])
test_acc = 0.0
for i, data in enumerate(testloader, 0):
inputs, labels = data
inputs = inputs.view(-1, 28, 28)
outputs = model(inputs)
test_acc += get_accuracy(outputs, labels, BATCH_SIZE)
print('Test Accuracy: %.2f'%( test_acc/i))
for epoch in range(N_EPHOCS): # loop over the dataset multiple times
train_running_loss = 0.0
train_acc = 0.0
model.train()
# TRAINING ROUND
for i, data in enumerate(trainloader):
# zero the parameter gradients
optimizer.zero_grad()
import torch.optim as optim
# Device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Model instance
model = ImageRNN(BATCH_SIZE, N_STEPS, N_INPUTS, N_NEURONS, N_OUTPUTS)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
dataiter = iter(trainloader)
images, labels = dataiter.next()
model = ImageRNN(BATCH_SIZE, N_STEPS, N_INPUTS, N_NEURONS, N_OUTPUTS)
logits = model(images.view(-1, 28,28))
print(logits[0:10])
### output
'''
tensor([[-0.0197, 0.0082, 0.0248, 0.0758, -0.0110, 0.0521, -0.0261, 0.0217,
-0.0083, 0.0866],
class ImageRNN(nn.Module):
def __init__(self, batch_size, n_steps, n_inputs, n_neurons, n_outputs):
super(ImageRNN, self).__init__()
self.n_neurons = n_neurons
self.batch_size = batch_size
self.n_steps = n_steps
self.n_inputs = n_inputs
self.n_outputs = n_outputs
# parameters
N_STEPS = 28
N_INPUTS = 28
N_NEURONS = 150
N_OUTPUTS = 10
N_EPHOCS = 10
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
#img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
# get some random training images
import torchvision
import torchvision.transforms as transforms
BATCH_SIZE = 64
# list all transformations
transform = transforms.Compose(
[transforms.ToTensor()])
# download and load training dataset