假设您有一个内部带有acos的Keras损失函数,并且想要保护acos免受无效值([-1,1]
之外的值)的影响。即,将<-1设置为-1,将1设置为1。
你该怎么做?
我已经考虑过使用tf.cond
,但不确定如何正确应用它:
def loss(y_true, y_pred):
# typically in [-1,1], but maybe slightly smaller/larger than that due to num precision
unprotected_acos_input = ... # shape: [batchsize]
shape = unprotected_acos_input.shape[0]
cond_low = tf.constant(-1, shape=[shape], dtype="float32")
cond_high = tf.constant(1, shape=[shape], dtype="float32")
protected_acos_input = tf.cond(tf.greater_equal(unprotected_acos_input, cond_low), lambda: unprotected_acos_input, lambda: tf.constant(-1, shape=[shape], dtype="float32"))
# same for cond_high
loss = tf.acos(protected_acos_input)
return loss
如果批次中只有一个值在[-1,1]之外,这会将所有 acos输入设置为1或-1。如何只更改acos_input范围之外的项目,而不是所有项目?
答案 0 :(得分:1)
您可以使用 @objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 52, right: 0)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed:Bool) in
})
}
}
。
K.clip(unprotected_acos_input, -1, 1)