From 4d3d0e8a9c366c42c44bc21f78610d2ebb019b77 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Sat, 20 Oct 2018 15:26:59 +0330 Subject: update --- dlstuff/five.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dlstuff/seven.py | 64 ++++++++++++++++++++++++++++++++++++++++++ dlstuff/two.py | 9 ++++-- 3 files changed, 154 insertions(+), 3 deletions(-) create mode 100755 dlstuff/five.py create mode 100755 dlstuff/seven.py diff --git a/dlstuff/five.py b/dlstuff/five.py new file mode 100755 index 0000000..0779e6c --- /dev/null +++ b/dlstuff/five.py @@ -0,0 +1,84 @@ +#!/usr/bin/python3 +# _*_ coding=utf-8 _*_ + +import argparse +import code +import readline +import signal +import sys +import keras +from keras import Input, Model +from keras import layers + +def SigHandler_SIGINT(signum, frame): + print() + sys.exit(0) + +class Argparser(object): + def __init__(self): + parser = argparse.ArgumentParser() + parser.add_argument("--string", type=str, help="string") + parser.add_argument("--bool", action="store_true", help="bool", default=False) + parser.add_argument("--dbg", action="store_true", help="debug", default=False) + self.args = parser.parse_args() + +# write code here +def premain(argparser): + signal.signal(signal.SIGINT, SigHandler_SIGINT) + #here + callbacks_list = [keras.callbacks.EarlyStopping(monitor="acc", patience=1,), keras.callbacks.ModelCheckpoint(filepath="mymodel.h5", monitor="val_loss", save_best_only=True,)] + input_tensor = Input(shape=(64,)) + x = layers.Dense(32, activation="relu")(input_tensor) + x = layers.Dense(32, activation="relu")(x) + output_tensor = layers.Dense(10, activation="softmax")(x) + model = Model(input_tensor, output_tensor) + model.summary() + model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["acc"]) + import numpy as np + x_train = np.random.random((1000, 64)) + y_train = np.random.random((1000, 10)) + x_val = np.random.random((1000, 64)) + y_val = np.random.random((1000, 10)) + model.fit(x_train, y_train, epochs=10, batch_size=128, callbacks=callbacks_list, validation_data=(x_val, y_val)) + score = model.evaluate(x_train, y_train) + print(score) + ''' + text_vocabulary_size = 10000 + question_vocabulary_size = 10000 + answer_vocabulary_size = 500 + text_input = Input(shape=(None, ), dtype="int32", name="text") + embedded_text = layers.Embedding(64, text_vocabulary_size)(text_input) + encoded_text = layers.LSTM(32)(embedded_text) + question_input = Input(shape=(None,), dtype="int32", name="question") + embedded_question = layers.Embedding(32, question_vocabulary_size)(question_input) + encoded_question = layers.LSTM(16)(embedded_question) + concatenated = layers.concatenate([encoded_text, encoded_question], axis=-1) + answer = layers.Dense(answer_vocabulary_size, activatoin="softmax")(concatenated) + model = Model([text_input, question_input], answer) + model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["acc"]) + import numpy as np + num_samples = 1000 + max_length = 100 + text = np.random.randint(1, text_vocabulary_size, size=(num_samples, max_length)) + question = np.random.randint(1, question_vocabulary_size, size=(num_samples, max_length)) + answers = np.random.randint(0, 1, size=(num_samples, answer_vocabulary_size)) + model.fit([text, question], answers, epochs=10, batch_size=128) + ''' + +def main(): + argparser = Argparser() + if argparser.args.dbg: + try: + premain(argparser) + except Exception as e: + print(e.__doc__) + if e.message: print(e.message) + variables = globals().copy() + variables.update(locals()) + shell = code.InteractiveConsole(variables) + shell.interact(banner="DEBUG REPL") + else: + premain(argparser) + +if __name__ == "__main__": + main() diff --git a/dlstuff/seven.py b/dlstuff/seven.py new file mode 100755 index 0000000..12cab94 --- /dev/null +++ b/dlstuff/seven.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 +# _*_ coding=utf-8 _*_ + +import argparse +import code +import readline +import signal +import sys +import keras +from keras import layers +from keras.datasets import imdb +from keras.preprocessing import sequence + +def SigHandler_SIGINT(signum, frame): + print() + sys.exit(0) + +class Argparser(object): + def __init__(self): + parser = argparse.ArgumentParser() + parser.add_argument("--string", type=str, help="string") + parser.add_argument("--bool", action="store_true", help="bool", default=False) + parser.add_argument("--dbg", action="store_true", help="debug", default=False) + self.args = parser.parse_args() + +# write code here +def premain(argparser): + signal.signal(signal.SIGINT, SigHandler_SIGINT) + #here + callbacks = [keras.callbacks.TensorBoard(log_dir="logfiles", histogram_freq=1, embeddings_freq=1,)] + max_features = 2000 + max_len = 500 + (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) + x_train = sequence.pad_sequences(x_train, maxlen=max_len) + x_test = sequence.pad_sequences(x_test, maxlen=max_len) + model = keras.models.Sequential() + model.add(layers.Embedding(max_features, 128, input_length=max_len, name="embed")) + model.add(layers.Conv1D(32, 7, activation="relu")) + model.add(layers.MaxPooling1D(5)) + model.add(layers.Conv1D(32, 7, activation="relu")) + model.add(layers.GlobalMaxPooling1D()) + model.add(layers.Dense(1)) + summary = model.summary() + print(summary) + model.compile(optimizer="rmsprop", loss="binary_crossentropy", metrics=["acc"]) + history = model.fit(x_train, y_train, epochs=20, batch_size=128, validation_split=0.2, callbacks=callbacks) + +def main(): + argparser = Argparser() + if argparser.args.dbg: + try: + premain(argparser) + except Exception as e: + print(e.__doc__) + if e.message: print(e.message) + variables = globals().copy() + variables.update(locals()) + shell = code.InteractiveConsole(variables) + shell.interact(banner="DEBUG REPL") + else: + premain(argparser) + +if __name__ == "__main__": + main() diff --git a/dlstuff/two.py b/dlstuff/two.py index 9eab134..8287708 100755 --- a/dlstuff/two.py +++ b/dlstuff/two.py @@ -10,6 +10,7 @@ from keras.datasets import imdb import numpy as np from keras import models from keras import layers +from keras import regularizers import matplotlib.pyplot as plt def SigHandler_SIGINT(signum, frame): @@ -67,8 +68,10 @@ def premain(argparser): y_test = np.asarray(test_labels).astype("float32") model = models.Sequential() - model.add(layers.Dense(16, activation="relu", input_shape=(10000,))) - model.add(layers.Dense(16, activation="relu")) + model.add(layers.Dense(16, kernel_regularizer=regularizers.l2(0.001), activation="relu", input_shape=(10000,))) + model.add(layers.Dropout(0.5)) + model.add(layers.Dense(16, kernel_regularizer=regularizers.l2(0.001), activation="relu")) + model.add(layers.Dropout(0.5)) model.add(layers.Dense(1, activation="sigmoid")) x_val = x_train[:10000] @@ -85,7 +88,7 @@ def premain(argparser): plot_acc(history) ''' - model.fit(x_train, y_train, epochs=4, batch_size=512) + model.fit(x_train, y_train, epochs=20, batch_size=512) results = model.evaluate(x_test, y_test) print(results) -- cgit v1.2.3