question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React Native Bug? - tf.Tensor changes values

See original GitHub issue

using

@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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Zurdgecommented, Jul 10, 2020

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 error The 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 🎣

import '@tensorflow/tfjs-react-native/dist/platform_react_native';
import { util } from '@tensorflow/tfjs-core';
// IF RUNNING IN EMULATOR
// UNCOMMENT THIS LINE
//tf.setBackend('cpu');

Loader Class 🐟

The code below is a simple loader which can be used in a tf.loadLayersModel function

class CustomLoader{
  constructor(...props){
    this.modelJSON = props[0];
    this.modelWeights = props[1];
  }

  load = async()=>{
    const modelJson = await this.loadJSON()
    const weightData = await this.loadWeights()
    let modelArtifacts = modelJson;
    modelArtifacts.weightSpecs = modelJson.weightsManifest[0].weights;
    modelArtifacts.weightData = weightData;
    return(modelArtifacts)
  }
  loadWeights = async()=>{
    const RNFS = require('react-native-fs');
    var base64Weights = await RNFS.readFileAssets(this.modelWeights, 'base64');
    const weightData = util.encodeString(base64Weights, 'base64').buffer;
    return weightData;
  }
  loadJSON = async()=>{
    const RNFS = require('react-native-fs');
    var baseJSON = await RNFS.readFileAssets(this.modelJSON, 'ascii');
    const result = JSON.parse(baseJSON)
    return result;
  }
}

Load the model 🍣

Android
  • In Android Studio, right click root folder > New > Folder > Assets Folder.
  • Add your model files into the assets folder.
iOS
  • Not done this yet
const Loader = new CustomLoader("model/model.json", "model/group1-shard1of1.bin");
const model = await tf.loadLayersModel(Loader);
model.summary()

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 😸

0reactions
rthadurcommented, Jul 9, 2020

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

lstm - tfjs Error: Argument tensors passed to stack must be a ...
I think this is a bug in tf.js namely RNN layers don't accept sequences with just one element (throws an error when fitting)....
Read more >
tf.io.RequestDetails - TensorFlow.js React Native API
tfjs-react-native provides a TensorFlow.js platform adapter for react native. ... Concatenated binary weight values, stored as a base64-encoded string.
Read more >
Kindacode - The World Of Code
Popular Topics. Node.js · Flutter · React · Next.js · React Native · TypeScript · Docker · Databases · HTML & CSS ·...
Read more >
Tensorflow Owner - Stack Exchange Data Explorer
'Can ReLU handle a negative input?', 'Clarification on tf.Tensor.set_shape()', 'How can I use Tensorflow with react-native?
Read more >
Differences between a Matrix and a Tensor - GeeksforGeeks
When the coordinate systems change, the entries of a tensor also transform ... in a system and changes its values when other values...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found