def test_batch_norm_storage(self): x_train, x_test, y_train, y_test = simple_classification() batch_norm = layers.BatchNorm() gdnet = algorithms.MinibatchGradientDescent( [ layers.Input(10), layers.Relu(5), batch_norm, layers.Sigmoid(1), ], batch_size=10, verbose=True, # keep it as `True` ) gdnet.train(x_train, y_train) error_before_save = gdnet.prediction_error(x_test, y_test) mean_before_save = batch_norm.running_mean.get_value() inv_std_before_save = batch_norm.running_inv_std.get_value() with tempfile.NamedTemporaryFile() as temp: storage.save(gdnet, temp.name) storage.load(gdnet, temp.name) error_after_load = gdnet.prediction_error(x_test, y_test) mean_after_load = batch_norm.running_mean.get_value() inv_std_after_load = batch_norm.running_inv_std.get_value() self.assertAlmostEqual(error_before_save, error_after_load) np.testing.assert_array_almost_equal(mean_before_save, mean_after_load) np.testing.assert_array_almost_equal(inv_std_before_save, inv_std_after_load)
def __init__(self, inputs, outputs): #Initialsies a gradient descent neural net, reshaping inputs and outputs self.neural_network = algorithms.MinibatchGradientDescent((10, 20, 2), verbose=True, step=0.1) inputs = np.array(inputs) outputs = np.array(outputs) self.X = np.reshape(inputs, (len(inputs), 10)) self.Y = np.reshape(outputs, (len(outputs), 2))
def select_algorithm(self, algorithm, options=None): try: self.network = algorithms.LevenbergMarquardt(self.layers) opt = options print(opt[1]) print("Wybrano optymalizator: " + str(algorithm)) except RecursionError: print("Problem rekursji") return None if algorithm == 'GradientDescent': self.network = algorithms.GradientDescent(self.layers) if algorithm == 'LevenbergMarquardt': self.network = algorithms.LevenbergMarquardt(connection=self.layers, mu=opt[0], mu_update_factor=opt[1]) if algorithm == 'Adam': self.network = algorithms.Adam(self.layers) if algorithm == 'QuasiNewton': self.network = algorithms.QuasiNewton(self.layers) if algorithm == 'Quickprop': self.network = algorithms.Quickprop(self.layers) if algorithm == 'MinibatchGradientDescent': self.network = algorithms.MinibatchGradientDescent(self.layers) if algorithm == 'ConjugateGradient': self.network = algorithms.ConjugateGradient(self.layers) if algorithm == 'Hessian': self.network = algorithms.Hessian(self.layers) if algorithm == 'HessianDiagonal': self.network = algorithms.HessianDiagonal(self.layers) if algorithm == 'Momentum': self.network = algorithms.Momentum(self.layers) if algorithm == 'RPROP': self.network = algorithms.RPROP(self.layers) if algorithm == 'IRPROPPlus': self.network = algorithms.IRPROPPlus(self.layers) if algorithm == 'Adadelta': self.network = algorithms.Adadelta(self.layers) if algorithm == 'Adagrad': self.network = algorithms.Adagrad(self.layers) if algorithm == 'RMSProp': self.network = algorithms.RMSProp(self.layers) if algorithm == 'Adamax': self.network = algorithms.Adamax(self.layers)
def test_on_bigger_dataset(self): data, targets = datasets.make_regression(n_samples=4000) scaler = preprocessing.MinMaxScaler(feature_range=(0, 1)) data = scaler.fit_transform(data) targets = scaler.fit_transform(targets) x_train, x_test, y_train, y_test = train_test_split( data, targets, train_size=0.7 ) in_size = data.shape[1] out_size = targets.shape[1] if len(targets.shape) > 1 else 1 sgd_network = algorithms.MinibatchGradientDescent( SigmoidLayer(in_size) > SigmoidLayer(300) > OutputLayer(out_size), step=0.2, batch_size=10 ) sgd_network.train(x_train, y_train, x_test, y_test, epochs=10) result = sgd_network.predict(x_test) test_error = sgd_network.error(result, np.reshape(y_test, (y_test.size, 1))) self.assertAlmostEqual(0.02, test_error, places=2)
environment.reproducible() rectangle_dataset = dataset.Rectangles() rectangle_dataset.fetch(download_if_missing=True) data, target = rectangle_dataset.classification_task() x_train, x_test, y_train, y_test = model_selection.train_test_split( data, target, test_size=0.5) network = algorithms.MinibatchGradientDescent( [ layers.Input(784), layers.Sigmoid(20), layers.Sigmoid(1), ], error='binary_crossentropy', verbose=True, show_epoch=1, batch_size=1, ) network.train(x_train, y_train, x_test, y_test, epochs=10) y_predicted = network.predict(x_test).round() print(metrics.classification_report(y_test, y_predicted)) roc_score = metrics.roc_auc_score(y_test, y_predicted) print("ROC score: {}".format(roc_score)) accuracy = metrics.accuracy_score(y_test, y_predicted) print("Accuracy: {:.2%}".format(accuracy))
encoder_classifier = algorithms.Adadelta( layers.Input(encoder.output_shape) > classifier_network, verbose=True, step=0.05, shuffle_data=True, batch_size=64, error='categorical_crossentropy', ) encoder_classifier.architecture() encoder_classifier.train(x_labeled_encoded, y_labeled, x_unlabeled_encoded, y_unlabeled, epochs=100) classifier = algorithms.MinibatchGradientDescent( encoder > classifier_network, verbose=True, step=0.01, shuffle_data=True, batch_size=64, error='categorical_crossentropy', ) classifier.architecture() classifier.train(x_labeled_4d, y_labeled, epochs=100) unlabeled_predicted = classifier.predict(x_unlabeled_4d).argmax(axis=1) y_unlabeled_classes = np.asarray(y_unlabeled).argmax(axis=1) print(metrics.classification_report(y_unlabeled_classes, unlabeled_predicted)) score = metrics.accuracy_score(y_unlabeled_classes, unlabeled_predicted) print("Validation accuracy: {:.2%}".format(score))
x_test /= std x_train = x_train.reshape(x_train.shape[0], x_train.shape[2]) y_train = y_train.reshape(y_train.shape[0], y_train.shape[2]) x_test = x_test.reshape(x_test.shape[0], x_test.shape[2]) y_test = y_test.reshape(y_test.shape[0], y_test.shape[2]) # Creating the network network = algorithms.MinibatchGradientDescent( [ layers.Input(500), layers.Relu(252), layers.Relu(128), layers.Softplus(4), ], batch_size=128, step=0.1, # Using Mean Squared Error as the Loss Function error='mse', # Learning Rate #step=1.0, # Display network data in console verbose=True, # shuffle training data random before each epoch shuffle_data=True, show_epoch=1) # Show network architecture in console network.architecture() network.train(x_train, y_train, x_test, y_test, epochs=70) plots.error_plot(network) # Making test filters MIN_POWER = np.min(x_train)
error='categorical_crossentropy', ) linear_classifier.architecture() linear_classifier.train(x_labeled_encoded, y_labeled, x_unlabeled_encoded, y_unlabeled, epochs=100) classification_layer = surgery.cut(linear_classifier, start=1, end=4) classifier_structure = surgery.sew_together( [classifier_structure, classification_layer]) classifier = algorithms.MinibatchGradientDescent( classifier_structure, verbose=True, step=0.1, shuffle_data=True, batch_size=128, error='categorical_crossentropy', ) classifier.architecture() classifier.train(x_labeled_4d, y_labeled, epochs=1000) unlabeled_predicted = classifier.predict(x_unlabeled_4d).argmax(axis=1) y_unlabeled_classes = np.asarray(y_unlabeled).argmax(axis=1) print(metrics.classification_report(y_unlabeled_classes, unlabeled_predicted)) score = metrics.accuracy_score(y_unlabeled_classes, unlabeled_predicted) print("Validation accuracy: {:.2%}".format(score))