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))
next_chars.append(text[i + maxlen]) print('nb 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_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))
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
X_train, y_train, X_val, y_val, X_test, y_test = Datasets.load_mnist() X_obs, y_obs = X_train, y_train X_reward = [] X_reward.append(np.zeros((X_obs.shape[0] * 10, X_obs.shape[1], X_obs.shape[2], X_obs.shape[3]))) X_reward.append(np.zeros((X_obs.shape[0] * 10, y_obs.shape[1]))) y_reward = np.zeros((X_obs.shape[0] * 10, 1)) dropout_p = 0.2 num_filters = 32 filter_size = 3 pool_size = 2 num_classes = 10 pad = True state_branch = Branch() state_branch.add_layer(Convolutional(num_filters, filter_size, filter_size, pad=pad)) state_branch.add_layer(PReLULayer()) state_branch.add_layer(MaxPooling(pool_size, pool_size)) state_branch.add_layer(Convolutional(num_filters, filter_size, filter_size, pad=pad)) state_branch.add_layer(PReLULayer()) state_branch.add_layer(MaxPooling(pool_size, pool_size)) state_branch.add_layer(FullyConnected((100, 1, 1, 1))) state_branch.add_layer(ActivationLayer(Activations.tanh)) state_branch.add_input(X_reward[0]) action_branch = Branch() action_branch.add_layer(FullyConnected((100, 1, 1, 1))) action_branch.add_layer(PReLULayer()) action_branch.add_input(X_reward[1]) tree = Branch()