-
Notifications
You must be signed in to change notification settings - Fork 221
Feature/add t bool test #628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
winne42
wants to merge
2
commits into
tensorflow:master
Choose a base branch
from
winne42:feature/add-TBoolTest
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package org.tensorflow.types; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.tensorflow.EagerSession; | ||
| import org.tensorflow.ndarray.NdArray; | ||
| import org.tensorflow.ndarray.NdArrays; | ||
| import org.tensorflow.ndarray.Shape; | ||
| import org.tensorflow.ndarray.index.Indices; | ||
| import org.tensorflow.op.Ops; | ||
| import org.tensorflow.op.core.Constant; | ||
| import org.tensorflow.op.math.LogicalAnd; | ||
| import org.tensorflow.op.math.LogicalNot; | ||
| import org.tensorflow.op.math.LogicalOr; | ||
|
|
||
| class TBoolTest { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be public to match the rest of the tests. |
||
|
|
||
| @Test | ||
| void createScalar() { | ||
| TBool tensorT = TBool.scalarOf(true); | ||
| assertNotNull(tensorT); | ||
| assertEquals(Shape.scalar(), tensorT.shape()); | ||
| assertEquals(true, tensorT.getObject()); | ||
|
|
||
| TBool tensorF = TBool.scalarOf(false); | ||
| assertNotNull(tensorF); | ||
| assertEquals(Shape.scalar(), tensorF.shape()); | ||
| assertEquals(false, tensorF.getObject()); | ||
| } | ||
|
|
||
| @Test | ||
| void createVector() { | ||
| TBool tensor = TBool.vectorOf(true, false); | ||
| assertNotNull(tensor); | ||
| assertEquals(Shape.of(2), tensor.shape()); | ||
| assertEquals(true, tensor.getObject(0)); | ||
| assertEquals(false, tensor.getObject(1)); | ||
| } | ||
|
|
||
| @Test | ||
| void createCopy() { | ||
| NdArray<Boolean> bools = | ||
| NdArrays.ofObjects(Boolean.class, Shape.of(2, 2)) | ||
| .setObject(true, 0, 0) | ||
| .setObject(false, 0, 1) | ||
| .setObject(false, 1, 0) | ||
| .setObject(true, 1, 1); | ||
|
|
||
| TBool tensor = TBool.tensorOf(bools); | ||
| assertNotNull(tensor); | ||
| bools | ||
| .scalars() | ||
| .forEachIndexed((idx, s) -> assertEquals(s.getObject(), tensor.getObject(idx))); | ||
| } | ||
|
|
||
| @Test | ||
| void initializeTensorsWithBools() { | ||
| // Allocate a tensor of booleans of the shape (2, 3, 2) | ||
| TBool tensor = TBool.tensorOf(Shape.of(2, 3, 2)); | ||
|
|
||
| assertEquals(3, tensor.rank()); | ||
| assertEquals(12, tensor.size()); | ||
| NdArray<Boolean> data = (NdArray<Boolean>) tensor; | ||
|
|
||
| try (EagerSession session = EagerSession.create()) { | ||
| Ops tf = Ops.create(session); | ||
|
|
||
| // Initialize tensor memory with falses and take a snapshot | ||
| data.scalars().forEach(scalar -> ((NdArray<Boolean>) scalar).setObject(false)); | ||
| Constant<TBool> x = tf.constantOf(tensor); | ||
|
|
||
| // Initialize the same tensor memory with trues and take a snapshot | ||
| data.scalars().forEach(scalar -> ((NdArray<Boolean>) scalar).setObject(true)); | ||
| Constant<TBool> y = tf.constantOf(tensor); | ||
|
|
||
| // Calculate x AND y and validate the result | ||
| LogicalAnd xAndY = tf.math.logicalAnd(x, y); | ||
| ((NdArray<Boolean>) xAndY.asTensor()) | ||
| .scalars() | ||
| .forEach(scalar -> assertEquals(false, scalar.getObject())); | ||
|
|
||
| // Calculate x AND y and validate the result | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be OR not AND. |
||
| LogicalOr xOrY = tf.math.logicalOr(x, y); | ||
| ((NdArray<Boolean>) xOrY.asTensor()) | ||
| .scalars() | ||
| .forEach(scalar -> assertEquals(true, scalar.getObject())); | ||
|
|
||
| // Calculate !x and validate the result against y | ||
| LogicalNot notX = tf.math.logicalNot(x); | ||
| assertEquals(y.asTensor(), notX.asTensor()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void setAndCompute() { | ||
| NdArray<Boolean> heapData = | ||
| NdArrays.ofBooleans(Shape.of(4)) | ||
| .setObject(true, 0) | ||
| .setObject(false, 1) | ||
| .setObject(true, 2) | ||
| .setObject(false, 3); | ||
|
|
||
| // Creates a 2x2 matrix | ||
| try (TBool tensor = TBool.tensorOf(Shape.of(2, 2))) { | ||
| NdArray<Boolean> data = (NdArray<Boolean>) tensor; | ||
|
|
||
| // Copy first 2 values of the vector to the first row of the matrix | ||
| data.set(heapData.slice(Indices.range(0, 2)), 0); | ||
|
|
||
| // Copy values at an odd position in the vector as the second row of the matrix | ||
| data.set(heapData.slice(Indices.odd()), 1); | ||
|
|
||
| assertEquals(true, data.getObject(0, 0)); | ||
| assertEquals(false, data.getObject(0, 1)); | ||
| assertEquals(false, data.getObject(1, 0)); | ||
| assertEquals(false, data.getObject(1, 1)); | ||
|
|
||
| // Read rows of the tensor in reverse order | ||
| NdArray<Boolean> flippedData = data.slice(Indices.flip(), Indices.flip()); | ||
|
|
||
| assertEquals(false, flippedData.getObject(0, 0)); | ||
| assertEquals(false, flippedData.getObject(0, 1)); | ||
| assertEquals(false, flippedData.getObject(1, 0)); | ||
| assertEquals(true, flippedData.getObject(1, 1)); | ||
|
|
||
| try (EagerSession session = EagerSession.create()) { | ||
| Ops tf = Ops.create(session); | ||
|
|
||
| LogicalNot sub = tf.math.logicalNot(tf.constantOf(tensor)); | ||
| NdArray<Boolean> result = (NdArray<Boolean>) sub.asTensor(); | ||
|
|
||
| assertEquals(false, result.getObject(0, 0)); | ||
| assertEquals(true, result.getObject(0, 1)); | ||
| assertEquals(true, result.getObject(1, 0)); | ||
| assertEquals(true, result.getObject(1, 1)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the same copyright statement as the other tests.