Welcome back.
If you remember from the previous videos, we have our linear relationship price equals to three times
size plus two plus noise.
We give the model the size in square feet which is the input layer.
We have only one number, one input.
That dense layer will start learning w and B, which are the slope and the intercept respectively.
Then in the output layer we get the price and calculate the price according to the learned equation.
We take size.
We take one number as input.
We get price which is another number.
So the input is one number.
The output is one number.
This is very important.
You should understand what are the model input and output.
Back to Android Studio we created this load model file.
Now let's create the function that will do the Their prediction.
So here function predict how many variables we need to pass.
We need to pass only one variable as an input.
So here the size input which is a float and how many outputs we get.
We need to return only one float.
Okay.
So here we need to return a float.
Let's start with val.
Input size equals to array of float array of and we pass the size input here the shape to the array
or shape to the array.
Let me move it to here.
Set the shape one one also.
Val output price.
Array of float array of zero.
Also the shape is one one.
Please pay attention with me.
TensorFlow Lite models expect multi-dimensional arrays or tensors.
Even if your model takes a single number exactly like our situation, we only need to pass the size
of the house as an input.
So we have only one single number.
It must be wrapped in arrays.
Float array of this function gives us a 1D array.
We wrap the size input into 1D array, and in order to transform this 1D array into 2D array, we use
float array of array of float array of x into transformed into 2D array.
So the size input is float.
We when we put it with float array of.
This will transform our data our float into 1D array.
If we wrapped inside array of, we get a 2D array.
So guys this is a very important step.
You should pay attention to it.
We need to transform our float into a tensor.
A tensor is a 2D array or multi-dimensional arrays.
Okay so again guys the size input is float float array of is 1D array.
Transforming the float into 1D array and array of transforming it into a 2D array similar to the output
price.
Okay, so here we are preparing an empty output container for TensorFlow Lite to fill in.
It's initialized with zero f, but will be overwritten and shape one one again one prediction result
for one input.
So the shape one prediction result for one input.
Then we need to run the interpreter.
So run inference with the input data.
Start with a return Try interpreter dot run.
Pass the input size and the output price.
Set the output price zero zero and the Logcat.
Here if we have an error log ve failed to predict the interpreter dot run.
Here we are using the interpreter that we initialized before and run it with the model.
So here we are running the model and passing the input and the output the input input size.
The output is the output price.
Since interpreter is nullable, the safe call exclamation mark dot avoids crashes if it is not initialized.
And here after inference, the result is stored inside the output, returning the prediction result.
First row first column of the 2D array stores result in the first and only element of the 2D array,
and this returns a float.
And here we have error handling in case if there is any error, if anything goes wrong, it logs the
error and returns null.
In short, this function takes a single number which is the size input feeds it into your TensorFlow
Lite TensorFlow Lite model and returns the predicted number.
It's a wrapper that shapes the input correctly.
Like as we learned here, size is a float.
We transformed it to a 1D array.
Then we transformed it to 2D array, runs the inference and extract the and returns the result safety,
storing it into the first element of the 2D array.