tensorflow - How can I use training data into my custom metric? - Stack Overflow
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?
- 微软推绿色IT挑战新网站促进IT节能环保
- 鲍尔默安抚硬件伙伴:Surface只是一个参考设计
- 分析:Windows 8平板电脑称雄市场需三大因素
- swift - DCoder bitwise solution. As I can't see the test cases, it's difficult to understand what I did wrong -
- IMDB pagination-container for a series in python - Stack Overflow
- How do you use partitions in Music Player Daemon (MPD) - Stack Overflow
- c - Interrupts on STM32F407G discovery Micro controller with HAL library to blink led - Stack Overflow
- c - sorry, unimplemented: Thumb-1 ‘hard-float’ VFP ABI - arm-linux-gnueabihf-gcc - targeting armv6 - Stack Overflow
- c - Validity of `__attribute__((pure))` - Stack Overflow
- visual studio code - Cant use pip install on pytorch Python 3.13.01, MacOS Sonoma 14.6.1 - Stack Overflow
- r - Conflict between multiple uses of inset_element and function plot_annotation - Stack Overflow
- c# - Having trouble getting the correct position to draw my sprite to when rotating it - Stack Overflow
- ESP32 Bluetooth - Is it possible to keep Provisioning Manager Bluetooth Up, After Provisioning Is Done? - Stack Overflow
- react native - How to convert location of each corner relative to the Camera Preview to the device view? - Stack Overflow
- php - Laravel Defer on API requests - Stack Overflow
- reactjs - Google Books API setOnLoadCallback works only after page reload - Stack Overflow
- c++ - Pointer of an object which has static storage as template non-type parameter, Clang and GCC agrees, MSVC doesn't -