Lecture07. ANN (Chapter 10-2)
Lecture07. ANN (Chapter 10-2)
Lecture 7
MLP or ANNs
Chapter 10-2
Fall 2024
ANNs: artificial neural networks
Outline:
Perceptron
MLPs: Multilayer Perceptrons == ANNs
Classification
Regression
-----------
Playground
https://playground.tensorflow.org
Keras or Pytorch
Tensorbaord
Short History
1958: Perceptron (linear model)
1969: Perceptron has limitation
1980s: Multi-layer perceptron
Do not have significant difference from DNN today
1986: Backpropagation
3
Toward Deep Learning
After year 2000: More data and more computing power, ANN->DNN
2009: GPU
2011: Start to be popular in speech recognition
2012: Win ILSVRC image competition
2014-2015: Alpha Go
2022 ChatGPT
Now: Deep Learning =
Lots of training data + Parallel
Computation + Scalable, smart algorithms
(transformer)
4
ANN: Why Deep
5
Shallow vs Deep NNs
Shallow vs Deep NNs?
Deep is better
Now Deep Learning uses ANNs as the very core of Deep Learning
is built around ANNs, which form the core of its architecture
Is versatile, powerful, and scalable.
6
Deep Models of Recent years
7
1. Sk-learn ANN
Classifier:
https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPCl
assifier.html
Keras is a high-level Deep Learning API that allows you to easily build,
train, evaluate, and execute all sorts of neural networks.
APIs:
1. Sequential API: easy but limited
2. Functional API:
more complex topologies, or
with multiple inputs or outputs.
9
2.1 Sequential API:
Sequential API code:
model = keras.models.Sequential([
keras.layers.Flatten(input_shape=[28, 28]),
keras.layers.Dense(300, activation="relu"),
keras.layers.Dense(100, activation="relu"),
keras.layers.Dense(10, activation="softmax")
])
10
2.2 Building Complex Models
Using the Functional API
use function:
Wide + deep model
Use all features twice
input_ = keras.layers.Input(shape=X_train.shape[1:])
hidden1 = keras.layers.Dense(30,activation="relu")(input_)
hidden2 = keras.layers.Dense(30, activation="relu")(hidden1)
concat = keras.layers.Concatenate()([input_, hidden2])
output = keras.layers.Dense(1)(concat)
model = keras.Model(inputs=[input_], outputs=[output])
11
Handling multiple inputs: split features
12
Handling multiple inputs
input_A = keras.layers.Input(shape=[5],name="wide_input")
input_B = keras.layers.Input(shape=[6],name="deep_input")
hidden1 = keras.layers.Dense(30,activation="relu")(input_B)
hidden2 = keras.layers.Dense(30, activation="relu")(hidden1)
concat = keras.layers.concatenate([input_A, hidden2])
output = keras.layers.Dense(1, name="output")(concat)
model = keras.Model(inputs=[input_A, input_B], outputs=[output])
13
Multi-output
14
5 Step Life-Cycle for Neural Network
Models in Keras
15
Saving and Restoring a Model
model.save("my_keras_model.h5")
model = keras.models.load_model("my_keras_model.h5")
17
Using Callbacks
18
Callback example
checkpoint_cb =
keras.callbacks.ModelCheckpoint("my_keras_model.h5",
save_best_only=True)
model = keras.models.load_model("my_keras_model.h5")
19
Early Stopping: over epochs
Train the model many epochs:
if val_error < minimum_val_error:
20
Early stopping
early_stopping_cb =
keras.callbacks.EarlyStopping(patience=10,
restore_best_weights=True)
1. Classification
2. Regression
3. Complex model
4. Save and restore
As you can see, you can build any sort of architecture you want quite
easily with the Functional API. Let’s look at one last way you can build
Keras models.
22
Parameter Tuning
Grid Search (P321, might take hours)
autoML (both best structure and parameters)
https://machinelearningmastery.com/automl-libraries-for-python/
Wide or Deep:
An MLP with just one hidden layer can theoretically model even the most
complex functions, provided it has enough neurons.
But for complex problems, deep networks have a much higher parameter
efficiency than shallow ones: they can model complex functions using
exponentially fewer neurons than shallow nets, allowing them to reach
much better performance with the same amount of training data.
Single Perceptron
MLP: ANN for Classification/Regression
Train: Back-propagations
Keras:
Sequential/Functional API
Save and Restore model
Callback to early stop, find the best model
24
-- END --
Next Week:
Oct. 16: Naïve Byes
Homework
HW4.Part A: due by this Friday
HW4.Part B: due in two weeks (same day as the
Midterm)
25
Mid-term: Oct. 23
Mid-term: Oct. 23
Lecture time
2.5 hrs (all lectures)
100 points
paper-pencil, closed-book
Materials: Lecture 1-7
26