I'm pretty new to Keras and I'm trying to define my own metric. It calculates concordance index which is a measure for regression problems.
def cindex_score(y_true, y_pred):
sum = 0
pair = 0
for i in range(1, len(y_true)):
for j in range(0, i):
if i is not j:
if(y_true[i] > y_true[j]):
pair +=1
sum += 1* (y_pred[i] > y_pred[j]) + 0.5 * (y_pred[i] == y_pred[j])
if pair is not 0:
return sum/pair
else:
return 0
def baseline_model(hidden_neurons, inputdim):
model = Sequential()
model.add(Dense(hidden_neurons, input_dim=inputdim, init='normal', activation='relu'))
model.add(Dense(hidden_neurons, init='normal', activation='relu'))
model.add(Dense(1, init='normal')) #output layer
model.compile(loss='mean_squared_error', optimizer='adam', metrics=[cindex_score])
return model
def run_model(P_train, Y_train, P_test, model):
history = model.fit(numpy.array(P_train), numpy.array(Y_train), batch_size=50, nb_epoch=200)
plotLoss(history)
return model.predict(P_test)
baseline_model, run_model and cindex_score functions are in one.py and the following function is in two.py where I called the model,
def experiment():
hidden_neurons = 250
dmodel=baseline_model(hidden_neurons, train_pair.shape[1])
predicted_Y = run_model(train_pair,train_Y, test_pair, dmodel)
But I get the following error, "object of type 'Tensor' has no len()". It does not work with shape attribute as well.
For instance, y_true is represented as Tensor("dense_4_target:0", shape=(?, ?), dtype=float32) and its shape is Tensor("strided_slice:0", shape=(), dtype=int32).
Could you please help me about how to iterate within a Tensor object?
Best,