TfLite Model is giving different output on Android app and in python for same inputs. Outputs using python are realistic but for java it remains same. Why the outputs are behaving this way in java file?

So I’m building a very simple model using tensorflow that gives x+1 as output (prediction). I’ll deploy this model on android application so I convert it to tflite format. For Building model

import tensorflow as tf
# Create a simple Keras model.      
x = [1,2,3,4,5,6,7,8,9,10]
y = [2,3,4,5,6,7,8,9,10,11]

model = tf.keras.models.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error'), y, epochs=50)

path_file = 'saved_model/hello_world_tensorflow', path_file)

import pathlib
# Convert the model.
converter = tf.lite.TFLiteConverter.from_saved_model(path_file)
tflite_model = converter.convert()
tflite_model_file = pathlib.Path('model1.tflite')

Using model in Python code for getting output

import numpy as np
import tensorflow as tf

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="model1.tflite")

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array([[3]], dtype=np.float32) # 3 is the input here
interpreter.set_tensor(input_details[0]['index'], input_data)


# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])

Using model in Java Code ( File ) android

package ar.labs.androidml;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.tensorflow.lite.DataType;

import java.nio.ByteBuffer;


public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        Button btn= findViewById(;
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                    EditText inputEditText;

                    inputEditText = findViewById(;
                    Float data= Float.valueOf(inputEditText.getText().toString());
                    ByteBuffer byteBuffer= ByteBuffer.allocateDirect(1*4);

                    Model1 model = Model1.newInstance(getApplicationContext());

                    // Creates inputs for reference.
                    TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 1}, DataType.FLOAT32);

                    // Runs model inference and gets result.
                    Model1.Outputs outputs = model.process(inputFeature0);
                    TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();

                    // Releases model resources if no longer used.
                    TextView tv= findViewById(;
                    float[] data1=outputFeature0.getFloatArray();



                catch (Exception e)

Python code:

  • Input → Output
  • 1-> 1.4467…
  • 2 → 2.5395…
  • 3 → 3.6323…

Java Code

  • Input → Output
  • 1 → 0.3540…
  • 2 → 0.3540…
  • 2.1 → 2.967…E23
  • 2.11 → 0.39083…
  • 41 → 0.3540…

Why are outputs from Python code and Java code are so different for same input?
Why the outputs are behaving this way in java file like returning a constant value for most cases?
Please help me fix.

Please refer to the solution here