【问题标题】:Argument 'x' passed to 'cast' must be a Tensor or TensorLike, but got 'Tensor' for React Native传递给 \'cast\' 的参数 \'x\' 必须是 Tensor 或 TensorLike,但对于 React Native 得到了 \'Tensor\'
【发布时间】:2022-10-25 08:46:16
【问题描述】:

我在 React native 上找不到名为 tensorflow/qna 的示例,我用自己的方式完成了它,但我无法摆脱这个错误。 传递给“cast”的参数“x”必须是 Tensor 或 TensorLike,但出现“Tensor”错误它想告诉我什么

不知道 tensorflow/qna 是否适合移动应用?

错误画面

    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)

代码屏幕

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>
      )
    }

【问题讨论】:

    标签: reactjs react-native tensorflow tensorflow.js


    【解决方案1】:

    我认为您不能同时导入普通的tfjsreact-native 一个

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

    您从后者仅导入 fetch 的事实并不重要,因为 tfjs 在库加载时做了很多初始化,所以您基本上遇到了冲突 - 它们都实现了 Tensor

    【讨论】:

      猜你喜欢
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多