I have a neural network and i want to calculate error in it. If i give you the entire…
- User
I have a neural network and i want to calculate error in it. If i give you the entire neural network and the error funtion can you integrate the two
- Assistant
- User
i dont want to calculate loss, i want to see the error in test result
- Assistant
- User
This is my neural network in action
predicted_y_values = []
for x_test in x_test_split: x_test = np.array(x_test).reshape(-1, 1) layer1.forward(x_test) activation1 = Activation_tanh() activation1.forward(layer1.output)
layer2.forward(activation1.output) activation2 = Activation_tanh() activation2.forward(layer2.output) layer3.forward(activation2.output) activation3 = Activation_tanh() activation3.forward(layer3.output) layer4.forward(activation3.output) activation4 = Activation_tanh() activation4.forward(layer4.output) layer5.forward(activation4.output) activation5 = Activation_tanh() activation5.forward(layer5.output) predicted_y_values.append(activation5.output[0][0])Plot the predicted y values along with y_train_split for comparison
plt.scatter(x_test_split, y_test_split, label='Expected data') plt.scatter(x_test_split, predicted_y_values, color='red', label='Predicted data') plt.xlabel('x') plt.ylabel('y') plt.title('Predicted vs Training Data') plt.legend() plt.show()
and this is the error function def calculate_mape(y_true, y_pred): return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
mape = calculate_mape(y_test, A2) print(f'MAPE: {mape}%')
- Assistant
Based on the code snippets you've shared, you have a neural network in action that processes test inputs (
x_test_split) through multiple layers and uses the hyperbolic tangent (tanh) as the activation function for each layer. After computing the forward pass through the network, you collect the predictions () and plot them against the true values from to visually compare the model's performance. - User
MAPE: 158402498875801.5% how can i get this value
- Assistant
- User
give me the code to calculate mean absolute error
- Assistant
The Mean Absolute Error (MAE) is a measure of errors between paired observations expressing the same phenomenon. It's calculated as the average of the absolute differences between the predicted values and the actual values, without considering the direction. MAE is a useful metric because it's easy to understand and interpret.
Here's a Python function that calculates the Mean Absolute Error (MAE). This function assumes that you have two lists or arrays: one for the true values (
y_true) and one for the predicted values (y_pred). Both should be of the same length.import numpy as np def calculate_mae(y_true, y_pred): """ Calculate the Mean Absolute Error between true and predicted values Args: - y_true: Actual true values - y_pred: Predicted values Returns: - mae: Mean Absolute Error """ y_true, y_pred = np.array(y_true), np.array(y_pred) mae = np.mean(np.abs(y_true - y_pred)) return mae # Example usage: y_true = [3, -0.5, 2, 7] y_pred = [2.5, 0.0, 2, 8] mae = calculate_mae(y_true, y_pred) print(f"Mean Absolute Error (MAE): {mae}")