Web: https://www.meetup.com/Tel-Aviv-Deep-Learning-Bootcamp/events/241762893/
Notebooks: On GitHub
Shlomo Kashani
In [1]:
# !pip install pycuda
%reset -f
import numpy
import numpy as np
# imports
import numpy as np # numeric python lib
import matplotlib.image as mpimg # reading images to numpy arrays
import matplotlib.pyplot as plt # to plot any graph
import matplotlib.patches as mpatches # to draw a circle at the mean contour
import scipy.ndimage as ndi # to determine shape centrality
# matplotlib setup
%matplotlib inline
from pylab import rcParams
rcParams['figure.figsize'] = (6, 6) # setting default size of plots
import tensorflow as tf
print("tensorflow:" + tf.__version__)
!set "KERAS_BACKEND=tensorflow"
import numpy as np
import matplotlib.pyplot as plt
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from PIL import Image, ImageFilter
from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image
import matplotlib.pyplot as plot # Library to plot
import matplotlib.cm as colormap # Library to plot
import PIL
from PIL import Image as PILImage
import time
%matplotlib inline
import torch
import sys
print('__Python VERSION:', sys.version)
print('__pyTorch VERSION:', torch.__version__)
print('__CUDA VERSION')
from subprocess import call
# call(["nvcc", "--version"]) does not work
! nvcc --version
print('__CUDNN VERSION:', torch.backends.cudnn.version())
print('__Number CUDA Devices:', torch.cuda.device_count())
print('__Devices')
call(["nvidia-smi", "--format=csv", "--query-gpu=index,name,driver_version,memory.total,memory.used,memory.free"])
print('Active CUDA Device: GPU', torch.cuda.current_device())
print ('Available devices ', torch.cuda.device_count())
print ('Current cuda device ', torch.cuda.current_device())
from __future__ import print_function
import torch
x=torch.Tensor(3,2)
print (type(x))
print (x)
torch.from_numpy (np.zeros((3,4))).cuda()
Out[1]:
In [9]:
# https://github.com/mkrphys/ipython-tikzmagic
# ! pip install git+git://github.com/mkrphys/ipython-tikzmagic.git
# ! apt-get install -qyy imagemagick pdf2svg ghostscript
# sudo apt-get install texlive-full
In [1]:
# # test
# %load_ext tikzmagic
# %reload_ext tikzmagic
# %tikz \draw (0,0) rectangle (1,3);
In [8]:
import numpy as np
import torch
from torch.autograd import Variable
from torch import optim
dtype = torch.cuda.FloatTensor
from data_util import load_mnist
def build_model(input_dim, output_dim):
model = torch.nn.Sequential().cuda()
# model = torch.nn.DataParallel(model, device_ids=[0]).cuda()
model.add_module("linear_1", torch.nn.Linear(input_dim, 512, bias=False))
model.add_module("relu_1", torch.nn.ReLU())
model.add_module("dropout_1", torch.nn.Dropout(0.2))
model.add_module("linear_2", torch.nn.Linear(512, 512, bias=False))
model.add_module("relu_2", torch.nn.ReLU())
model.add_module("dropout_2", torch.nn.Dropout(0.2))
model.add_module("linear_3", torch.nn.Linear(512, output_dim, bias=False))
return model
def train(model, loss, optimizer, x_val, y_val):
x = Variable(x_val, requires_grad=False)
y = Variable(y_val, requires_grad=False)
# Reset gradient
optimizer.zero_grad()
# Forward
fx = model.forward(x)
output = loss.forward(fx, y)
# Backward
output.backward()
# Update parameters
optimizer.step()
return output.data[0]
def predict(model, x_val):
x = Variable(x_val, requires_grad=False)
output = model.forward(x)
return output.data.numpy().argmax(axis=1)
def main():
torch.manual_seed(42)
trX, teX, trY, teY = load_mnist(onehot=False)
trX = torch.from_numpy(trX).float()
teX = torch.from_numpy(teX).float()
trY = torch.from_numpy(trY).long()
n_examples, n_features = trX.size()
n_classes = 10
model = build_model(n_features, n_classes)
loss = torch.nn.CrossEntropyLoss(size_average=True)
optimizer = optim.Adam(model.parameters())
batch_size = 100
for i in range(30):
cost = 0.
num_batches = n_examples // batch_size
for k in range(num_batches):
start, end = k * batch_size, (k + 1) * batch_size
cost += train(model, loss, optimizer, trX[start:end], trY[start:end])
predY = predict(model, teX)
print("Epoch %d, cost = %f, acc = %.2f%%"
% (i + 1, cost / num_batches, 100. * np.mean(predY == teY)))
if __name__ == "__main__":
main()
In [ ]: