def main(): logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG) number_of_samples = 50 input_data_dimension = 10 output_data_dimension = 3 nn_object = Sequential() nn_object.add_layer( DenseLayer(units=32, activation="tanh", input_dimension=input_data_dimension)) nn_object.add_layer(DenseLayer(units=64, activation="tanh")) nn_object.add_layer( DenseLayer(units=output_data_dimension, activation="softmax")) nn_object.compile(loss="cross_entropy", error_threshold=0.001) input_data = -0.5 + np.random.rand(input_data_dimension, number_of_samples) targets = np.random.randint(0, output_data_dimension, number_of_samples).reshape(-1) # Convert labels to one hot encoding output_data = one_hot_encoding(targets) nn_object.train(input_data, output_data) predicted_output = nn_object.predict(input_data) predicted_labels = vector_to_label(predicted_output) print("Accuracy: {}%".format( 100 * categorical_accuracy(predicted_labels, targets)))
def test_model_training(self): # Create nn object nn_object = Sequential() nn_object.add_layer( DenseLayer(units=16, activation="sigmoid", input_dimension=self.input_data_dimension)) nn_object.add_layer( DenseLayer(units=self.output_data_dimension, activation="softmax")) nn_object.compile(loss="cross_entropy") # Train the neural network nn_object.train(self.input_data, self.target_data, max_epochs=1)
def construct_nn(self): # First construct an optimizer to use optimizer = ADAM(lr=self.learning_rate) # Create L2 regularizer regularizer = L2Regularizer(self.regularization_coefficient) self.nn = Sequential() self.nn.add_layer( DenseLayer(units=32, activation="tanh", input_dimension=self.input_data_dimension, regularizer=regularizer)) self.nn.add_layer( DenseLayer(units=32, activation="relu", regularizer=regularizer)) self.nn.add_layer( DenseLayer(units=32, activation="tanh", regularizer=regularizer)) self.nn.add_layer( DenseLayer(units=self.output_data_dimension, activation="softmax", regularizer=regularizer)) self.nn.compile(loss="cross_entropy", error_threshold=self.error_threshold, optimizer=optimizer)
def train_network(self, x_train, y_train): # Create a logger for training logger = logging.getLogger("cifar10") logger.setLevel(logging.INFO) fh = logging.FileHandler("cifar10_training.log") logger.addHandler(fh) # Construct the network optimizer = ADAM(lr=0.001) regularizer = L2Regularizer(0.01) self.nn = Sequential() self.nn.add_layer( Convolution2D(4, (3, 3), input_shape=(3, 32, 32), regularizer=regularizer)) self.nn.add_layer(MaxPooling(pool_size=(2, 2))) self.nn.add_layer(Convolution2D(8, (3, 3), regularizer=regularizer)) self.nn.add_layer(MaxPooling(pool_size=(2, 2))) self.nn.add_layer(Convolution2D(16, (3, 3), regularizer=regularizer)) self.nn.add_layer(Flatten()) self.nn.add_layer(DenseLayer(units=10, activation="softmax")) self.nn.compile(loss="cross_entropy", error_threshold=0.01, optimizer=optimizer) self.nn.train(x_train, y_train, logging_frequency=1, max_epochs=20, training_logger=logger, update_frequency=100, layers_filename=self.weights_filename, mini_batch_size=1024)