Javascript can';t使用我的tensorflow js预测值
请有人帮帮我,帮我接通。我是tensorflow js的新手。我想了解的是,在训练数据集之后,假设我们有[{x1,y1},{x2,y2},{x3,y3}]。所以我假设我们只需要提供x就可以得到y的预测。但我没有从训练数据集后的实际情况中理解。请检查我下面的代码Javascript can';t使用我的tensorflow js预测值,javascript,tensorflow2.0,tensorflow.js,Javascript,Tensorflow2.0,Tensorflow.js,请有人帮帮我,帮我接通。我是tensorflow js的新手。我想了解的是,在训练数据集之后,假设我们有[{x1,y1},{x2,y2},{x3,y3}]。所以我假设我们只需要提供x就可以得到y的预测。但我没有从训练数据集后的实际情况中理解。请检查我下面的代码 /** * Get the car data reduced to just the variables we are interested * and cleaned of missing data. */ async funct
/**
* Get the car data reduced to just the variables we are interested
* and cleaned of missing data.
*/
async function getData() {
const carsDataResponse = await fetch('https://storage.googleapis.com/tfjs-tutorials/carsData.json');
const carsData = await carsDataResponse.json();
const cleaned = carsData.map(car => ({
mpg: car.Miles_per_Gallon,
horsepower: car.Horsepower,
}))
.filter(car => (car.mpg != null && car.horsepower != null));
//console.log(cleaned)
return cleaned;
}
//console.log(y_data)
function createModel() {
// Create a sequential model
const model = tf.sequential();
// Add a single input layer
model.add(tf.layers.dense({inputShape: [1], units: 1, useBias: true}));
// Add an output layer
model.add(tf.layers.dense({units: 1, useBias: true}));
return model;
}
// Create the model
const model = createModel();
tfvis.show.modelSummary({name: 'Model Summary'}, model);
/**
* Convert the input data to tensors that we can use for machine
* learning. We will also do the important best practices of _shuffling_
* the data and _normalizing_ the data
* MPG on the y-axis.
*/
function convertToTensor(data) {
// Wrapping these calculations in a tidy will dispose any
// intermediate tensors.
return tf.tidy(() => {
// Step 1. Shuffle the data
tf.util.shuffle(data);
// Step 2. Convert data to Tensor
const inputs = data.map(d => d.horsepower)
const labels = data.map(d => d.mpg);
const inputTensor = tf.tensor2d(inputs, [inputs.length, 1]);
const labelTensor = tf.tensor2d(labels, [labels.length, 1]);
//Step 3. Normalize the data to the range 0 - 1 using min-max scaling
const inputMax = inputTensor.max();
const inputMin = inputTensor.min();
const labelMax = labelTensor.max();
const labelMin = labelTensor.min();
const normalizedInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin));
const normalizedLabels = labelTensor.sub(labelMin).div(labelMax.sub(labelMin));
return {
inputs: normalizedInputs,
labels: normalizedLabels,
// Return the min/max bounds so we can use them later.
inputMax,
inputMin,
labelMax,
labelMin,
}
});
}
async function trainModel(model, inputs, labels) {
// Prepare the model for training.
model.compile({
optimizer: tf.train.adam(),
loss: tf.losses.meanSquaredError,
metrics: ['mse'],
});
const batchSize = 32;
const epochs = 50;
return await model.fit(inputs, labels, {
batchSize,
epochs,
shuffle: true,
callbacks: tfvis.show.fitCallbacks(
{ name: 'Training Performance' },
['loss', 'mse'],
{ height: 200, callbacks: ['onEpochEnd'] }
)
});
}
function testModel(model, inputData, normalizationData) {
const {inputMax, inputMin, labelMin, labelMax} = normalizationData;
// Generate predictions for a uniform range of numbers between 0 and 1;
// We un-normalize the data by doing the inverse of the min-max scaling
// that we did earlier.
const [xs, preds] = tf.tidy(() => {
const xs = tf.linspace(0, 1, 100);
const preds = model.predict(xs.reshape([100, 1]));
//console.log(xs);
const unNormXs = xs
.mul(inputMax.sub(inputMin))
.add(inputMin);
const unNormPreds = preds
.mul(labelMax.sub(labelMin))
.add(labelMin);
// Un-normalize the data
return [unNormXs.dataSync(), unNormPreds.dataSync()];
});
const predictedPoints = Array.from(xs).map((val, i) => {
return {x: val, y: preds[i]}
});
const originalPoints = inputData.map(d => ({
x: d.horsepower, y: d.mpg,
}));
console.log(originalPoints);
tfvis.render.scatterplot(
{name: 'Model Predictions vs Original Data'},
{values: [originalPoints, predictedPoints], series: ['original', 'predicted']},
{
xLabel: 'Horsepower',
yLabel: 'MPG',
height: 300
}
);
}
/////////////这就是我试图给出一个值来预测另一个值的地方//////////////////////
async function run() {
// Load and plot the original input data that we are going to train on.
const data = await getData();
const values = data.map(d => ({
x: d.horsepower,
y: d.mpg,
}));
tfvis.render.scatterplot(
{name: 'Horsepower v MPG'},
{values},
{
xLabel: 'Horsepower',
yLabel: 'MPG',
height: 300
}
);
// More code will be added below
}
document.addEventListener('DOMContentLoaded', run);
var y_data;
getData().then(data=>{
const y_data = data;
console.log(y_data);
const Ty_data = convertToTensor(y_data);
trainModel(model, Ty_data.inputs, Ty_data.labels);
//here am trying to predict mpg by providing horsepower
var xs_val = [horsepower: 800];
const x_xs = convertToTensor(xs_val);
console.log(x_xs);
testModel(model, x_xs.inputs, x_xs);
})
不能像这样在Javascript中解压数组
const{inputMax,inputMin,labelMin,labelMax}=normalizationData代码>
此外,请提供您得到的错误;这里提供的信息很少。谢谢你的回答,我并没有真正得到错误,但有一点我想了解,那就是如何预测,例如在我们有一系列带日期的价格的情况下。那个么,我将如何预测未来约会的价格呢?看看这个。常量xs=tf.linspace(0,1100);const preds=model.predict(xs.reformate([100,1]);所以我真正想理解的是。Predict(传递你想要预测的价格日期)如果你想知道如何用X来预测,你可以用模型在代码中进行预测。Predict
。如果您谈论的是输入数据,那么看起来您需要使用model.predict(inputData)
,或者对其进行重新排列。可能必须首先对输入数据进行规范化。你到底想问什么还不清楚。你能不能让Google meet或zoom@Andy K