Nima Aghli
Nima Aghli

Reputation: 484

Evaluate keras model on part of output

I have a multi output regression model trained using Keras. Following is my network architecture:

model.add(Dense(4048, input_dim=16128,, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(3))

By calling:

score = model.evaluate(X_test, y_test)

I can get accuracy and mean absolute error over my test data and predicted values which is a array size of 3 by comparing to ground truth of array size 3.

My question is how can I evaluate the test data only on one output value, ignoring other two. I somehow want to evaluate on average mean error and also individual absolute errors.

Upvotes: 1

Views: 852

Answers (2)

Nima Aghli
Nima Aghli

Reputation: 484

Thanks for the hint. I took the option b and implemented my custom metrics as follows:

def MAE_ROLL(y_true, y_pred):
 return K.mean(K.abs(y_pred[:, 0] - y_true[:, 0]))

def MAE_PITCH(y_true, y_pred):
 return K.mean(K.abs(y_pred[:, 1] - y_true[:, 1]))

def MAE_YAW(y_true, y_pred):
 return K.mean(K.abs(y_pred[:, 2] - y_true[:, 2]))

model.compile(loss=mean_absolute_error, optimizer='adam',metrics=[MAE_ROLL,MAE_PITCH,MAE_YAW])

Upvotes: 0

rvinas
rvinas

Reputation: 11895

I would recommend one of the following two options:

a) Use the Keras functional API to define two different models model1 and model2 that are used to evaluate and train the network, respectively:

from keras.layers import Input, Dense, Concatenate
from keras.models import Model

a = Input((16128,))
h = Dense(4048, activation='relu')(a)
h = Dense(128, activation='relu')(h)
h1 = Dense(1)(h)
model1 = Model(a, h1)

h = Dense(2)(h)
h2 = Concatenate()([h1, h])
model2 = Model(a, h2)

# ... train on model2

# Evaluate on model1, which outputs the unit of interest
score = model1.evaluate(X_test, y_test)

b) Define your custom Keras metrics to exclusively select the unit of interest when computing the metrics.

Upvotes: 1

Related Questions