机器学习模型中无效的元素类型

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

错误

机器学习模型中无效的元素类型

bsyang1225 回答:机器学习模型中无效的元素类型

您需要导入#dev软件包。另外,如果后端是异步的,则应使用@tensorflow/tfjs-react-native 这是您的React应用外观的示例

tf.ready()
本文链接:https://www.f2er.com/3154435.html

大家都在问