Keras — Fundamentals for Deep Learning

Source: Deep Learning on Medium


Go to the profile of Sonish S

The main structure in Keras is the Model which defines the complete graph of a network. You can add more layers to an existing model to build a custom model that you need for your project.

Here’s how to make a Sequential Model and a few commonly used layers in deep learning

1. Sequential Model

from keras.models import Sequential
from keras.layers import Dense, Activation,Conv2D,MaxPooling2D,Flatten,Dropout
model = Sequential()

2. Convolutional Layer

This is an example of convolutional layer as the input layer with the input shape of 320x320x3, with 48 filters of size 3×3 and use ReLU as an activation function.

input_shape=(320,320,3) #this is the input shape of an image 320x320x3
model.add(Conv2D(48, (3, 3), activation='relu', input_shape= input_shape))

another type is

model.add(Conv2D(48, (3, 3), activation='relu'))

3. MaxPooling Layer

To downsample the input representation, use MaxPool2d and specify the kernel size

model.add(MaxPooling2D(pool_size=(2, 2)))

4. Dense Layer

adding a Fully Connected Layer with just specifying the output Size

model.add(Dense(256, activation='relu'))

5. Dropout Layer

Adding dropout layer with 50% probability

model.add(Dropout(0.5))

Compiling, Training, and Evaluate

After we define our model, let’s start to train them. It is required to compile the network first with the loss function and optimizer function. This will allow the network to change weights and minimized the loss.

model.compile(loss='mean_squared_error', optimizer='adam')

Now to start training, use fit to fed the training and validation data to the model. This will allow you to train the network in batches and set the epochs.

model.fit(X_train, X_train, batch_size=32, epochs=10, validation_data=(x_val, y_val))

Our final step is to evaluate the model with the test data.

score = model.evaluate(x_test, y_test, batch_size=32)

Lets try using simple linear regression

import keras
from keras.models import Sequential
from keras.layers import Dense, Activation
import numpy as np
import matplotlib.pyplot as plt

x = data = np.linspace(1,2,200)
y = x*4 + np.random.randn(*x.shape) * 0.3

model = Sequential()
model.add(Dense(1, input_dim=1, activation='linear'))
model.compile(optimizer='sgd', loss='mse', metrics=['mse'])
weights = model.layers[0].get_weights()
w_init = weights[0][0][0]
b_init = weights[1][0]
print('Linear regression model is initialized with weights w: %.2f, b: %.2f' % (w_init, b_init))

model.fit(x,y, batch_size=1, epochs=30, shuffle=False)
weights = model.layers[0].get_weights()
w_final = weights[0][0][0]
b_final = weights[1][0]
print('Linear regression model is trained to have weight w: %.2f, b: %.2f' % (w_final, b_final))
predict = model.predict(data)
plt.plot(data, predict, 'b', data , y, 'k.')
plt.show()

After training the data, the output should look like this

with the initial weight

Linear regression model is initialized with weights w: 0.37, b: 0.00

and final weight

Linear regression model is trained to have weight w: 3.70, b: 0.61