React Native with TypeScript tfjs-models/universal-sentence-encoder 模型加载错误
React Native with TypeScript tfjs-models/universal-sentence-encoder error on model load
我正在按照 these instructions 在 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}`);
}
};
}, []);
包版本:
- react-native@0.63.3
- @tensorflow-models/universal-sentence-encoder@1.3.2
- @tensorflow/tfjs@3.6.0
- @tensorflow/tfjs-react-native@0.5.0
我认为这与您导入 universal-sentence-encoder 的方式有关,试试这个:
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 提供的答案修复了引用的错误,但产生了另一个错误。需要对代码进行更多更改,@tensorflow/tfjs 需要从 3.3.0 降级到 3.0.0(在 this post 之后)。
这是更新后的代码:
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 模拟器上运行时,此代码会按预期生成嵌入,但在我的物理设备上 运行 时,它会生成一个充满 NaNs
的嵌入向量iPhone。这可能是一个单独的问题,因此请考虑此问题已解决。
我正在按照 these instructions 在 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}`);
}
};
}, []);
包版本:
- react-native@0.63.3
- @tensorflow-models/universal-sentence-encoder@1.3.2
- @tensorflow/tfjs@3.6.0
- @tensorflow/tfjs-react-native@0.5.0
我认为这与您导入 universal-sentence-encoder 的方式有关,试试这个:
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 提供的答案修复了引用的错误,但产生了另一个错误。需要对代码进行更多更改,@tensorflow/tfjs 需要从 3.3.0 降级到 3.0.0(在 this post 之后)。
这是更新后的代码:
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 模拟器上运行时,此代码会按预期生成嵌入,但在我的物理设备上 运行 时,它会生成一个充满 NaNs
的嵌入向量iPhone。这可能是一个单独的问题,因此请考虑此问题已解决。