0

I want to calculate simple NN model with gini coefficient as its optimizer function. Here is the my gini function:

def gini(actual, pred):
    nT = K.shape(actual)[-1]
    n = K.cast(nT, dtype='int32')
    inds = K.reverse(tf.nn.top_k(pred, n)[1], axes=[0])
    a_s = K.gather(actual, inds)
    a_c = K.cumsum(a_s)
    n = K.cast(nT, dtype=K.floatx())
    giniSum = K.cast(K.sum(a_c) / K.sum(a_s), dtype=K.floatx()) - (n + 1) / 2.0

    return giniSum / n


def gini_normalized(a, p):
    return gini(a, p) / gini(a, a)

And this is how I compile my model:

model = Sequential()
    model.add(Dense(32, input_shape=(60,)))
    model.add(Activation('relu'))
    model.add(Dense(2, activation='softmax'))

    sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss=gini_normalized, optimizer=sgd)

    return model

I always get this error "ValueError: None values not supported.", can anyone tell me what is my mistake?

Gregorius Edwadr
  • 399
  • 1
  • 3
  • 14

1 Answers1

0

This error is typical for functions that are not differentiable. (It also happens when some var is None and shouldn't be. Sometimes it's the case that someone forgot to add the return statement to a custom function somewhere or something like that).

In your case, it's not differentiable, indeed.

All the final values are coming only from actual, and actual is constant. (Your model can't be trained with such a function)

The var pred is the one that is connected to the model's weights, but the only part that pred is taking in the function is sorting the values in actual. But sorting is not a differentiable action.

There is probably nothing you can do about it, since your Gini function should indeed take values from actual as you did.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214