Beispiel #1
0
n_epoch = 10
batch_size = 1024

##############################
#### Training loop

loss_history = []
acc_history = []

for i in range(n_epoch):
    # print(f"EPOCH: {i}")
    for x_batch, y_batch in get_batches(train_x, train_y, batch_size):
        net.zeroGradParameters()

        # Forward
        predictions = net.forward(x_batch)
        loss = criterion.forward(predictions, y_batch)

        # Backward
        dp = criterion.backward(predictions, y_batch)
        net.backward(x_batch, dp)

        # Update weights
        adam_optimizer(net.getParameters(), net.getGradParameters(),
                       optimizer_config, optimizer_state)

        loss_history.append(loss)

        acc = 100 * accuracy_score(predictions.argmax(axis=-1),
                                   y_batch.argmax(axis=-1))
        acc_history.append(acc)
Beispiel #2
0
n_epoch = 20
batch_size = 128

##############################
#### Training loop

loss_history = []
acc_history = []

for i in range(n_epoch):
    print(f"EPOCH: {i}")
    for x_batch, y_batch in get_batches(X, Y, batch_size):
        net.zeroGradParameters()

        # Forward
        predictions = net.forward(x_batch)
        loss = criterion.forward(predictions, y_batch)

        # Backward
        dp = criterion.backward(predictions, y_batch)
        net.backward(x_batch, dp)

        # Update weights
        sgd_momentum(net.getParameters(), net.getGradParameters(),
                     optimizer_config, optimizer_state)

        loss_history.append(loss)
        acc_history.append(100 * accuracy_score(predictions.argmax(axis=-1),
                                                y_batch.argmax(axis=-1)))

################################
Beispiel #3
0
# test_label = test_label.add(0.125).mul(0.8)

# ----- Implementation of the architecture -----
architecture = Sequential(Linear(2, 25, ReLU()), Linear(25, 25, ReLU()),
                          Linear(25, 25, ReLU()), Linear(25, 2, Sigma()))

# ----- Training -----
round = 1
prev_loss = math.inf
prev_prev_loss = math.inf
errors = []
for epoch in range(epochs):
    for batch_start in range(0, nb_samples, batch_size):
        features = train_features[batch_start:batch_start + batch_size, :]
        labels = train_labels[batch_start:batch_start + batch_size]
        tr_loss, tr_error = architecture.forward(train_features, train_labels)
        architecture.backward()
        architecture.update(eta)
        loss, error = architecture.forward(test_features, test_labels)
        print(' --- Epoch ', round, '  Test Loss: ', loss.item(), '---',
              ' Errors: ', error)
        if prev_loss < tr_loss:
            eta *= 0.999
            if prev_prev_loss < tr_loss:
                eta *= 0.998

        errors.append(error)
        prev_prev_loss = prev_loss
        prev_loss = tr_loss

        round += 1
Beispiel #4
0
plotCostAndData(nn_test, inp_train, target_train, cost_nn_seq)

# In[243]:


# Computing the test error
def compute_nb_errors(model, data_input, data_target):
    mini_batch_size = 10
    nb_data_errors = 0
    for b in range(0, data_input.shape[0], mini_batch_size):
        input_batch = data_input[b:b + mini_batch_size]
        predicted_classes = model.forward(input_batch)
        predicted_classes = predicted_classes > 0.5
        for k in range(mini_batch_size):
            if data_target[b + k, 0] != predicted_classes[k, 0]:
                nb_data_errors += 1

    return nb_data_errors


# In[278]:

nb_test_err = compute_nb_errors(nn_test, inp_test, target_test)
print('Number of errors on the test set : ', nb_test_err)

# In[291]:

prediction = nn_test.forward(inp_train)
plt.scatter(inp_train[:, 0], inp_test[:, 1], c=prediction[:, 0], s=1)
plt.title('Prediction on the test set')