Skip to content

Just creating a function splitTestTrainFromLast in util.py and moving the shuffle and splitting of datasets there. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions ann.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import numpy as np
import matplotlib.pyplot as plt

from util import getData, softmax, cost2, y2indicator, error_rate, relu
from sklearn.utils import shuffle
from util import getData, softmax, cost2, y2indicator, error_rate, relu,splitTrainTestFromLast


class ANN(object):
Expand All @@ -11,10 +10,8 @@ def __init__(self, M):

# learning rate 10e-6 is too large
def fit(self, X, Y, learning_rate=10e-7, reg=10e-7, epochs=10000, show_fig=False):
X, Y = shuffle(X, Y)
Xvalid, Yvalid = X[-1000:], Y[-1000:]
# Tvalid = y2indicator(Yvalid)
X, Y = X[:-1000], Y[:-1000]
Xvalid,Yvalid,X,Y = splitTrainTestFromLast(X,Y,1000)

N, D = X.shape
K = len(set(Y))
Expand Down Expand Up @@ -70,7 +67,7 @@ def score(self, X, Y):

def main():
X, Y = getData()

model = ANN(200)
model.fit(X, Y, reg=0, show_fig=True)
print model.score(X, Y)
Expand Down
6 changes: 5 additions & 1 deletion util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import pandas as pd

from sklearn.utils import shuffle

def init_weight_and_bias(M1, M2):
W = np.random.randn(M1, M2) / np.sqrt(M1 + M2)
Expand Down Expand Up @@ -121,3 +121,7 @@ def crossValidation(model, X, Y, K=5):
errors.append(err)
print "errors:", errors
return np.mean(errors)

def splitTrainTestFromLast(X,Y,N):
X,Y = shuffle(X,Y)
return (X[-N:],Y[-N:],X[:-N],Y[:-N])