tensorflow / tensorflow/java

Unexpected behavior from `tf.math.greaterEqual`

Open
#177 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
928
Forks
227
PR merge metrics
No merged PRs in 30d

Description

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
    Custom code
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
    macOS BigSur 11.1
  • TensorFlow installed from (source or binary):
    TensorFlow Java latest GitHub, based on karl/tensor-ttype

I have the following code snippet.

       Operand<TFloat32> r = tf.random.randomUniform(tf.constant(Shape.of(3,2)), TFloat32.class,   
                                                         RandomUniform.seed(1001L));
      Operand<TFloat32> rate1 = tf.constant(0.5f);
      Operand<TBool> mask = tf.math.greaterEqual(r, rate1);

The generated random sequence (shape 3,2) is:

0,0). 0.028465
0,1). 0.936395
1.0). 0.035310
1,1). 0.230565
2,0). 0.269415
2,1). 0.145439

Describe the current behavior
When I run this code, I get mask values of:

0,0). false
0,1). true
1,0). false
1,1). false
2,0). false
2,1). true

Describe the expected behavior
I expected mask values to be:

0,0). false
0,1). true
1,0). false
1,1). false
2,0). false
2,1). false

The last random number, 0.145439, is less than 0.5.

Code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate the problem.

try (TestSession session = TestSession.createTestSession(TestSession.Mode.GRAPH)) {
      Ops tf = session.getTF();
       Operand<TFloat32> random = tf.random.randomUniform(tf.constant(Shape.of(3,2), TFloat32.class,   
                                                         RandomUniform.seed(1001L));
      Operand<TFloat32> rate = tf.constant(0.5f);
      Operand<TBool> mask = tf.math.greaterEqual(r, rate);

      System.out.println("******** MASK ***************");
      System.out.println("random:");
      session.print(random);
      System.out.println("rate1:");
      session.print(rate1);
      System.out.println("mask:");
      session.print(mask);
      System.out.println("******** MASK ***************");
}

If I do this code in Python it works as expected:

import tensorflow as tf
a = [
    [0.028465 ,  0.936395],
    [0.035310 , 0.230565],
    [0.269415 , 0.145439]
]
print(a);
rate = 0.5
b = tf.math.greater_equal(a, rate);
print(b)

Output:

[[0.028465, 0.936395], [0.03531, 0.230565], [0.269415, 0.145439]]
tf.Tensor(
[[False  True]
 [False False]
 [False False]], shape=(3, 2), dtype=bool)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Java snippet and the TestSession in GRAPH mode, then compare the generated tensor and tf.math.greaterEqual result with the Python example. Reproduce the reported final-element mismatch and identify whether the Java binding or the test setup produces it; done means the behavior is explained and a regression test or corrective change is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.