我在keras中定义了自定义损失函数。在此自定义损失函数中,我从y_pred
中提取非连续值,如下所示:
sel_row = tf.constant([[2],[5],[8]])
row_tmp = y_pred
selected = tf.transpose(tf.gather_nd(tf.transpose(row_tmp), sel_row))
有了这个,我只是从张量中选择列。现在,如果我对contiguos列执行相同的操作,即row_tmp[:, 2:5]
,则没有问题,但是对于非continuos列,我得到:
/tensorflow/lib/python3.7/site-packages/tensorflow_core/python/framework/indexed_slices.py:424:
UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape.
This may consume a large amount of memory.
"Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
一切正常,但是最好有一种更好的方法来避免消耗过多内存。
我尝试用tf.constant
更改tf.Variable
,但发生此错误:
ValueError: tf.function-decorated function tried to create variables on non-first call.
任何提示?
您可以做: