Benjamin Akera
Learning how machines learn, and learning along the way
Benjamin Akera
Deep learning frameworks offer building blocks for designing, training and validating deep neural networks, through a high level programming interface
Composing representations of data in a hierarchical manner
x = tf.constants(42,name='x')
w = tf.Variable(1.34, name = 'w')
input = tf.Placeholder("float")
with tf.Session() as sess:
1,[1,2],[[2,3]],[4,5]] ...
g = tf.Graph("my_graph")
with g.as_default():
c = tf.add(x,w)
m = tf.matmul(a,b)
c = tf.add(x,w);
m = tf.matmul(a,b)
Tensorflow Code
import tensorflow as tf
a = tf.placeholder(tf.float32,shape=(2,1))
b = tf.placeholder(tf.float32,shape=(1,2))
c = tf.matmul(a,b)
sess = tf.Session()
print(sess.run(c,{a:[[1],[2]],b:[[3,4]]}))
[[3.4.][6.8.]]
3-4: create tf placeholder types, a&b Define their input shapes as tensors
1: Import tensorflow
9: create a Tf Session, with input parameters for placeholders 'a' & 'b'
'c' as an operation won't run until sess.run() Lazily evaluated
13: TF Session output
Tensorflow Code: MNIST
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
## Create the model
x = tf.placeholder(tf.float32,[None,784])
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x,W) + b
y_ = tf.placeholder(tf.int64, [None])
...
# Define Loss and optimizer
cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y_,logits=y)
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# Create Session
sess = tf.InteractiveSession()
tf.global_variables_initializer().run
## Train
for _ in range(1000):
batch_xs,batch_ys = mnist.train.next_batch(100)
sess.run(train_steps, feed_dict{x:batch_xs, batch_ys})
Tf placeholders and variables
Define our model TF variable for predicted value y
Train the model
...
Estimators
Layers
GPU
TPU
Android
iOS
...
Datasets
Metrics
Keras Models
Python
C++
Java
Go
Tensorflow Distributed Execution Engine
CPU
High-Level TF
APIs
Mid-Level TF
APIs
Low-Level TF
APIs
TF Kernel
Use canned estimators
Build models
TF Stack
Visualizing Tensors: Tensorboard
Deploy + serving models: TFX
Tensorflow: We get it....So what?
Keras Code: MNIST
2.Compile Network
1. Define
Network
3.Fit Network
4. Evaluate
Network
5. Make Predictions
from keras import models
from keras import layers
mnist = tf.keras.datasets.mnist
(train_images, train_labels),(test_images, test_labels)=
prepare_data(mnist.load_data())
network= models.Sequential()
network.add(layers.Dense(512, activation='relu',input_shape(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))
network.compile(optimizer=’rmsprop',loss='categorical_crossentropy',
metrics=['accuracy'])
results = network.evaluate(test_images, test_labels)
predictions = network.predict(new_images)
network.fit(train_images, train_labels,epochs=5,
batch_size=128)
The Pythonic Deep Learning Framework
What ?
Why ?
Research
NumPy
Celebrity endorsements
Dynamic computation graphs
Dynamic computation graphs
Can use regular control flow statements eg. for loops
Much easier to debug, can set breakpoints anywhere in the code
a = 1
b = 3
c = a + b
d = c * 3
e = c - 5
print(d)
a = 1
b = 3
c = a + b
d = c * 3
e = c - 5
f = run(d)
print(f)
import numpy as np
import torch
from torch.autograd import Variable
model = torch.nn.Linear(1, 1)
loss_fn = torch.nn.MSELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for t in range(10000):
x = Variable(torch.from_numpy(np.random.random((1,1)).astype(np.float32)))
y = x * 3
y_pred = model(x)
loss = loss_fn(y_pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print loss.data[0]
Source: Tensorflow sucks
import tensorflow as tf
import numpy as np
X = tf.placeholder("float")
Y = tf.placeholder("float")
W = tf.Variable(np.random.random(), name="weight")
pred = tf.multiply(X, W)
cost = tf.reduce_sum(tf.pow(pred-Y, 2))
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for t in range(10000):
x = np.array(np.random.random()).reshape((1, 1, 1, 1))
y = x * 3
(_, c) = sess.run([optimizer, cost], feed_dict={X: x, Y: y})
print c
Source: Tensorflow sucks
Deployment and multi GPU training
TensorBoard visualisation
Are doing research and want to jump into production quickly
Want to take advantage of the huge TF community
Want to use tensorboard, Google Cloud Support + Many other tools
Want to get scooped by Google :)
As an Expert etc
Are doing research, especially NLP
Like debugging things easily
Are a machine learning hipster
Want to get started with Deep Learning
Are a beginner
Want to use Deep Learning for Production on Microsoft Azure
Are a Java Developer
For Interoperability with other frameworks
To Conclude ...
Questions ?
BenjaminAkera
BenjaminAkera
akeraben@gmail.com
By Benjamin Akera
Presentation made at the IndabaX ug workshop 5th April 2019