I can't pass training data into my custom metric
I can't understand, how can I use input_prices into my custom metric?
Metric:
def metric_overprice(input_prices):
def overpricing(y_true, y_pred):
y_pred = tf.round(y_pred)
pred_value = tf.reduce_sum(y_pred * input_prices, axis=1)
true_value = tf.reduce_sum(y_true * input_prices, axis=1)
return tf.reduce_mean(pred_value - true_value)
return overpricing
Passing the symbolic tensor into the metric_overprice:
def supervised_continues_knapsack(item_count=5):
input_weights = Input((item_count,))
input_prices = Input((item_count,))
input_capacity = Input((1,))
inputs_concat = Concatenate()([input_weights, input_prices, input_capacity])
picks = Dense(item_count, use_bias=False, activation="sigmoid")(inputs_concat)
model = Model(inputs=[input_weights, input_prices, input_capacity], outputs=[picks])
modelpile("sgd",
binary_crossentropy,
metrics=[binary_accuracy, metric_overprice(input_prices), metric_pick_count()])
return model
I have the error message:
ValueError: Tried to convert 'input' to a tensor and failed. Error: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces keras.layers
and keras.operations
). You are likely doing something like:
x = Input(...)
...
tf_fn(x) # Invalid.
What you should do instead is wrap tf_fn
in a layer:
class MyLayer(Layer):
def call(self, x):
return tf_fn(x)
x = MyLayer()(x)
How can I do that correctly?