def run():
    file_path = os.path.dirname(
        os.path.realpath(__file__)) + "/dlmb_mnist_example.json"

    # If a file of the neural-net model's architexture already exists,
    # then there is no need to build a new model.
    if os.path.isfile(file_path):

        # load the model and get its predictions based on x_test
        nn_model = Sequential()
        nn_model.load(file_path)

        predictions = nn_model.predict(x_test)

        # compare the predictions to the correct labels
        print(
            f"This model got a {validate_model(predictions, y_test)/100}% accuracy"
        )

    # If the file doesn't exist then we need to build a neural-net model and train it.
    else:

        # Build the neural-net model
        nn_model = Sequential([
            Dense(
                128, 784, activation="ReLU"
            ),  # for the layer_dim we want 128 outputs and 784 inputs (each pixel on the image)
            Batchnorm(128),
            Dense(128, 128, activation="ReLU"),
            Batchnorm(128),
            Dense(32, 128, activation="ReLU"),
            Batchnorm(32),
            Dense(10, 32, activation="Softmax"
                  )  # We have 10 nodes in the layer for each number from 0 - 9
        ])

        nn_model.build(loss="crossentropy", optimizer="adam")
        # Crossentropy is a good loss function when you are doing logistic regression (classification)
        # Adam is one of the most popular optimizers

        nn_model.train(x_train, y_train, epochs=10, batch_size=1000)
        # Train the model
        # We go through the data 10 times and split the data of 60000 samples into 1000 sized batches leaving 60 samples

        # Now we save the model so we can use it again without re-training
        nn_model.save(file_path)  # When saving, files must end in .json
Ejemplo n.º 2
0
def main():
    # 先讀取資料,並建立模型。
    # 輸入的維度為一個資料的長度,因為資料量小,batch size即為資料總數。
    x, y = ParityBits(8).load_data()
    batch_size, input_dim = x.shape
    model = Sequential(
        [Dense(64, activation=ReLU()),
         Dense(32, activation=Tanh()),
         Dense(16, activation=Tanh()),
         Dense(4, activation=None),
         Dense(1, activation=Sigmoid())],
        input_dim=input_dim,
        # 使用GD為優化器,MSE為損失函式。
        optimizer=GradientDescent(learning_rate=0.01, momentum=0.0),
        loss=MeanSquaredError())
    # 設定好epochs後訓練模型,訓練完後取得預測結果和每個epoch的損失值。
    y_pred, losses = model.train(
        x, y, batch_size=batch_size, epochs=200, verbose_step=10)

    # 因為答案皆為整數0或1,因此訓練的成果為模型預測的結果取整數。
    result = np.around(y_pred).astype(int)
    # 將答案與訓練成果相減。
    diff = np.subtract(y, result)
    print(pd.DataFrame({
        # 印出表格時,須將輸入的資料的每項陣列例如`[0 0 0 0 0 0 0 0]`轉成字串,
        # 因為Pandas的DataFrame的每一項不能吃陣列。
        "Data": [np.array_str(v) for v in x],
        "Answer": y[:, 0],
        "Prediction": [f'{v:.8f}' for v in y_pred[:, 0]],
        "Result": result[:, 0],
        # 如果答案與訓練成果在相減之後為0的話代表預測正確,否則失敗。
        "Correct": [True if v == 0 else False for v in diff[:, 0]]
    }, index=np.arange(1, len(x) + 1)).to_string())
    # 輸出最後的損失值和訓練成果與答案差了幾項,並繪製每個epoch與其損失值的變化圖表。
    print(f'loss: {losses[-1]:.8f}, difference: {np.count_nonzero(diff)}')
    plt.figure(figsize=(8, 4))
    plt.plot(losses)
    plt.xlabel('epoch')
    plt.ylabel('loss')
    plt.show()
Ejemplo n.º 3
0
    (train_x, test_x, train_y, test_y) = mnist(data_dir)
    (train_x, valid_x, train_y, valid_y) = train_test_split(train_x, train_y, train_size=0.8,
                                                            random_state=np.random.randint(10e6))
    return (shared(train_x, borrow=True),
            shared(test_x, borrow=True),
            shared(valid_x, borrow=True),
            shared(train_y, borrow=True),
            shared(test_y, borrow=True),
            shared(valid_y, borrow=True))


