我正在使用@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);
});
错误