class Model(Observable, Observer): def __init__(self, structure): Observable.__init__(self) self.neural_net = NeuralNet(structure, random_init_bound=0.05) self.commands = [] def load(self, path): self.neural_net.load(path) def predict(self, x): return self.neural_net.evaluate(x) def update(self, command): self.notify_observers() def add_command(self, command): self.commands.append(command) command.add_observer(self) self.notify_observers() def undo(self): if self.commands: self.commands.pop() self.notify_observers() def clear(self): self.commands.clear() self.notify_observers()
cumulative_acc = 0 # k-Fold Cross validation kf = KFold(n_splits=splits, shuffle=True) for i, (train_index, test_index) in enumerate(kf.split(x)): x_train, x_test = x[train_index], x[test_index] y_train, y_test = y[train_index], y[test_index] _cost, _accuracy = nnet.train(x_train, y_train, epochs=epochs, alpha=alpha, reg_para=reg_para, batch_size=batch_size, print_details=False, epochs_bw_details=epochs_bw_details, dropout=dropout_percent, dropout_layers=d_layers) op = nnet.predict(x_test) cumulative_acc += nnet.evaluate(op, y_test) print("-" * 30 + "\nKFold #{0}\n".format(i) + "-" * 30) print(classification_report(y_test.argmax(axis=1), op.argmax(axis=1))) print() print("Mean cross validation accuracy = {0}".format(cumulative_acc / splits)) s = input("Dump model? (y/n)\n") if s == 'y': nnet.dump()