def main_okapi(): from OkapiV2.Core import Model, Branch from OkapiV2.Layers.Basic import FullyConnected, Dropout from OkapiV2.Layers.Activations import ActivationLayer, PReLULayer from OkapiV2.Layers.Convolutional import Convolutional, MaxPooling from OkapiV2 import Activations, Datasets from OkapiV2 import Optimizers X_train, y_train, X_val, y_val, X_test, y_test = Datasets.load_mnist() tree = Branch() tree.add_layer(Convolutional(num_filters, filter_size, filter_size, pad=pad)) # tree.add_layer(BatchNorm()) tree.add_layer(PReLULayer()) tree.add_layer(MaxPooling(pool_size, pool_size)) tree.add_layer(Convolutional(num_filters, filter_size, filter_size, pad=pad)) # tree.add_layer(BatchNorm()) tree.add_layer(PReLULayer()) tree.add_layer(MaxPooling(pool_size, pool_size)) tree.add_layer(Convolutional(num_filters, filter_size, filter_size, pad=pad)) # tree.add_layer(BatchNorm()) tree.add_layer(PReLULayer()) tree.add_layer(MaxPooling(pool_size, pool_size)) '''tree.add_layer(Convolutional(num_filters, filter_size, filter_size, pad=pad)) # tree.add_layer(BatchNorm()) tree.add_layer(PReLULayer()) tree.add_layer(MaxPooling(pool_size, pool_size))''' '''tree.add_layer(Dropout(0.25)) tree.add_layer(FullyConnected((h_size, 1, 1, 1))) tree.add_layer(PReLULayer()) # tree.add_layer(BatchNorm())''' tree.add_layer(Dropout(0.5)) tree.add_layer(FullyConnected()) tree.add_layer(ActivationLayer(Activations.alt_softmax)) tree.add_input(X_train) model = Model() model.set_tree(tree) model.set_optimizer(Optimizers.RMSprop(learning_rate=learning_rate, momentum=momentum)) model.train([X_train], y_train, num_epochs=num_epochs, batch_size=batch_size) # ok.save_model(model, 'okapi_mnist.pk') okapi_accuracy = model.get_accuracy([X_test], y_test) print("Test Accuracy: {}%" .format(round(okapi_accuracy, 2))) return okapi_accuracy
def main_okapi(): import OkapiV2.Core as ok from OkapiV2.Core import Model from OkapiV2.Layers.Basic import FullyConnected, Dropout, BatchNorm from OkapiV2.Layers.Activations import ActivationLayer, PReLULayer from OkapiV2.Layers.Recurrent import LSTM from OkapiV2 import Activations, Optimizers, Losses path = 'data/lear.txt' text = open(path).read().lower() # [0:corpus_length] print('Corpus length:', len(text)) chars = set(text) print('Total Characters:', len(chars)) char_to_index = dict((c, i) for i, c in enumerate(chars)) index_to_char = dict((i, c) for i, c in enumerate(chars)) # cut the text in semi-redundant sequences of maxlen characters sentences = [] next_chars = [] for i in range(0, len(text) - maxlen, step): sentences.append(text[i: i + maxlen]) next_chars.append(text[i + maxlen]) print('Total Sequences:', len(sentences)) print('Vectorization...') X = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) y = np.zeros((len(sentences), len(chars)), dtype=np.bool) for i, sentence in enumerate(sentences): for t, char in enumerate(sentence): X[i, t, char_to_index[char]] = 1 y[i, char_to_index[next_chars[i]]] = 1 def sample(a, temperature=1.0): # helper function to sample an index from a probability array a = np.log(a) / temperature a = np.exp(a) / np.sum(np.exp(a)) - 1e-7 return np.argmax(np.random.multinomial(1, a, 1)) model = Model() model.add(LSTM((h_layer_size, 1, 1, 1))) model.add(PReLULayer()) model.add(Dropout(0.2)) model.add(BatchNorm()) model.add(LSTM((h_layer_size, 1, 1, 1))) model.add(PReLULayer()) model.add(Dropout(0.2)) model.add(BatchNorm()) model.add(FullyConnected()) model.add(ActivationLayer(Activations.alt_softmax)) model.set_loss(Losses.Crossentropy()) model.set_optimizer(Optimizers.RMSprop(learning_rate=learning_rate)) for iteration in range(0, num_iterations): print() print('-' * 50) print('Iteration', iteration + 1) model.train(X, y, batch_size=batch_size, num_epochs=1, params_filename='okapi_shakespeare_params.pk') start_index = random.randint(0, len(text) - maxlen - 1) for diversity in diversities: print() print('----- diversity:', diversity) generated = '' sentence = text[start_index: start_index + maxlen] generated += sentence print('----- Generating with seed: "' + sentence + '"') sys.stdout.write(generated) for iteration in range(num_chars): x = np.zeros((1, maxlen, len(chars))) for t, char in enumerate(sentence): x[0, t, char_to_index[char]] = 1. preds = model.predict(x) preds = preds[0] next_index = sample(preds, diversity) next_char = index_to_char[next_index] generated += next_char sentence = sentence[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() print() ok.save_model(model, 'okapi_shakespeare_model.pk')
from OkapiV2.Core import Model, Branch from OkapiV2.Layers.Basic import FullyConnected from OkapiV2.Layers.Activations import ActivationLayer, PReLULayer from OkapiV2 import Activations, Datasets, Optimizers x_train, y_train, x_val, y_val, x_test, y_test = Datasets.load_mnist() tree = Branch() tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected()) tree.add_layer(ActivationLayer(Activations.softmax)) tree.add_input(x_train) model = Model() model.set_tree(tree) model.set_optimizer(Optimizers.RMSprop(learning_rate=0.00005)) index = 60000 model.train([x_train[:index, :, :, :]], y_train[:index, :], 24) accuracy = model.get_accuracy([x_train[:index, :, :, :]], y_train[:index]) print("Accuracy: {}%".format(accuracy)) test_accuracy = model.get_accuracy([x_test], y_test) print("Test accuracy: {}%".format(test_accuracy))
dropout_p = 0.2 num_filters = 32 filter_size = 3 pool_size = 2 num_classes = 10 pad = True batch_size = 10000 population_size = 100 num_generations = 100 rm_p = 0.8 init_mut_p = 1e-5 init_mut_std = 1e-3 init_cross_p = 0.7 model = Model() model.add(Convolutional(num_filters, filter_size, filter_size, pad=pad)) model.add(ActivationLayer(Activations.tanh)) model.add(MaxPooling(pool_size, pool_size)) model.add(PReLULayer()) model.add(Dropout(dropout_p)) model.add(FullyConnected(bias_initializer=Initializers.glorot_uniform)) model.add(ActivationLayer(Activations.alt_softmax)) model.compile(X_train, y_train) X_batches, y_batches, num_batches = \ ok.make_batches(X_train, y_train, batch_size) def initialize(population_size):
from OkapiV2.Core import Model, Branch from OkapiV2.Layers.Basic import FullyConnected from OkapiV2.Layers.Activations import ActivationLayer, PReLULayer from OkapiV2 import Activations, Datasets, Optimizers X_train, y_train, X_val, y_val, X_test, y_test = Datasets.load_mnist() tree = Branch() tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected()) tree.add_layer(ActivationLayer(Activations.softmax)) tree.add_input(X_train) model = Model() model.set_tree(tree) model.set_optimizer(Optimizers.RMSprop(learning_rate=0.00005)) index = 60000 model.train([X_train], y_train, 24) accuracy = model.get_accuracy([X_train], y_train) print('Accuracy: {}%'.format(accuracy)) test_accuracy = model.get_accuracy([X_test], y_test) print('Test accuracy: {}%'.format(test_accuracy))
dropout_p = 0.2 num_filters = 1 filter_size = 5 pool_size = 2 num_classes = 10 pad = False batch_size = 1000 population_size = 100 num_generations = 100 rm_p = 0.8 init_mut_p = 1e-5 init_mut_std = 1e-3 init_cross_p = 0.7 model = Model() model.add(Convolutional(num_filters, filter_size, filter_size, pad=pad)) model.add(ActivationLayer(Activations.tanh)) model.add(MaxPooling(pool_size, pool_size)) model.add(ActivationLayer(Activations.ReLU)) model.add(Dropout(dropout_p)) model.add(FullyConnected()) model.add(ActivationLayer(Activations.alt_softmax)) model.compile(X_train, y_train) X_batches, y_batches, num_batches = \ ok.make_batches(X_train, y_train, batch_size) def get_loss(X_batch=None, y_batch=None, full=False): if full:
X[i, t, char_indices[char]] = 1 y[i, char_indices[next_chars[i]]] = 1 # build the model: 2 stacked LSTM print('Build model...') tree = Branch() tree.add_layer(LSTM((512, 1, 1, 1), return_sequences=True)) tree.add_layer(Dropout(0.2)) tree.add_layer(LSTM((512, 1, 1, 1), return_sequences=False)) tree.add_layer(Dropout(0.2)) tree.add_layer(FullyConnected()) tree.add_layer(ActivationLayer(Activations.softmax)) tree.add_input(X) model = Model() model.set_tree(tree) def sample(a, temperature=1.0): # helper function to sample an index from a probability array a = np.log(a) / temperature a = np.exp(a) / np.sum(np.exp(a)) return np.argmax(np.random.multinomial(1, a, 1)) # train the model, output generated text after each iteration for iteration in range(1, 60): print() print('-' * 50) print('Iteration', iteration) model.train([X], y, num_epochs=1)
action_branch.add_layer(FullyConnected((100, 1, 1, 1))) action_branch.add_layer(PReLULayer()) action_branch.add_input(X_reward[1]) tree = Branch() tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected()) tree.add_input(X_reward[0]) tree.add_input(X_reward[1]) model = Model() model.set_tree(tree) model.set_loss(Losses.MeanSquared()) model.add_output(y_reward) learning_rate = 0.00002 model.set_optimizer(Optimizers.RMSprop(learning_rate=learning_rate)) model.compile(X_reward, y_reward, initialize_params=True) file = open('reward_model_params_vec.pk', 'rb') params = pickle.load(file) file.close() model.set_params_as_vec(params) model.compile(X_reward, y_reward, initialize_params=False) '''ok.save_model(model, 'okapi_reward_model.pk') model = ok.load_model('okapi_reward_model.pk')'''
action_branch.add_layer(PReLULayer()) action_branch.add_input(X_reward[1]) tree = Branch() tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected()) tree.add_layer(ActivationLayer(Activations.tanh)) tree.add_input(X_reward[0]) tree.add_input(X_reward[1]) model = Model() model.set_tree(tree) model.set_loss(Losses.MeanSquared()) learning_rate = 0.00002 model.set_optimizer(Optimizers.RMSprop(learning_rate=learning_rate)) # model.compile(X_reward, y_reward) # model.train(X_reward, y_reward, 24) reinforce_index = X_obs.shape[0] X_batches, y_batches, num_batches = ok.make_batches([X_train], y_train, batch_size=10000) for i in range(8): print('\n---Iteration {}---'.format(i + 1)) for X_batch, y_batch in zip(X_batches, y_batches): model.train(X_reward, y_reward, 24) accuracy, preds = model.get_dream_accuracy([X_batch[0], None], y_batch) preds = preds[0]
action_branch.add_layer(PReLULayer()) action_branch.add_input(X_reward[1]) tree = Branch() tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected((512, 1, 1, 1))) tree.add_layer(PReLULayer()) tree.add_layer(FullyConnected()) tree.add_layer(ActivationLayer(Activations.tanh)) tree.add_input(X_reward[0]) tree.add_input(X_reward[1]) model = Model() model.set_tree(tree) model.set_loss(Losses.MeanSquared()) model.set_optimizer(Optimizers.RMSprop(learning_rate=0.00005)) model.add_output(y_reward) model.compile(X_reward, y_reward) model.train(X_reward, y_reward, 24) # print(model.predict([X_train[[0]], y_train[0]])) # print(model.predict([X_train[[0]], np.asarray([1, 0, 0, 0, 0, 0, 0, 0, 0, 0])])) # prediction = model.predict_dream([X_test, None], X_reward[1][[0]].shape) X_batches, y_batches, num_batches = ok.make_batches([X_train], y_train, batch_size=10000) for i in range(12): for i, [X_batch, y_batch] in enumerate(zip(X_batches, y_batches)): accuracy, preds = model.get_dream_accuracy([X_batch[0], None], y_batch) preds = preds[0]