0

Testing and training function

def CNN_training(image, label, layers, alpha=2.0):

# Forward step
output, loss, accuracy = CNN_forward(image, label, layers)

# Initial gradient
gradient = np.zeros(10)
gradient[label] = -1/output[label]

# Backprop step
gradient_back = CNN_backprop(gradient, layers, alpha)

return loss, accuracy

def CNN_testing(image, label, layers):
# Perform forward propagation
for layer in layers:
    image = layer.forward(image)

# Compute the loss and accuracy
loss = -np.log(image[label])
accuracy = 1 if np.argmax(image) == label else 0

return loss, accuracy

Main function

def main():
# Load training data
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train_full = X_train[:5000]
y_train_full = y_train[:5000]
# Define the network
layers = [
    ConvolutionLayer(16, 3),  # layer with 16 3x3 filters, output (26, 26, 16)
    MaxPoolingLayer(2),  # pooling layer 2x2, output (13, 13, 16)
    SoftmaxLayer(13 * 13 * 16, 10)  # softmax layer with 5*5*32 input and 10 output
]

learning_rate = 0.05
batch_size = 10
epochs = 4

accuracy_results = []
time_results = []
average_loss_results = []

Loop for getting the result for 4 epoch

for run in range(1):
    print(f"Run {run + 1}")

    accuracy_list = []
    average_loss_list = []
    start_time = time.time()

    for epoch in range(epochs):
        print('Epoch {} ->'.format(epoch + 1))
        # Shuffle training data
        permutation = np.random.permutation(len(X_train_full))
        X_train = X_train_full[permutation]
        y_train = y_train_full[permutation]

        # Training the CNN
        loss = 0
        accuracy = 0

        for i, (image, label) in enumerate(zip(X_train, y_train)):
            if i % batch_size == 0:
                print("Step {}. For the last {} steps: average loss {}, accuracy {}".format(
                    i + 1, batch_size, loss / batch_size, accuracy / batch_size))
                loss = 0
                accuracy = 0

            batch_loss, batch_accuracy = CNN_training(image, label, layers, learning_rate)
            loss += batch_loss
            accuracy += batch_accuracy

            # Append accuracy and average loss to lists
            accuracy_list.append(batch_accuracy)
            average_loss_list.append(batch_loss / batch_size)

Calculate for the computational time in the loop

    end_time = time.time()
    total_time = end_time - start_time

    print(f"Total time taken for training: {total_time:.2f} seconds")
    average_accuracy = sum(accuracy_list) / len(accuracy_list)
    print("Average Accuracy: {:.2%}".format(average_accuracy))

    # Save the results from each run
    accuracy_results.append(average_accuracy)
    time_results.append(total_time)
    average_loss_results.append(average_loss_list)

Plotting the graph

# Plot accuracy, time, and average loss
plt.figure(figsize=(12, 4))

# Average loss plot
plt.subplot(1, 3, 3)
for avg_loss in average_loss_results:
    plt.plot(x_values, avg_loss)  # Use x_values as the x-axis values
plt.xlabel('Epoch')
plt.ylabel('Average Loss')
plt.title('Average Loss during Training')

plt.tight_layout()
plt.show()

What is the wrong on the graph as the output is not show any plotting on it? I want to get two graph which are training average loss per epoch and testing average loss per epoch

Elfs
  • 1
  • 3

0 Answers0