(train_x, test_x, valid_x, train_y, test_y, valid_y) = load_data('../data/mnist')

# Shared params
epoch = 5
batch_size = 1000

# Without dropout
model = Sequential(
    [FullConnected(784, 625, activation='relu'),
     FullConnected(625, 625, activation='relu'),
     FullConnected(625, 10, activation='softmax')],
    optimizer=RMSprop()
)

model.train(train_x, train_y, epoch=epoch, batch_size=batch_size,
            validation_data=(valid_x, valid_y), valid_freq=20, monitor=True)

score = model.score(test_x, test_y)
print('test score: {0}'.format(score.eval()))
Ejemplo n.º 4
0
    if valid > 0:
        (train_x, valid_x, train_y, valid_y) = train_test_split(all_x, all_y, train_size=1 - valid)
        return (shared(train_x, borrow=True),
                shared(test_x, borrow=True),
                shared(valid_x, borrow=True),
                shared(train_y, borrow=True),
                shared(test_y, borrow=True),
                shared(valid_y, borrow=True))
    else:
        return (shared(train_x, borrow=True),
                shared(test_x, borrow=True),
                shared(train_y, borrow=True),
                shared(test_y, borrow=True))

dataset = datasets.load_digits()
(train_x, test_x, valid_x, train_y, test_y, valid_y) = load_data(dataset, valid=0.2)

model = Sequential(
    [FullConnected(64, 128),
     FullConnected(128, 10, activation='softmax')],
    optimizer=SGD(lr=0.0001, decay=.001, momentum=0.9)
)

model.train(train_x, train_y, epoch=100, batch_size=1437,
            validation_data=(valid_x, valid_y), valid_freq=5, patience=10,
            monitor=True)

score = model.score(test_x, test_y)
print('test score: {0}'.format(score.eval()))
Ejemplo n.º 5
0
        (train_x, valid_x, train_y,
         valid_y) = train_test_split(all_x, all_y, train_size=1 - valid)
        return (shared(train_x, borrow=True), shared(test_x, borrow=True),
                shared(valid_x, borrow=True), shared(train_y, borrow=True),
                shared(test_y, borrow=True), shared(valid_y, borrow=True))
    else:
        return (shared(train_x, borrow=True), shared(test_x, borrow=True),
                shared(train_y, borrow=True), shared(test_y, borrow=True))


dataset = datasets.load_digits()
(train_x, test_x, valid_x, train_y, test_y, valid_y) = load_data(dataset,
                                                                 valid=0.2)

model = Sequential(
    [FullConnected(64, 128),
     FullConnected(128, 10, activation='softmax')],
    optimizer=SGD(lr=0.0001, decay=.001, momentum=0.9))

model.train(train_x,
            train_y,
            epoch=100,
            batch_size=1437,
            validation_data=(valid_x, valid_y),
            valid_freq=5,
            patience=10,
            monitor=True)

score = model.score(test_x, test_y)
print('test score: {0}'.format(score.eval()))
Ejemplo n.º 6
0
    return (shared(train_x, borrow=True), shared(test_x, borrow=True),
            shared(valid_x, borrow=True), shared(train_y, borrow=True),
            shared(test_y, borrow=True), shared(valid_y, borrow=True))


(train_x, test_x, valid_x, train_y, test_y,
 valid_y) = load_data('../data/mnist')

# Shared params
epoch = 5
batch_size = 1000

# Without dropout
model = Sequential([
    FullConnected(784, 625, activation='relu'),
    FullConnected(625, 625, activation='relu'),
    FullConnected(625, 10, activation='softmax')
],
                   optimizer=RMSprop())

model.train(train_x,
            train_y,
            epoch=epoch,
            batch_size=batch_size,
            validation_data=(valid_x, valid_y),
            valid_freq=20,
            monitor=True)

score = model.score(test_x, test_y)
print('test score: {0}'.format(score.eval()))