Neural Networks (Deep Learning) Example

This is a simple example of Neural Networks (Deep Learning) using Python and TensorFlow/Keras.

Neural Networks Overview

Neural Networks, a fundamental component of deep learning, are powerful models inspired by the human brain's structure and functioning. They consist of interconnected layers of artificial neurons that transform input data into meaningful output. Deep Learning refers to the use of deep neural networks with multiple layers, enabling the model to learn complex hierarchical representations of data.

Key concepts of Neural Networks:

Deep Learning has achieved remarkable success in various domains, including image and speech recognition, natural language processing, and more. TensorFlow and Keras provide user-friendly interfaces for building and training neural networks.

Python Source Code:

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import mean_squared_error

# Generate synthetic data
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Build a Sequential model
model = Sequential([
    Dense(10, activation='relu', input_shape=(1,)),
    Dense(1)
])

# Compile the model
model.compile(optimizer=Adam(learning_rate=0.01), loss='mean_squared_error')

# Train the model
history = model.fit(X_train, y_train, epochs=50, verbose=0)

# Make predictions on the test set
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {mse}')

# Plot the results
plt.scatter(X_test, y_test, color='black')
plt.scatter(X_test, y_pred, color='red', marker='x')
plt.title('Neural Networks (Deep Learning) Example')
plt.xlabel('X')
plt.ylabel('y')
plt.show()

Explanation: