-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from michabirklbauer/develop
add wf and tests
- Loading branch information
Showing
4 changed files
with
144 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions | ||
# Reference workflow provided by (c) GitHub | ||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions | ||
|
||
name: neuralnet | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ['3.7', '3.8', '3.9', '3.10'] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install flake8 pytest | ||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi | ||
- name: Lint with flake8 | ||
run: | | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics | ||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide | ||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics | ||
- name: Test with pytest | ||
run: | | ||
cp neuralnet.py tests/neuralnet.py | ||
pytest tests/tests.py |
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
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,99 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# NEURAL NETWORK IMPLEMENTATION - TESTS | ||
# 2022 (c) Micha Johannes Birklbauer | ||
# https://github.com/michabirklbauer/ | ||
# micha.birklbauer@gmail.com | ||
|
||
def test_bcc(): | ||
|
||
#### Binary-class Classification #### | ||
|
||
from zipfile import ZipFile as zip | ||
|
||
with zip("data.zip") as f: | ||
f.extractall() | ||
f.close() | ||
|
||
from neuralnet import NeuralNetwork | ||
import numpy as np | ||
import pandas as pd | ||
from sklearn.metrics import accuracy_score | ||
from sklearn.preprocessing import OneHotEncoder | ||
from sklearn.model_selection import train_test_split | ||
|
||
data = pd.read_csv("binaryclass_train.csv", header = None) | ||
data["label"] = data[1].apply(lambda x: 1 if x == "M" else 0) | ||
train, test = train_test_split(data, test_size = 0.3) | ||
train_data = train.loc[:, ~train.columns.isin([0, 1, "label"])].to_numpy() | ||
train_target = train["label"].to_numpy() | ||
test_data = test.loc[:, ~test.columns.isin([0, 1, "label"])].to_numpy() | ||
test_target = test["label"].to_numpy() | ||
|
||
NN = NeuralNetwork(input_size = train_data.shape[1]) | ||
NN.add_layer(16, "relu") | ||
NN.add_layer(16, "relu") | ||
NN.add_layer(1, "sigmoid") | ||
NN.compile(loss = "binary crossentropy") | ||
NN.summary() | ||
|
||
hist = NN.fit(train_data, train_target, epochs = 1000, batch_size = 32, learning_rate = 0.01) | ||
|
||
train_predictions = np.round(NN.predict(train_data)) | ||
train_acc = accuracy_score(train["label"].to_numpy(), train_predictions) | ||
test_predictions = np.round(NN.predict(test_data)) | ||
test_acc = accuracy_score(test["label"].to_numpy(), test_predictions) | ||
|
||
import os | ||
os.remove("binaryclass_train.csv") | ||
os.remove("multiclass_train.csv") | ||
|
||
assert train_acc > 0.85 and test_acc > 0.85 | ||
|
||
def test_mcc(): | ||
|
||
#### Multi-class Classification #### | ||
|
||
from zipfile import ZipFile as zip | ||
|
||
with zip("data.zip") as f: | ||
f.extractall() | ||
f.close() | ||
|
||
from neuralnet import NeuralNetwork | ||
import numpy as np | ||
import pandas as pd | ||
from sklearn.metrics import accuracy_score | ||
from sklearn.preprocessing import OneHotEncoder | ||
from sklearn.model_selection import train_test_split | ||
|
||
data = pd.read_csv("multiclass_train.csv") | ||
train, test = train_test_split(data, test_size = 0.3) | ||
train_data = train.loc[:, train.columns != "label"].to_numpy() / 255 | ||
train_target = train["label"].to_numpy() | ||
test_data = test.loc[:, test.columns != "label"].to_numpy() / 255 | ||
test_target = test["label"].to_numpy() | ||
|
||
one_hot = OneHotEncoder(sparse = False, categories = "auto") | ||
train_target = one_hot.fit_transform(train_target.reshape(-1, 1)) | ||
test_target = one_hot.transform(test_target.reshape(-1, 1)) | ||
|
||
NN = NeuralNetwork(input_size = train_data.shape[1]) | ||
NN.add_layer(32, "relu") | ||
NN.add_layer(16, "relu") | ||
NN.add_layer(10, "softmax") | ||
NN.compile(loss = "categorical crossentropy") | ||
NN.summary() | ||
|
||
hist = NN.fit(train_data, train_target, epochs = 30, batch_size = 16, learning_rate = 0.05) | ||
|
||
train_predictions = np.argmax(NN.predict(train_data), axis = 1) | ||
train_acc = accuracy_score(train["label"].to_numpy(), train_predictions) | ||
test_predictions = np.argmax(NN.predict(test_data), axis = 1) | ||
test_acc = accuracy_score(test["label"].to_numpy(), test_predictions) | ||
|
||
import os | ||
os.remove("binaryclass_train.csv") | ||
os.remove("multiclass_train.csv") | ||
|
||
assert train_acc > 0.85 and test_acc > 0.85 |