我在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))
这样我就从张量中选择了列。现在,如果我对continiguos列执行相同的操作,即row_tmp[:, 2:5]
,我没有问题,但是对于notcontiniguos列,我得到:
/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.Variable
更改tf.constant
,但出现以下错误:
ValueError: tf.function-decorated function tried to create variables on non-first call.
有什么建议吗
您只需执行以下操作:
相关问题 更多 >
编程相关推荐