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.

[tfjs-react-native] Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'a.substr')

See original GitHub issue

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 20.04
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: Pixel_3a_API_30_x86.avd
  • TensorFlow.js installed from (npm or script link):
  • TensorFlow.js version: npm install @tensorflow/tfjs,
  • CUDA/cuDNN version:

Describe the problem

I’m trying to run some sample function found in your documentation https://js.tensorflow.org/api/latest/#tf.LayersModel.fit in a react-native-app.

if i run this example on the web executing npm run web and check the browser window it loads, it works fine.

If however i run the same example by running npm run web then scan the QR code using my android phone, or if i hit a to run a android emulator, in both cases i get the following error. [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'a.substr')]

What is it about the function do_training_example that ive introduced that it does not like when running via the QR code on my phone or via the emulator. If i remove this function the error goes.

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'a.substr')]
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3754:8 in isMobile
at node_modules/@tensorflow/tfjs-backend-webgl/dist/tf-backend-webgl.node.js:1043:17 in <global>
at http://192.168.1.104:19001/index.bundle?platform=android&dev=true&hot=false&minify=false:161631:39 in get
at http://192.168.1.104:19001/index.bundle?platform=android&dev=true&hot=false&minify=false:249141:41 in runWebGLProgram
at node_modules/@tensorflow/tfjs-backend-webgl/dist/tf-backend-webgl.node.js:5217:12 in MathBackendWebGL.prototype.uploadToGPU
at http://192.168.1.104:19001/index.bundle?platform=android&dev=true&hot=false&minify=false:249103:25 in <unknown>
at [native code]:null in map
at node_modules/@tensorflow/tfjs-backend-webgl/dist/tf-backend-webgl.node.js:5016:8 in MathBackendWebGL.prototype.runWebGLProgram
at node_modules/@tensorflow/tfjs-backend-webgl/dist/tf-backend-webgl.node.js:8060:37 in int
at http://192.168.1.104:19001/index.bundle?platform=android&dev=true&hot=false&minify=false:163816:35 in kernelFunc
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3199:17 in scopedRun$argument_2
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3011:12 in Engine.prototype.scopedRun
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3195:12 in Engine.prototype.runKernelFunc
at node_modules/@tensorflow/tfjs/dist/tf.node.js:3548:12 in f2
at node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:24702:13 in tfc.tidy$argument_0
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3001:126 in scopedRun$argument_2
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3011:12 in Engine.prototype.scopedRun
at [native code]:null in map
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3001:126 in scopedRun$argument_2
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3011:12 in Engine.prototype.scopedRun
at node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:24838:87 in tfc.tidy$argument_0
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3001:126 in scopedRun$argument_2
at node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3011:12 in Engine.prototype.scopedRun
at http://192.168.1.104:19001/index.bundle?platform=android&dev=true&hot=false&minify=false:206783:36 in <unknown>
at http://192.168.1.104:19001/index.bundle?platform=android&dev=true&hot=false&minify=false:199674:24 in step
at node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:16694:22 in step
at node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:16694:22 in step
at node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:16681:40 in fulfilled
at node_modules/react-native/node_modules/promise/setimmediate/core.js:37:13 in tryCallOne
at node_modules/react-native/node_modules/promise/setimmediate/core.js:123:24 in setImmediate$argument_0
at [native code]:null in flushedQueue
at [native code]:null in invokeCallbackAndReturnFlushedQueue

Any other info / logs

You can see ive only added the function do_training_example and executing that within the componentDidMount

App.tsc

import * as tf from '@tensorflow/tfjs';
import '@tensorflow/tfjs-react-native';
import React, { Component } from 'react';
import { Text, View } from 'react-native';
export default class App extends React.Component {
    constructor(props: any) {
        super(props);
        this.state = {
            isTfReady: false,
        };
    }

    async componentDidMount() {
        // Wait for tf to be ready.
        await tf.ready();

        // Signal to the app that tensorflow.js can now be used.
        this.setState({
            isTfReady: true,
        });


        await this.do_training_example();

    }

    do_training_example = async () => {

        const model = tf.sequential({
            layers: [tf.layers.dense({ units: 1, inputShape: [10] })]
        });
        model.compile({ optimizer: 'sgd', loss: 'meanSquaredError' });
        for (let i = 1; i < 5; ++i) {
            const h = await model.fit(tf.ones([8, 10]), tf.ones([8, 1]), {
                batchSize: 4,
                epochs: 3
            });
            console.log("Loss after Epoch " + i + " : " + h.history.loss[0]);
        }

    }


    render() {
        return (
          <View style={{
              flex: 1,
              justifyContent: "center",
              alignItems: "center"
            }}>
            <Text>Hello, world!</Text>
          </View>
        );
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MareSeesterncommented, Apr 3, 2021

Choosing a backend with

tf.setBackend('cpu')

solved the problem for me.

1reaction
carrycooldudecommented, Mar 14, 2021

Yes true this is bug , Version issue

On Sun, 14 Mar 2021, 20:38 Ralf Gutkowski, @.***> wrote:

Same here on expo while using expo-gl. It seems that tensorflow > 3.0.0 have issues with environment detection.

If you don’t want to downgrade and you can wait until this is fixed, knowing your app is only going to run on mobile, you can place this code early in your program to avoid this error:

/**

  • Original functions throw an error in:
  • @tensorflow/tfjs-core/dist/tf-core.node.js:3749
  • This may be related to TFJS as it for some reason tries to load node webgl
  • code instead of using expo-gl bridge.
  • Not sure how to figure it out so here’s a workaround. */ tf.device_util.isMobile = () => true tf.device_util.isBrowser = () => false

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/tfjs/issues/4806#issuecomment-798923670, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJZ4ZSFVH74PFPSHKEGN3HTTDTGO3ANCNFSM4ZAWKYLA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can i solve this Possible Unhandled promise rejection ...
This is a bug with tfjs-react-native. If you downgrade @tensorflow/tfjs to 3.0.0, it should work.
Read more >
TypeError: undefined is not an object (evaluating 'x.mul')] ...
I'm trying to get a pre-trained model loaded onto react native. When I try to run it, it gives me the error: [Unhandled...
Read more >
Getting Error 'Undefined Is Not An Object' When Using ...
[Unhandled promise rejection: TypeError: undefined is not an object evaluating 'a.substr']. What is it about the function dotrainingexample ...
Read more >
undefined is not an object (evaluating 'response.data')
But I always get this error when submiting a form. Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'response.data'). Any ideas ......
Read more >
React Native Auth0 Possible Unhandled Promise Rejection
Possible Unhandled Promise Rejection (id: 1): TypeError: undefined is not an object (evaluating '_yield$getCredentials.accessToken').
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