Basic Neural Networks
Duration: 7 min
This module introduces the fundamentals of neural networks, focusing on their architecture, training process, and implementation using TensorFlow and Keras. Understanding basic neural networks is crucial as they form the foundation for more complex models like CNNs and RNNs.
Understanding Neural Network Architecture
A neural network is composed of layers of interconnected nodes or neurons. Each neuron receives inputs, applies a weight to each input, sums them, and passes the result through an activation function. The architecture typically includes an input layer, one or more hidden layers, and an output layer.
import tensorflow as tf
from tensorflow.keras import layers, models
# Define a simple neural network
model = models.Sequential([
layers.Dense(32, activation='relu', input_shape=(784,)),
layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])Model: "sequential"
__________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 32) 25120
__________________________________________________________________
dense_1 (Dense) (None, 10) 330
=================================================================
Total params: 25,450
Trainable params: 25,450
Non-trainable params: 0
__________________________________________________________________
Training a Neural Network
Training a neural network involves feeding it input data, allowing it to make predictions, comparing these predictions to the actual outcomes, and adjusting the weights to minimize the difference. This process is repeated over multiple epochs until the model achieves satisfactory performance.
import numpy as np
# Generate dummy data
x_train = np.random.random((1000, 784))
y_train = np.random.randint(10, size=(1000, 1))
x_test = np.random.random((100, 784))
y_test = np.random.randint(10, size=(100, 1))
# Train the model
model.fit(x_train, y_train, epochs=5, batch_size=32)
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('Test accuracy:', test_acc)💡 Tip: Always ensure your input data is properly preprocessed and normalized before training to improve model performance and convergence.
❓ What is the role of an activation function in a neural network?
❓ Which method is used to adjust the weights of a neural network during training?