Hi! I’m trying to build a custom loss function for my model, but whenever I try to convert Tensors into .numpy() arrays with run_eagerly = True, it gives “WARNING: Gradients do not exist for variables …”. So I debugged other custom loss functions implemented using TensorFlow. But in my case, I need to apply to mask and splitting index arrays and then use those arrays as indices to apply some sort of arithmetic functions using broadcasting. But I retrieved indices lists after masking, but I just have to access those indices and add specific functions. But I found no way in TensorFlow to implement that in a vectorized way.
In NumPy, I can access it using:
error[i, j] = error[i, j] * 5
But TensorFlow does not provide any support related to this.
Also, I could not implement the custom_loss function by converting arrays to NumPy because it makes Gradient None. And it gives “WARNING: Gradients do not exist for variables …”.
TypeError Traceback (most recent call last)
Input In [193], in <cell line: 1>()
----> 1 error[i, j]
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\util\traceback_utils.py:153, in filter_traceback..error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.traceback)
→ 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\ops\array_ops.py:899, in _check_index(idx)
894 dtype = getattr(idx, “dtype”, None)
895 if (dtype is None or dtypes.as_dtype(dtype) not in _SUPPORTED_SLICE_DTYPES or
896 idx.shape and len(idx.shape) == 1):
897 # TODO(slebedev): IndexError seems more appropriate here, but it
898 # will break _slice_helper
contract.
→ 899 raise TypeError(_SLICE_TYPE_ERROR + “, got {!r}”.format(idx))
TypeError: Only integers, slices (:
), ellipsis (...
), tf.newaxis (None
) and scalar tf.int32/tf.int64 tensors are valid indices, got <tf.Tensor: shape=(4797,), dtype=int64, numpy=array([ 0, 0, 0, …, 26, 26, 26], dtype=int64)>
I tried using other functions provided by TensorFlow too, but those did not work.