Javascript 机器学习模型中的无效元素类型
我使用的是一个简单的Javascript 机器学习模型中的无效元素类型,javascript,react-native,machine-learning,tensorflow.js,Javascript,React Native,Machine Learning,Tensorflow.js,我使用的是一个简单的@tensorflow/tfjs模型,它简单地显示了准确性。在代码沙盒中运行相同的代码时,在visual studio代码中运行时不会出现任何错误,但会出现不变类型错误。我的代码附在下面。另外,还将介绍代码中使用的输入形状和单位术语,以及如何在react native中实现此代码 import '@tensorflow/tfjs-react-native' import * as tf from "@tensorflow/tfjs"; import * as ft from
@tensorflow/tfjs
模型,它简单地显示了准确性。在代码沙盒中运行相同的代码时,在visual studio代码中运行时不会出现任何错误,但会出现不变类型错误。我的代码附在下面。另外,还将介绍代码中使用的输入形状和单位术语,以及如何在react native中实现此代码
import '@tensorflow/tfjs-react-native'
import * as tf from "@tensorflow/tfjs";
import * as ft from '@tensorflow/tfjs-backend-webgpu';
//import { writeFileSync, readFileSync } from 'fs';
(async() => {
await ft.ready
// then do all operations on the backend
})()
const model = tf.sequential({
layers: [
tf.layers.dense({ inputShape: [784], units: 32, activation: "relu" }),
tf.layers.dense({ units: 10, activation: "softmax" })
]
});
model.weights.forEach(w => {
console.log(w.name, w.shape);
});
model.weights.forEach(w => {
const newVals = tf.randomNormal(w.shape);
// w.val is an instance of tf.Variable
w.val.assign(newVals);
});
model.compile({
optimizer: "sgd",
loss: "categoricalCrossentropy",
metrics: ["accuracy"]
});
const data = tf.randomNormal([100, 784]);
const labels = tf.randomUniform([100, 10]);
function onBatchEnd(batch, logs) {
console.log("Accuracy", logs.acc);
}
// Train for 5 epochs with batch size of 32.
model
.fit(data, labels, {
epochs: 5,
batchSize: 32,
callbacks: { onBatchEnd }
})
.then(info => {
console.log("Final accuracy", info.history.acc);
});
错误呢
您需要导入
@tensorflow/tfjs react native
包。另外,如果后端是异步的,则应使用tf.ready()
下面是您的react应用程序的示例
import * as tf from '@tensorflow/tfjs';
import '@tensorflow/tfjs-react-native';
export class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isTfReady: false,
};
}
init() {
const model = tf.sequential({
layers: [
tf.layers.dense({
inputShape: [784],
units: 32,
activation: "relu"
}),
tf.layers.dense({
units: 10,
activation: "softmax"
})
]
});
model.weights.forEach(w => {
console.log(w.name, w.shape);
});
model.weights.forEach(w => {
const newVals = tf.randomNormal(w.shape);
// w.val is an instance of tf.Variable
w.val.assign(newVals);
});
model.compile({
optimizer: "sgd",
loss: "categoricalCrossentropy",
metrics: ["accuracy"]
});
const data = tf.randomNormal([100, 784]);
const labels = tf.randomUniform([100, 10]);
function onBatchEnd(batch, logs) {
console.log("Accuracy", logs.acc);
}
// Train for 5 epochs with batch size of 32.
model
.fit(data, labels, {
epochs: 5,
batchSize: 32,
callbacks: {
onBatchEnd
}
})
.then(info => {
console.log("Final accuracy", info.history.acc);
});
}
async componentDidMount() {
// Wait for tf to be ready.
await tf.ready();
// Signal to the app that tensorflow.js can now be used.
this.setState({
isTfReady: true,
});
}
render() {
init()
//
}
}
您还需要导入react本机包<代码>导入“@tensorflow/tfjs react native”@edkeveked它现在给出这个错误,就像“最高优先级后端'rn webgl'尚未初始化。请确保在调用其他metgods之前等待tf.ready()”在使用任何其他方法之前添加
等待tf.ready()
。因为后端是异步的,所以我添加了这些行,现在从“@tensorflow/tfjs backend webgpu”以ft形式导入*;const init=async()=>{await ft.ready();//现在我们可以创建张量并运行ops。};init();错误为“getpropertyAsobject:property'\u fb requireBatchedBridge'不是对象no Stack您是否按照建议添加了包“@tensorflow/tfjs react native”
?