Use TensorFlow if you want a finer level of control:
We will be mostly writing python code using Keras libraries, but "under the hood" Keras is using tensorflow libraries.
The documentation is at keras.io.
The simplest kind of model is of the Sequential kind:
from tensorflow.keras.models import Sequential
model = Sequential()
This is an "empty" model, with no layers, no inputs or outputs are defined either.
Adding layer is easy:
from tensorflow.keras.layers import Dense
model.add(Dense(units=3, activation='relu', input_dim=3))
model.add(Dense(units=2, activation='softmax'))
A "Dense" layer is a fully connected layer as the ones we have seen in Multi-layer Perceptrons. The above is equal to having this network:
If we want to see the layers in the Model this far, we can just call:
model.summary()
Using "model.add()" keeps stacking layers on top of what we have:
model.add(Dense(units=2, activation=None))
model.summary()
Common layers (we will cover most of these!)
Trainable
Non-trainable
model.add(Dropout(0.5))
import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import Dropout
from tensorflow.keras import backend as K
tf.random.set_seed(1)
drop = Dropout(0.5, input_shape=(4,))
data = tf.reshape(tf.range(1.0,13.0), (3, 4))
print("Before:", data, sep="\n")
output = drop(data, training=True)
print("After:", K.eval(output), sep="\n")
Before: tf.Tensor( [[ 1. 2. 3. 4.] [ 5. 6. 7. 8.] [ 9. 10. 11. 12.]], shape=(3, 4), dtype=float32) After: [[ 0. 4. 6. 0.] [ 0. 12. 14. 0.] [18. 20. 22. 24.]]
2023-03-20 07:58:31.056981: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set 2023-03-20 07:58:31.057209: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: SSE4.1 SSE4.2 AVX AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2023-03-20 07:58:31.058599: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
import numpy as np
from tensorflow.keras.layers import Dropout
from tensorflow.keras import backend as K
#f.random.set_seed(0)
drop = Dropout(0.5, input_shape=(4,))
data = tf.reshape(tf.range(1.0,13.0), (3, 4))
print("Before:", data, sep="\n")
output = drop(data, training=True)
print("After:", K.eval(output), sep="\n")
Before: tf.Tensor( [[ 1. 2. 3. 4.] [ 5. 6. 7. 8.] [ 9. 10. 11. 12.]], shape=(3, 4), dtype=float32) After: [[ 2. 0. 0. 8.] [10. 0. 0. 16.] [ 0. 20. 0. 24.]]
from tensorflow.keras.layers import Lambda
from tensorflow.keras import backend as K
def sum_two_tensors(inputs):
x, y = inputs
sum_of_tensors = x + y
return sum_of_tensors
input_tensor_1 = tf.range(0, 9)
input_tensor_2 = tf.range(1, 10)
print(input_tensor_1)
print(input_tensor_2)
#lambda_out = Lambda(sum_two_tensors)([input_tensor_1, input_tensor_2])
lambda_layer = Lambda(sum_two_tensors)
lambda_out = lambda_layer([input_tensor_1, input_tensor_2])
K.eval(lambda_out)
#model.add(Lambda(sum_two_tensors))
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32) tf.Tensor([1 2 3 4 5 6 7 8 9], shape=(9,), dtype=int32)
array([ 1, 3, 5, 7, 9, 11, 13, 15, 17], dtype=int32)
Activation functions for regression or inner layers:
THE activation function for classification (output layer only):
It's an activation function applied to a output vector z with K elements (one per class) and outputs a probability distribution over the classes:
What makes softmax your favorite activation:
Softmax is usually only used to activate the last layer of a NN
Used in "internal" layers, usually not at last layer
Pros:
Cons:
from IPython.display import IFrame
IFrame('https://polarisation.github.io/tfjs-activation-functions/', width=860, height=470)
We can add activations as string parameters, or as functions:
model = Sequential()
model.add(Dense(units=2, activation='sigmoid'))
model.add(Dense(units=2, activation='relu'))
model.add(Dense(units=2, activation=tf.keras.activations.relu))
model.add(Dense(units=2, activation='softmax'))
But also as separate layers
import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense
model = Sequential()
model.add(Dense(units=2))
model.add(Activation('sigmoid'))
model.add(Dense(units=2))
model.add(Activation('relu'))
model.add(Dense(units=2))
model.add(Activation(tf.keras.activations.relu))
model.add(Dense(units=2))
model.add(Activation('softmax'))
from tensorflow.keras.optimizers import RMSprop
model.compile(optimizer=RMSprop(), #adaptive learning rate method
loss='sparse_categorical_crossentropy', #loss function for classification problems with integer labels
metrics=['accuracy']) #the metric doesn't influence the training
model.optimizer.get_config()
{'name': 'RMSprop', 'learning_rate': 0.001, 'decay': 0.0, 'rho': 0.9, 'momentum': 0.0, 'epsilon': 1e-07, 'centered': False}
from keras.optimizers import RMSprop
model.compile(optimizer=RMSprop(learning_rate=1.0), #adaptive learning rate method
loss='sparse_categorical_crossentropy', #loss function for classification problems with integer labels
metrics=['accuracy']) #the metric doesn't influence the training
model.optimizer.get_config()
{'name': 'RMSprop', 'learning_rate': 1.0, 'decay': 0.0, 'rho': 0.9, 'momentum': 0.0, 'epsilon': 1e-07, 'centered': False}
data = np.genfromtxt('path/to/dataset.csv',delimiter=',')
X_train = data[:,0:10]
y_train = data[:,10]
model.fit(X_train, y_train,...)
Here a quick example on how a generator that loads loads data from a list of files (images, pickle objects, csv files...) on the filesystem:
def generator(input_list):
input_list_file = open(input_list, 'r')
while 1:
for next_file in input_list_file:
data = open(next_file, 'r').readlines()
X = data[:,0:10]
y = data[:,10]
yield X,y
input_list_file.seek(0)
model.fit(generator(train_data_list),...)
from tensorflow.keras.layers import Conv2D
model.add(Conv2D(filters, kernel_size, strides=(1, 1), padding="valid"))
from tensorflow.keras.layers import LSTM
model.add(LSTM(units, activation="tanh", recurrent_activation="sigmoid"))
[0.1, 0.003, 1.2 ..., 0]
from tensorflow.keras.layers import Embed
model.add(Embedding(input_dim, output_dim))
Classifying IMDB reviews into positive or negative.
Check the exercises notebook!