Last active
September 10, 2019 00:18
-
-
Save crodriguez1a/b752e4d5fc60c02b5afd89e7f9dad833 to your computer and use it in GitHub Desktop.
Single Perceptron (Shallow Network)
This file contains hidden or 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
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import confusion_matrix | |
from sklearn.preprocessing import LabelEncoder | |
import keras | |
from keras.models import Sequential | |
from keras.layers import Dropout, Flatten, Dense, BatchNormalization, Activation | |
from keras import metrics | |
from keras import backend as K | |
# Encode categorical data | |
labelencoder:LabelEncoder = LabelEncoder() | |
y = labelencoder.fit_transform(y) | |
# 80/20 split | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0) | |
# GPU when available | |
K.tensorflow_backend._get_available_gpus() | |
# Initialize network | |
model:Sequential = Sequential() | |
# Single Perceptron/Shallow Network Architecture | |
model.add(Dense(use_bias=False, units=1, kernel_initializer="uniform", input_shape=(128, 30))) | |
# Regulate values going into each activation function | |
model.add(BatchNormalization()) | |
# ELU activation | |
# ref: https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#elu | |
model.add(Activation("elu")) | |
# Add dropout to prevent overfitting | |
model.add(Dropout(rate=0.1)) | |
# Add output layer | |
model.add(Dense(use_bias=False, units=1, kernel_initializer="uniform")) | |
model.add(BatchNormalization()) | |
model.add(Activation("sigmoid")) | |
# Summarize | |
model.summary() | |
# Define loss and optimizer | |
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # binary_crossentropy | |
# Fit training set | |
model.fit(X_train, y_train, batch_size=100, epochs=150) | |
# Predicting on test set | |
y_pred = model.predict(X_test) | |
y_pred = (y_pred > 0.5) | |
# Confusion matrix | |
cm = confusion_matrix(y_test, y_pred) | |
print(cm) | |
# TODO: add references (confusion matrix), type hints |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment