コード例 #1
0
    model.add_module(Dense(784, 30, initializer, initializer))
    model.add_module(Sigmoid())
    model.add_module(Dense(30, 10, initializer, initializer))
    model.add_module(Sigmoid())

    SGD.train(model, train_data.T)

    predict_data = (model.forward(test_input.T)).T

    accuracy = Accuracy()
    cost = Cost(loss_SGD, l2, model)
    precision = Precision()
    recall = Recall()
    f1 = F1()
    tab_of_predict = TabOfPredict()

    accuracy.evaluate(test_output, predict_data)
    cost.evaluate(test_output, predict_data)
    recall.evaluate(test_output, predict_data)
    precision.evaluate(test_output, predict_data)
    f1.evaluate(recall.history[0], precision.history[0])
    tab_of_predict.evaluate(test_output, predict_data)
    print("Accuracy: ", accuracy.history[0], "\n",
          "Cost: ", cost.history[0], "\n",
          "Recall: ", "\n", recall.history[0], "\n",
          "Precision: ", "\n", precision.history[0], "\n",
          "F1 score: ", "\n", f1.history[0], "\n",
          "Table of prediction: ", "\n", tab_of_predict.history[0])


コード例 #2
0
    def train(self, model, training_data):
        dataset = Dataset()
        train_accuracy = Accuracy()
        valid_accuracy = Accuracy()
        train_cost = Cost(self.loss, self.l2, model)
        valid_cost = Cost(self.loss, self.l2, model)

        for j in range(self.epoch):
            np.random.shuffle(training_data.T)
            size = int(training_data.shape[1] * (1 - self.size_of_valid))

            train_input = training_data[1:, :size]
            train_output = training_data[0:1, :size]
            train_input = dataset.normalization(train_input, 255)
            train_output = dataset.hot_one(train_output, 10)

            valid_input = training_data[1:, size:]
            valid_output = training_data[0:1, size:]
            valid_input = dataset.normalization(valid_input, 255)
            valid_output = dataset.hot_one(valid_output, 10)

            for k in range(0, len(train_output.T), self.batch_size):
                mini_input = train_input[:,
                                         self.batch_size:2 * self.batch_size]
                mini_output = train_output[:,
                                           self.batch_size:2 * self.batch_size]
                self.update(model, mini_input, mini_output,
                            len(train_output.T))

            train_model_predict = model.forward(train_input).T
            valid_model_predict = model.forward(valid_input).T

            train_accuracy.evaluate(train_output.T, train_model_predict)
            valid_accuracy.evaluate(valid_output.T, valid_model_predict)
            train_cost.evaluate(train_output.T, train_model_predict)
            valid_cost.evaluate(train_output.T, valid_model_predict)

            print("Epoch: ", j + 1)
            print(
                "Accuracy train: ",
                train_accuracy.history[j],
                "Accuracy valid: ",
                valid_accuracy.history[j],
            )
            print("Cost train: ", train_cost.history[j], "Cost valid: ",
                  valid_cost.history[j])

        epochs = range(1, self.epoch + 1)
        plt.plot(epochs,
                 train_accuracy.history,
                 'g',
                 label='Training accuracy')
        plt.plot(epochs,
                 valid_accuracy.history,
                 'b',
                 label='Validation accuracy')
        plt.title('Training and Validation accuracy')
        plt.xlabel('Epochs')
        plt.ylabel('Accuracy')
        plt.legend()
        plt.show()

        epochs = range(1, self.epoch + 1)
        plt.plot(epochs, train_cost.history, 'g', label='Training loss')
        plt.plot(epochs, valid_cost.history, 'b', label='Validation loss')
        plt.title('Training and Validation loss')
        plt.xlabel('Epochs')
        plt.ylabel('Loss')
        plt.legend()
        plt.show()