开发者问题收集

用打字稿TFJS模型/通用句子编码器在模型负载上进行反应本机

2021-05-12
536

我正在尝试按照 这些说明 在 React Native 应用中使用 tfjs-models/universal-sentence-encoder。但是,当我尝试加载模型时,我收到以下错误:

ERROR: TypeError: undefined is not an object (evaluating '_universalSentenceEncoder.default.load'

代码:

import React, { useEffect, useState } from 'react';
require('@tensorflow/tfjs');
const use = require('@tensorflow-models/universal-sentence-encoder');

export default function App() {

  useEffect(() => {
    console.log("App is starting...")
    
    const init = async () => {
      // initialize state variables 
      // console.log("App is initializing services...")
      
      // Load the model.
      try {
        use.load().then((model: any) => {
          // Embed an array of sentences.
          const sentences = [
            'Hello.',
            'How are you?'
          ];
          model.embed(sentences).then((embeddings: any) => {
            // `embeddings` is a 2D tensor consisting of the 512-dimensional embeddings for each sentence.
            // So in this example `embeddings` has the shape [2, 512].
            embeddings.print(true /* verbose */);
          });
        });
      }
      catch (err) {
        console.log(`ERROR: ${err}`);
      }
    };
  }, []);

软件包版本:

2个回答

我认为这与您导入通用句子编码器的方式有关,请尝试以下方法:

import React, { useEffect, useState } from 'react';
import * as use from '@tensorflow-models/universal-sentence-encoder';

export default function App() {

  useEffect(() => {
    console.log("App is starting...")
    
    const init = async () => {
      // initialize state variables 
      // console.log("App is initializing services...")
      
      // Load the model.
      try {
        use.load().then((model: any) => {
          // Embed an array of sentences.
          const sentences = [
            'Hello.',
            'How are you?'
          ];
          model.embed(sentences).then((embeddings: any) => {
            // `embeddings` is a 2D tensor consisting of the 512-dimensional embeddings for each sentence.
            // So in this example `embeddings` has the shape [2, 512].
            embeddings.print(true /* verbose */);
          });
        });
      }
      catch (err) {
        console.log(`ERROR: ${err}`);
      }
    };
  }, []);
yudhiesh
2021-05-13

yudhiesh 提供的答案修复了所引用的错误,但产生了另一个错误。代码还需要进行一些更改,并且需要将 @tensorflow/tfjs 从 3.3.0 降级到 3.0.0(参见 这篇帖子 )。

这是更新后的代码:

import React, { useEffect, useState } from 'react';
import "@tensorflow/tfjs-react-native";
import * as tf from '@tensorflow/tfjs';
import * as use from '@tensorflow-models/universal-sentence-encoder';

export default function App() {

  useEffect(() => {
    console.log("App is starting...")
    
    const init = async () => {
      // initialize state variables 
      // console.log("App is initializing services...")
      
      await tf.ready();
      
      // Load the model.
      try {
        use.load().then((model: any) => {
          // Embed an array of sentences.
          const sentences = [
            'Hello.',
            'How are you?'
          ];
          model.embed(sentences).then((embeddings: any) => {
            // `embeddings` is a 2D tensor consisting of the 512-dimensional embeddings for each sentence.
            // So in this example `embeddings` has the shape [2, 512].
            embeddings.print(true /* verbose */);
          });
        });
      }
      catch (err) {
        console.log(`ERROR: ${err}`);
      }
    };
  }, []);

请注意 @tensorflow/tfjs-react-native 的导入、如何导入 @tensorflow/tfjs@tensorflow-models/universal-sentence-encoder 的变化,以及添加调用 await tf.ready() 以确保在尝试加载使用之前初始化 tensorflow模型。

此代码在 iOS 模拟器上运行时按预期生成嵌入,但在我的物理 iPhone 上运行时,它生成一个充满 NaN 的嵌入向量。这可能是一个单独的问题,因此请认为此问题已解决。

nimbus
2021-05-13