我已经为crf损失计算编写了一个自定义的 keras 损失。
def myLoss(self,y_true, y_pred):
"""
Args:
y_true: a tensor of shape batch_size X num_labels
y_pred: a tensor of shape batch_size X seq_length X num_labels
"""
with tf.init_scope():
self.seqlen = tf.constant(self.batch_size, shape=(self.seq_length,))
log_likelihood, transtion = tfa.text.crf.crf_log_likelihood(y_pred,y_true,self.seqlen )# logits, labels, seq_length
loss = tf.reduce_sum(-log_likelihood)
return loss
Run Code Online (Sandbox Code Playgroud)
但是上面的代码引发了以下问题:
ValueError: Tensor-typed variable initializers must either be wrapped in an init_scope or callable (e.g., `tf.Variable(lambda : tf.truncated_normal([10, 40]))`) when building functions. Please file a feature request if this restriction inconveniences you.
Run Code Online (Sandbox Code Playgroud)
根据错误,我尝试用 包装张量计算init_scope,但不确定这是否是正确的方法。建议?
小智 0
我遇到了类似的问题,但在不同的背景下。使变量初始值设定项可调用对我有用。lambda只需在像这样调用代码之前添加一个self.seqlen,看看它是否有效:
tfa.text.crf.crf_log_likelihood(y_pred, y_true, lambda: self.seqlen)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3933 次 |
| 最近记录: |