React Native Bug? - tf.Tensor changes values
See original GitHub issueusing
“@tensorflow/tfjs”: “^2.0.1”, “@tensorflow/tfjs-react-native”: “^0.3.0”,
tf.tensor seems to be changing the values slightly which results in faulty predictions.
I’m building a react native app and have followed the instructions by building it with expo etc. Tensorflow itself is running correctly however I’m not getting any predictions. After looking into the tensor’s values I noticed they have changed (slightly, but enough to not work)
Example
Its easy to reproduce on my end. Simply test this code.
var dataArray = [0.05,0.05,0.15,0.13,0.15]
var dataset = await tf.tensor(dataArray);
console.log(dataArray) <----- 1
console.log(dataset.arraySync()) <--- 2
Output
#1 - [0.05, 0.05, 0.15, 0.13, 0.15]
#2 - [0.05000000074505806, 0.05000000074505806, 0.15000000596046448, 0.12999999523162842, 0.15000000596046448]<< These have changed from the original values
I trained my model on values which were fixed to only 2 decimal places. My model works in python and browser Javascript. however in React Native my model doesn’t predict correctly. I think the reason my model isn’t generating the correct predictions is because the values are changing from 0.05
to 0.05000000074505806
etc
Has anyone else experienced this?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:11 (3 by maintainers)
Top GitHub Comments
Summary of issue
Using
import "@tensorflow/tfjs-react-native"
was causing some issues. There were two issues which stem out from this.1 - One was
WebGL
in the Android emulator was causing Tensorflow results to default to[0]
.2 - The second was that something inside
import "@tensorflow/tfjs-react-native"
was causing expo to not build correctly throwing the errorThe hashAssetFiles Metro plugin is not configured. You need to add a metro.config.js to your project that configures Metro to use this plugin. See https://github.com/expo/expo/blob/master/packages/expo-updates/README.md#metroconfigjs for an example.
inside Android Studio.Temp solution
Below is a solution I worked on which solves the hurdles I came across. Maybe it will help others too 🤞
Imports 🎣
Loader Class 🐟
The code below is a simple loader which can be used in a
tf.loadLayersModel
functionLoad the model 🍣
Android
iOS
Full Gist
https://gist.github.com/Zurdge/cc44b68c17180bef25a4aa8d49d2083c
@tafsiri @rthadur Thank you for your help. The above isn’t a perfect solution but I will close this issue as I am able to progress with the project using TensorflowJS in React Native now.
If you folks develop a solution which allows the backend to be set to GPU whilst running in the emulator and also more information about that expo build error I would be interested to hear about it.
All the best 😸
@Zurdge I believe this has been cased by
expo
please check here for similar issue, if not we will wait for @tafsiri to comment on this, thank you