-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import math | ||
import numpy as np | ||
from sklearn import datasets | ||
import matplotlib.pyplot as plt | ||
from scratch_ml.deep_learning.optimizers import Adam | ||
from scratch_ml.deep_learning import NeuralNetwork | ||
from scratch_ml.deep_learning.layers import Dense, Dropout, Conv2D, Flatten, Activation, BatchNormalization | ||
from scratch_ml.utils import to_categorical, train_test_split, Plot, CrossEntropy | ||
|
||
|
||
def main(): | ||
|
||
data = datasets.load_digits() | ||
X = data.data | ||
y = data.target | ||
|
||
# Convert to one-hot encoding | ||
y = to_categorical(y.astype("int")) | ||
X_train, X_test, y_train, y_test = train_test_split( | ||
X, y, seed=1) | ||
|
||
# Reshape X to (n_samples, channels, height, width) | ||
X_train = X_train.reshape((-1, 1, 8, 8)) | ||
X_test = X_test.reshape((-1, 1, 8, 8)) | ||
|
||
optimizer = Adam() | ||
Model = NeuralNetwork(optimizer=optimizer, | ||
loss=CrossEntropy, | ||
validation_data=(X_test, y_test)) | ||
|
||
Model.add(Conv2D(n_filters=16, filter_shape=(3, 3), | ||
stride=1, input_shape=(1, 8, 8), padding='same')) | ||
Model.add(Activation("relu")) | ||
Model.add(Dropout(0.25)) | ||
Model.add(BatchNormalization()) | ||
Model.add(Conv2D(n_filters=32, filter_shape=( | ||
3, 3), stride=1, padding="same")) | ||
Model.add(Activation("relu")) | ||
Model.add(Dropout(0.25)) | ||
Model.add(BatchNormalization()) | ||
Model.add(Flatten()) | ||
Model.add(Dense(256)) | ||
Model.add(Activation("relu")) | ||
Model.add(Dropout(0.4)) | ||
Model.add(BatchNormalization()) | ||
Model.add(Dense(10)) | ||
Model.add(Activation("softmax")) | ||
Model.summary(name="CNN") | ||
|
||
train_err, val_err = Model.fit( | ||
X_train, y_train, n_epochs=50, batch_size=256) | ||
n = len(train_err) | ||
training, = plt.plot(range(n), train_err, label="Training Error") | ||
validation, = plt.plot(range(n), val_err, label="Validation Error") | ||
plt.legend(handles=[training, validation]) | ||
plt.title("Error Plot") | ||
plt.ylabel("Error") | ||
plt.xlabel("Iterations") | ||
plt.show() | ||
|
||
_, accuracy = Model.test_on_batch(X_test, y_test) | ||
print("Accuracy:", accuracy) | ||
y_pred = np.argmax(Model.predict(X_test), axis=1) | ||
X_test = X_test.reshape(-1, 8*8) | ||
|
||
Plot().plot_2d(X_test, y_pred, title="Convolutional Neural Network", | ||
accuracy=accuracy, legend_label=range(10)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import math | ||
import numpy as np | ||
from sklearn import datasets | ||
import matplotlib.pyplot as plt | ||
from scratch_ml.deep_learning.optimizers import Adam | ||
from scratch_ml.deep_learning import NeuralNetwork | ||
from scratch_ml.deep_learning.layers import Dense, Dropout, Conv2D, Flatten, Activation, BatchNormalization | ||
from scratch_ml.utils import to_categorical, train_test_split, CrossEntropy | ||
|
||
|
||
class GAN(): | ||
"""Generative Adversarial Network with fully connected neural nets as | ||
Generator and Discriminator.""" | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def generator(): | ||
pass | ||
|
||
def discriminator(): | ||
pass | ||
|
||
|
||
if __name__ == '__main__': | ||
gan = GAN() |