skip to Main Content

I couldn’t find an example called tensorflow/qna on React native and I did it with my own means, but I couldn’t get rid of this error.
Argument ‘x’ passed to ‘cast’ must be a Tensor or TensorLike, but got ‘Tensor’ error what does it want to tell me

I wonder if tensorflow/qna is suitable for mobile applications?

Error screen

    Error: Argument 'x' passed to 'cast' must be a Tensor or TensorLike, but got 'Tensor'
Error: Argument 'x' passed to 'cast' must be a Tensor or TensorLike, but got 'Tensor'
    at convertToTensor (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:201029:22)
    at cast_ (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:201558:29)
    at apply (native)
    at cast__op (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:201086:30)
    at executeOp$1 (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:215647:23)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:215817:31)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:142463:20)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:142477:20)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:142458:28)
    at tidy (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:147919:23)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:215816:37)
    at executeOp (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:215835:6)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:216351:36)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:142463:20)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:142477:20)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:142458:28)
    at tidy (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:147919:23)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:216328:22)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:217079:41)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:254882:33)
    at call (native)
    at step (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:254695:25)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:254625:20)
    at fulfilled (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:254579:30)
    at tryCallOne (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:31463:16)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:31564:27)
    at apply (native)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:32004:26)
    at _callTimer (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:31892:17)
    at _callImmediatesPass (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:31931:17)
    at callImmediates (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:32148:33)
    at __callImmediates (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:3986:35)
    at anonymous (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:3765:34)
    at __guard (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:3969:15)
    at flushedQueue (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:3764:21)
    at callFunctionReturnFlushedQueue (http://10.0.3.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.agarr&modulesOnly=false&runModule=true:3746:33)

code screen

import React, { useState,useEffect } from "react";
    import { SafeAreaView,View,TextInput,Input,Button,Text } from "react-native";
    import * as tf from "@tensorflow/tfjs";
    import * as qna from "@tensorflow-models/qna";
    import {fetch} from '@tensorflow/tfjs-react-native';
    
    
    export default function DataRef() {
    
      const [question, setQuestion] = useState("When was born?");
      const [result, setResult] = useState("");
      const [passage,setPassage] =useState("Elon Reeve Musk FRS (born June 28, 1971) is a business magnate, industrial designer and engineer. He is the founder, CEO, CTO and chief designer of SpaceX; early investor, CEO and product architect of Tesla, Inc.; founder of The Boring Company; co-founder of Neuralink; and co-founder and initial co-chairman of OpenAI. In 2018, he was elected a Fellow of the Royal Society (FRS) and ranked 25th on the Forbes list of The World's Most Powerful People. A centibillionaire, Musk became the richest person in the world in January 2021, surpassing Jeff Bezos.");
      const [model,setModel] = useState(null)
      const [tfModel,setTfmodel] = useState(null);
    
    
    
      const modelLoading = async () =>{
        await tf.ready();
        const loadModel = await qna.load();
        setModel(loadModel);
        console.log("Loaded");
        
       
    
      }
    
      const handleAdd = async () => {
       
        if(model !== null){  
        const answers = await model.findAnswers(question,passage);
        setResult(answers);
        console.log(result);
        console.log("Done!");
      }else{
        alert('daha yuklenmedi')
      }
    
    
      
      
        
       
    
      };
    
      useEffect(() => {
      
        modelLoading();
       
       
    
        
      }, [])
    
      return (
        <SafeAreaView>
          <TextInput
            defaultValue={passage}
            onChange={(e)=>setPassage(e.target.value)}
          />
    
          
          <TextInput 
          placeholder={question} 
          value={question} 
          onChange={(e)=> setQuestion(e.target.value)}
          />
          <Button onPress={()=>handleAdd()} title="bas"/>
          <Text>asdasda{result}</Text>
        </SafeAreaView>
      )
    }

2

Answers


  1. i don’t think you can import both normal tfjs and react-native one

        import * as tf from "@tensorflow/tfjs";
        import {fetch} from '@tensorflow/tfjs-react-native';
    

    the fact you’re importing just fetch from latter doesn’t matter as tfjs does a lot of initializations on libary load, so you basically got a conflict – they both implement Tensor

    Login or Signup to reply.
  2. Make sure your jsEngine is configured to be ‘jsc’ instead of ‘hermes’.

    If you’re using expo.

    app.json

    {
      'expo': {
        ...
          'jsEngine': 'jsc'
        ...
      }
    }
    

    Thread: https://github.com/tensorflow/tfjs/issues/7056

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search