Skip to content

Commit e4dcab8

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 96c374a commit e4dcab8

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

machine_learning/neural_networks/cnn_mnist.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1313
"""
1414

15-
1615
import tensorflow as tf
1716
from tensorflow.keras import layers, models
1817
from tensorflow.keras.datasets import mnist
@@ -22,8 +21,8 @@
2221
(X_train, y_train), (X_test, y_test) = mnist.load_data()
2322

2423
# Normalize the pixel values (0 to 1)
25-
X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255
26-
X_test = X_test.reshape(-1, 28, 28, 1).astype('float32') / 255
24+
X_train = X_train.reshape(-1, 28, 28, 1).astype("float32") / 255
25+
X_test = X_test.reshape(-1, 28, 28, 1).astype("float32") / 255
2726

2827
# Convert labels to one-hot encoding
2928
y_train = to_categorical(y_train, 10)
@@ -33,38 +32,40 @@
3332
model = models.Sequential()
3433

3534
# 1st Convolutional Layer
36-
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
35+
model.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 1)))
3736
model.add(layers.MaxPooling2D((2, 2)))
3837
model.add(layers.BatchNormalization())
3938

4039
# 2nd Convolutional Layer
41-
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
40+
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
4241
model.add(layers.MaxPooling2D((2, 2)))
4342
model.add(layers.BatchNormalization())
4443

4544
# 3rd Convolutional Layer
46-
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
45+
model.add(layers.Conv2D(128, (3, 3), activation="relu"))
4746
model.add(layers.BatchNormalization())
4847

4948
# Flattening the data before fully connected layers
5049
model.add(layers.Flatten())
5150

5251
# Fully Connected (Dense) Layer with Dropout for regularization
53-
model.add(layers.Dense(128, activation='relu'))
52+
model.add(layers.Dense(128, activation="relu"))
5453
model.add(layers.Dropout(0.5))
5554

5655
# Output Layer for classification
57-
model.add(layers.Dense(10, activation='softmax'))
56+
model.add(layers.Dense(10, activation="softmax"))
5857

5958
# Compile the model
60-
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
59+
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
6160

6261
# Display the model summary
6362
model.summary()
6463

6564
# Train the model
66-
history = model.fit(X_train, y_train, epochs=5, batch_size=32, validation_data=(X_test, y_test))
65+
history = model.fit(
66+
X_train, y_train, epochs=5, batch_size=32, validation_data=(X_test, y_test)
67+
)
6768

6869
# Evaluate the model on test data
6970
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
70-
print(f'\nTest accuracy: {test_acc}')
71+
print(f"\nTest accuracy: {test_acc}")

machine_learning/neural_networks/fully_connected_mnist.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
- Train the model, evaluate its accuracy and loss at each epoch, and predict sample outputs.
1212
"""
1313

14-
1514
import tensorflow as tf
1615
from tensorflow.keras import layers, models
1716
import numpy as np
@@ -35,26 +34,28 @@
3534
model.add(layers.Flatten(input_shape=(28, 28)))
3635

3736
# First hidden layer with 128 neurons and ReLU activation
38-
model.add(layers.Dense(128, activation='relu'))
37+
model.add(layers.Dense(128, activation="relu"))
3938

4039
# Dropout layer to prevent overfitting (randomly drops 20% of neurons)
4140
model.add(layers.Dropout(0.2))
4241

4342
# Second hidden layer with 64 neurons and ReLU activation
44-
model.add(layers.Dense(64, activation='relu'))
43+
model.add(layers.Dense(64, activation="relu"))
4544

4645
# Output layer with 10 neurons (one for each digit class 0-9), softmax for probabilities
47-
model.add(layers.Dense(10, activation='softmax'))
46+
model.add(layers.Dense(10, activation="softmax"))
4847

4948
# Compile the model
50-
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
49+
model.compile(
50+
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
51+
)
5152

5253
# Train the model on the MNIST training data
5354
model.fit(X_train, y_train, epochs=5, batch_size=32)
5455

5556
# Evaluate the model on the test set
5657
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
57-
print(f'\nTest accuracy: {test_acc}')
58+
print(f"\nTest accuracy: {test_acc}")
5859

5960
# Make a prediction on a random test image
6061
random_index = np.random.randint(0, len(X_test))
@@ -63,4 +64,4 @@
6364
predicted_digit = np.argmax(prediction)
6465

6566
# Print the predicted result and actual label
66-
print(f'Predicted digit: {predicted_digit}, Actual digit: {y_test[random_index]}')
67+
print(f"Predicted digit: {predicted_digit}, Actual digit: {y_test[random_index]}")

0 commit comments

Comments
 (0)