Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Update min(_:_:) and max(_:_:) gradients to match Python TensorFlow #480

Merged
merged 10 commits into from
Aug 26, 2019

Conversation

jon-tow
Copy link
Contributor

@jon-tow jon-tow commented Aug 24, 2019

Fixes #479

@jon-tow
Copy link
Contributor Author

jon-tow commented Aug 24, 2019

@dan-zheng @rxwei This should be ready.

Copy link
Member

@dan-zheng dan-zheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the prompt fix! 🙂

@dan-zheng
Copy link
Member

dan-zheng commented Aug 24, 2019

There appears to be one failure:

Test Case 'LossTests.testSigmoidCrossEntropyGrad' started at 2019-08-24 07:41:17.269
/swift-apis/Tests/TensorFlowTests/LossTests.swift:219: error: LossTests.testSigmoidCrossEntropyGrad : XCTAssertEqual failed: ("0.125") is not equal to ("0.0625") +/- ("1e-06") -

It's likely because sigmoidCrossEntropy internally calls max:

@differentiable(wrt: logits)
public func sigmoidCrossEntropy<Scalar: TensorFlowFloatingPoint>(
    logits: Tensor<Scalar>,
    labels: Tensor<Scalar>,
    reduction: @differentiable (Tensor<Scalar>) -> Tensor<Scalar> = _mean
) -> Tensor<Scalar> {
    let maxLogitsWithZero = max(logits, Tensor(0)) // `max` called here
    let result = log(1 + exp(-abs(logits)))
    return reduction(maxLogitsWithZero - logits * labels + result)
}

Could you please fix? Updating the expected value in the test should be good - validating the expected value against tf.losses.sigmoid_cross_entropy would be great (I'm not sure how expectedGradientsBeforeMean was computed in the test).

@jon-tow
Copy link
Contributor Author

jon-tow commented Aug 24, 2019

tf.losses.sigmoid_cross_entropy is computed via tf.python.ops.nn_impl.sigmoid_cross_entropy_with_logits. which is a bit different than the swift-api version in that it uses custom max and abs in order to compute gradients at 0. I'm still trying to find my way around this but will update as soon as possible.

Copy link
Member

@dan-zheng dan-zheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jon-tow for the many adjustments!

@jon-tow
Copy link
Contributor Author

jon-tow commented Aug 26, 2019

No problem @dan-zheng. Thanks for the guidance!

@dan-zheng dan-zheng merged commit b7f2a06 into tensorflow:master Aug 26, 2019
@jon-tow jon-tow deleted the gradient/min-max branch August 26, 2019 21:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update 'min(_:_:)' and 'max(_:_:)' gradients to match Python TensorFlow.
6 participants