机器学习模型中的无效元素类型
inavalid element type in machine learning model
我使用的是 @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()
//
}
}
我使用的是 @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()
//
}
}