Created
January 27, 2018 11:33
-
-
Save Orbifold/65c6a49db71e625b04d4eb066904319d to your computer and use it in GitHub Desktop.
Running experiments to improve accuracy without grid-search or alike.
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
import argparse | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from keras.layers.core import Dense | |
from keras.models import Sequential | |
from numpy import array | |
from scipy import signal | |
def parse_arguments(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--epochs', type=int, nargs='+', | |
help='The epoches to run', required=True) | |
parser.add_argument('--mode', help='The mode: azure or python', | |
choices=['python', 'azure'], default='python') | |
args = parser.parse_args() | |
# will always be there since it's a required argument | |
epochs = np.array(args.epochs) | |
epochs_string = ' '.join(str(x) for x in epochs) | |
return epochs | |
def main(): | |
epochs = parse_arguments() | |
if len(epochs) == 0: | |
print("Nothing to do: no epochs given.") | |
else: | |
accs = list() | |
for i in epochs: | |
print('Running with %s epochs.' % i) | |
exp = Experiment() | |
acc = exp.learn(i) | |
accs.append(acc) | |
plt.plot(epochs, accs, alpha=0.5) | |
plt.show() | |
class Experiment(): | |
def __init__(self): | |
self.N = 10 | |
self.model = self.create_model() | |
self.X, self.y = self.create_data(1000) | |
self.X_test, self.y_test = self.create_test() | |
def create_model(self): | |
model = Sequential() | |
model.add(Dense(1, input_shape=(1, self.N))) | |
model.add(Dense(10)) | |
model.add(Dense(1)) | |
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['acc']) | |
return model | |
def create_data(self, n): | |
X = list() | |
y = list() | |
for i in range(n): | |
X.append(array([array(signal.unit_impulse(self.N, i % self.N))])) | |
y.append(array([array([i % self.N])])) | |
return array(X), array(y) | |
def create_test(self): | |
a = array([0, 0, 4, 5, 5, 6, 8, 4, 1, 4, 9, 8, 1, 1, 7, 9, 9, 3, 6, 7]) | |
X = list() | |
y = list() | |
for i in a: | |
X.append(array([array(signal.unit_impulse(self.N, i % self.N))])) | |
y.append(array([array([i % self.N])])) | |
return array(X), array(y) | |
def learn(self, epochs): | |
self.model.fit(self.X, self.y, epochs=epochs, verbose=0) | |
e = self.model.evaluate(self.X_test, self.y_test, verbose=0) | |
print("%s: %.0f%%" % (self.model.metrics_names[1], e[1] * 100)) | |
return e[1] | |
# python run_basic.py --epochs 5 10 15 20 25 30 40 50 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment