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

Implement isAlmostEqual method for floating-point tensors. #348

Merged
merged 3 commits into from
Jul 15, 2019

Conversation

dominikgrewe
Copy link
Contributor

No description provided.

@dominikgrewe dominikgrewe requested a review from dan-zheng July 10, 2019 18:11
@eaplatanios
Copy link
Contributor

Can we change the test helpers that test for approximate equality to use this function? Also, isn't isApproximatelyEqual a better name for this?

to other: Tensor,
tolerance: Scalar = Scalar.ulpOfOne.squareRoot()
) -> Bool {
return self.shape == other.shape &&
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use Tensor.elementsApproximatelyEqual(_:tolerance:) directly to make this faster. Also, maybe we should also add the to argument label to all these other functions that check for element-wise equality.

Copy link
Contributor

@rxwei rxwei Jul 10, 2019

Choose a reason for hiding this comment

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

Also, maybe we should also add the to argument label to all these other functions that check for element-wise equality.

This elementsApproximatelyEqual(_:tolerance:) method name is a sentence rather than a verb phrase, and "equal" here is a transitive verb. Same for Sequence.elementsEqual(_:).

We should change "approximately" to "almost" for consistency with isAlmostEqual(to:).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've renamed elementsApproximatelyEqual to elementsAlmostEqual and this is now called from isAlmostEqual. For consistency I also changed the condition on Scalar to now conform to TensorFlowFloatingPoint so that it's convertible to Double. Ptal.

@rxwei
Copy link
Contributor

rxwei commented Jul 10, 2019

Can we change the test helpers that test for approximate equality to use this function? Also, isn't isApproximatelyEqual a better name for this?

This is to follow SE-0259. If that gets changed, we should change ours to follow.

XCTAssertTrue(x.isAlmostEqual(to: y, tolerance: 0.01))
}

func testIsNotAlmostEqual() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be better to put this also in testIsAlmostEqual() since they are both testing the same function (i.e., isAlmostEqual).

* Rename `elementsApproximatelyEqual` to `elementsAlmostEqual`
* Use `elementsAlmostEqual` in implementation of `isAlmostEqual`
* Add tests for nan and infinity.
Copy link
Contributor

@rxwei rxwei left a comment

Choose a reason for hiding this comment

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

Looking great!

@rxwei rxwei merged commit eb996cd into tensorflow:master Jul 15, 2019
to other: Tensor,
tolerance: Scalar = Scalar.ulpOfOne.squareRoot()
) -> Bool {
return self.shape == other.shape &&
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we want to have this check for mismatching shapes here. @rxwei wouldn't a precondition be better? I believe it would be misleading for this to return false when the shapes are mismatching as it should be expected that it operates over tensors with matching shapes.

Another issue is that of broadcasting. If we want to support broadcasting for this function we should remove that check altogether.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

@dominikgrewe dominikgrewe deleted the allcloseto branch July 15, 2019 20:10
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.

4 participants