【发布时间】:2020-08-21 13:15:34
【问题描述】:
我在 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))
有了这个,我只需从张量中选择列。现在,如果我对连续列(即row_tmp[:, 2:5])做同样的事情,我没有问题,但我得到的不是连续列:
/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.
有什么建议吗?
【问题讨论】:
标签: python tensorflow keras backend loss-function