<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <center><img src="images/title.png" width="95%"/></center>
<center><a href="http
Step1:  <center><img src="images/tf_logo.jpg" width="70%"/></center>
"An open-source software library for Machine Intelligence"
Numerical computation using data flow graphs. 
TensorFlow
Step2:  Munge data
<center><img src="images/mnist_keras.png" width="75%"/></center>
Convert image matrix into vector to feed into first layer
Step3:  RTFM - https
Step4:  <center><img src="images/dense.png" width="55%"/></center>
Step5:  <center><img src="images/waiting.jpg" width="55%"/></center>
Step6:  Keras' Other Features
Common built-in functions (e.g., activation functions and optimitizers)
Convolutional neural network (CNN or ConvNet)
Recurrent neural network (RNN) & Long-short term memory (LSTM)
Pre-trained models
Summary
Keras is designed for human beings, not computers.
Easier to try out Deep Learning (focus on the what, not the how).
Simple to define neural networks.
<center><img src="images/twitter.png" width="100%"/></center>
Futher Study - Keras
Keras docs
Keras blog
Keras courses
edX
Coursera
Futher Study - Deep Learning
Prerequisites
<ASSISTANT_TASK:>
Python Code:
reset -fs
import keras
# What is the backend / execution engine?
keras.backend.backend()
Explanation: <center><img src="images/title.png" width="95%"/></center>
<center><a href="http://bit.ly/pybay-keras">bit.ly/pybay-keras</a></center>
Who Am I?
<center>Brian Spiering</center>
What Do I Do?
<b><center>Professor @</center><b>
<center><img src="images/msds_logo.png" width="28%"/></center>
<center><img src="images/real_deep_learning.png" width="80%"/></center>
Keras - Neural Networks for humans
<center><img src="images/keras-logo-small.jpg" width="20%"/></center>
A high-level, intuitive API for Deep Learning.
Easy to define neural networks, then automatically handles execution.
A simple, modular interface which allows focus on learning and enables fast experimentation
Goals
General introduction to Deep Learning
Overview of keras library
An end-to-end example in keras 
Anti-Goals
Understanding of Deep Learning (there will be no equations)
Building neural networks from scratch
Complete survey of keras library
Deep Learning 101
<center><img src="images/neural_nets.jpg" width="75%"/></center>
Deep Learning (DL) are Neural networks (NN) with >1 hidden layer
<center><img src="images/neural-networks-layers.jpg" width="80%"/></center>
Neural Networks are Nodes & Edges
<center><img src="images/sum.png" width="75%"/></center>
Nonlinear function allows learning of nonlinear relationships
<center><img src="images/function_3.png" width="80%"/></center>
Groups of nodes all the way down
<center><img src="images/layers.png" width="75%"/></center>
Deep Learning isn't magic, it is just very good at finding patterns
<center><img src="images/features.png" width="80%"/></center>
Deep Learning has fewer steps than traditional Machine Learning
<center><img src="images/traditional-ml-deep-learning-2.png" width="100%"/></center>
If you want to follow along…
GitHub repo: bit.ly/pybay-keras
If you want to type along…
Run a local Jupyter Notebook
Binder: In-Browser Jupyter Notebook
Colaboratory: "Google Docs for Jupyter Notebooks"
End of explanation
# Import data
from keras.datasets import mnist
# Setup train and test splits
(x_train, y_train), (x_test, y_test) = mnist.load_data()
from random import randint
from matplotlib import pyplot
%matplotlib inline
pyplot.imshow(x_train[randint(0, x_train.shape[0])], cmap='gray_r');
Explanation: <center><img src="images/tf_logo.jpg" width="70%"/></center>
"An open-source software library for Machine Intelligence"
Numerical computation using data flow graphs. 
TensorFlow: A great backend
A very flexible architecture which allows you to do almost any numerical operation.
Then deploy the computation to CPUs or GPUs (one or more) across desktop, cloud, or mobile device. 
<center><img src="images/tf_features.png" width="38%"/></center>
MNIST handwritten digit database: <br> The “Hello World!” of Computer Vision
<center><img src="images/mnist-digits.png" width="80%"/></center>
<center><img src="images/MNIST-Matrix.png" width="100%"/></center>
<center><img src="images/MNIST_neuralnet_image.png" width="100%"/></center>
End of explanation
# Munge Data
# Transform from matrix to vector, cast, and normalize
image_size = 784 # 28 x 28
x_train = x_train.reshape(x_train.shape[0], image_size) # Transform from matrix to vector
x_train = x_train.astype('float32') # Cast as 32 bit integers
x_train /= 255 # Normalize inputs from 0-255 to 0.0-1.0
x_test = x_test.reshape(x_test.shape[0], image_size) # Transform from matrix to vector
x_test = x_test.astype('float32') # Cast as 32 bit integers
x_test /= 255 # Normalize inputs from 0-255 to 0.0-1.0
# Convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
# Import the most common type of neural network
from keras.models import Sequential
Explanation: Munge data
<center><img src="images/mnist_keras.png" width="75%"/></center>
Convert image matrix into vector to feed into first layer
End of explanation
# Define model instance
model = Sequential()
# Import the most common type of network layer, fully interconnected
from keras.layers import Dense
Explanation: RTFM - https://keras.io/layers/
End of explanation
# Define input layer
layer_input = Dense(units=512,            # Number of nodes
                    activation='sigmoid', # The nonlinearity
                    input_shape=(image_size,)) 
model.add(layer_input)
# Define another layer
model.add(Dense(units=512, activation='sigmoid'))
# Define output layers
layer_output = Dense(units=10,             # Number of digits (0-9)
                     activation='softmax') # Convert neural activation to probability of category
model.add(layer_output)
# Print summary
model.summary()
# Yes - we compile the model to run it
model.compile(loss='categorical_crossentropy', 
              optimizer='sgd',
              metrics=['accuracy'])
# Train the model
training = model.fit(x_train, 
                     y_train,
                     epochs=5, # Number of passes over complete dataset
                     verbose=True, 
                     validation_split=0.1)
Explanation: <center><img src="images/dense.png" width="55%"/></center>
End of explanation
# Let's see how well our model performs
loss, accuracy = model.evaluate(x_test, 
                                y_test, 
                                verbose=True)
print(f"Test loss: {loss:.3}")
print(f"Test accuracy: {accuracy:.3%}")
Explanation: <center><img src="images/waiting.jpg" width="55%"/></center>
End of explanation
# reset -fs
# from keras import *
# whos
# from keras.datasets import fashion_mnist
# # Setup train and test splits
# (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
# from random import randint
# from matplotlib import pyplot
# %matplotlib inline
# pyplot.imshow(x_train[randint(0, x_train.shape[0])], cmap='gray_r');
# # Define CNN model
# # Redefine input dimensions to make sure conv works
# img_rows, img_cols = 28, 28
# x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
# x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
# input_shape = (img_rows, img_cols, 1)
# import keras
# # Convert class vectors to binary class matrices
# y_train = keras.utils.to_categorical(y_train, 10)
# y_test = keras.utils.to_categorical(y_test, 10)
# from keras.layers import Conv2D, Dense, Flatten, MaxPooling2D
# # Define model
# model = Sequential()
# model.add(Conv2D(32, 
#              kernel_size=(3, 3),
#              activation='sigmoid',
#              input_shape=input_shape))
# model.add(Conv2D(64, (3, 3), activation='sigmoid'))
# model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Flatten())
# model.add(Dense(128, activation='sigmoid'))
# model.add(Dense(10, activation='softmax'))
# model.compile(loss='categorical_crossentropy', 
#               optimizer='adam',
#               metrics=['accuracy'])
# # Define training
# training = model.fit(x_train, 
#                      y_train,
#                      epochs=5,
#                      verbose=True, 
#                      validation_split=0.1)
# loss, accuracy = model.evaluate(x_test, 
#                                 y_test, 
#                                 verbose=True)
# print(f"Test loss: {loss:.3}")
# print(f"Test accuracy: {accuracy:.3%}")
Explanation: Keras' Other Features
Common built-in functions (e.g., activation functions and optimitizers)
Convolutional neural network (CNN or ConvNet)
Recurrent neural network (RNN) & Long-short term memory (LSTM)
Pre-trained models
Summary
Keras is designed for human beings, not computers.
Easier to try out Deep Learning (focus on the what, not the how).
Simple to define neural networks.
<center><img src="images/twitter.png" width="100%"/></center>
Futher Study - Keras
Keras docs
Keras blog
Keras courses
edX
Coursera
Futher Study - Deep Learning
Prerequisites: Linear Algebra, Probability, Machine Learning
fast.ai Course
Deep Learning Book
<br>
Bonus Material
End of explanation
<END_TASK>