0
$\begingroup$

I'm trying to write a custom metric for early stopping in which I exclude the highest 5% of points from MSE computation. The code looks as follows:

def myMetric(y_true, y_pred):
    res = K.square(y_pred - y_true)
    res = tf.sort(res)

    upPerc = tf.Variable(95.0)
    trimFrac = tf.Variable (0.01)
    inputLen = K.shape(res)
    # trimIdx = tf.Variable(K.shape(res)[0] * upPerc * trimFrac, dtype=tf.int32)

    return K.mean(tf.slice(res, tf.constant(0), inputLen))
    # return K.mean(tf.slice(res, tf.constant(0), trimIdx))

This doesn't work because I suspect the tensor shape is None. The trace is as follows:

File "../coreModels/training.py", line 170, in build
    self.model.compile(loss=losses, optimizer=thisAdam, metrics=['mae', evalFunction])
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 330, in compile
    masks=self._prepare_output_masks())
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2170, in _handle_metrics
    target, output, output_mask))
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2118, in _handle_per_output_metrics
    mask)
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2094, in _call_metric_fn
    strategy=self._distribution_strategy)
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/distribute/distributed_training_utils.py", line 1054, in call_replica_local_fn
    return fn(*args, **kwargs)
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 873, in call_metric_function
    return metric_fn(y_true, y_pred, sample_weight=weights)
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/metrics.py", line 170, in __call__
    update_op = self.update_state(*args, **kwargs)  # pylint: disable=not-callable
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/utils/metrics_utils.py", line 73, in decorated
    update_op = update_state_fn(*args, **kwargs)
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/metrics.py", line 549, in update_state
    matches = self._fn(y_true, y_pred, **self._fn_kwargs)
  File "../coreModels/training.py", line 184, in clippedMAE
    return K.mean(tf.slice(res, tf.constant(0), inputLen))
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py", line 733, in slice
    return gen_array_ops._slice(input_, begin, size, name=name)
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 8823, in _slice
    "Slice", input=input, begin=begin, size=size, name=name)
  File "/Users/tejas/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 545, in _apply_op_helper
    (input_name, err))
ValueError: Tried to convert 'size' to a tensor and failed. Error: Cannot convert an unknown Dimension to a Tensor: ?

How can I use the tensor length to slice the residual tensor?

| improve this question | | | | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.