Multi-Layer Perceptron

Multi-Layer Perceptron

Overview of Perceptrons

  • A perceptron is a supervised binary classification algorithm capable of solving only linearly separable problems.

Structure and Activation Functions of Multi-Layer Perceptrons

  • The architecture of a multi-layer perceptron consists of an input layer, one or more hidden layers, and an output layer, enabling it to approximate non-linear functions.
  • An activation function is appplied within neural network units to assist the model in learning complex patterns from data. It maps the input of a neuron to its output.
  • Common activation functions include Sigmoid, Tanh, and ReLU.

Backpropagation Neural Network Algorithm

  • A backpropagation network is a type of multi-layer neural network trained using the error backpropagation method. It performs forward propagation to compute loss and backward propagation to calculate errors, updating weights accordingly based on the error signals.

Softmax Multiclass Classification Using MLP

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

# Generate synthetic data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

model = Sequential()
# Dense(64) represents a fully connected layer with 64 hidden neurons.
# The first layer requires specification of the input shape:
# Here, it's a 20-dimensional vector.
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(learning_rate=0.01, weight_decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
print(score)


Binary Classification Using MLP

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout

# Generate synthetic data
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100, 20))
y_test = np.random.randint(2, size=(100, 1))

model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
print(score)


Convolutional Neural Network Similar to VGG

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD

# Generate synthetic data
x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
x_test = np.random.random((20, 100, 100, 3))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)

model = Sequential()
# Input: 3-channel 100x100 pixel image -> (100, 100, 3) tensor.
# Apply 32 filters of size 3x3.
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(learning_rate=0.01, weight_decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)

model.fit(x_train, y_train, batch_size=32, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=32)
print(score)


Tags: Machine Learning Neural Networks Deep Learning keras TensorFlow

Posted on Mon, 10 Aug 2026 16:00:46 +0000 by quanghoc