tensorflow 传递给“cast”的参数“x”必须是Tensor或TensorLike,但为React Native获得了“Tensor”

qlvxas9a  于 2022-11-16  发布在  React
关注(0)|答案(1)|浏览(191)

我在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>
      )
    }
ohtdti5x

ohtdti5x1#

我不认为你可以同时导入普通的tfjsreact-native

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

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

相关问题