我正在尝试建立一个简单的线性回归模型,我的数据集是从1到10的数字。我正在尝试训练该模型以预测对于任何给定的输出(例如3),输出应为值(y = x
的输入。
预测总是错误的。有人可以告诉我我在做什么错吗?
const tf = require("@tensorflow/tfjs");
const xArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const yArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const createModel = () => {
const model = tf.sequential();
model.add(tf.layers.dense({ inputShape: [1], units: 1, useBias: true })); //input layer
model.add(tf.layers.dense({ units: 1, useBias: true })); //output layer
return model;
};
const convertToTensor = () => {
return tf.tidy(() => {
const inputTensor = tf.tensor2d(xArray, [xArray.length, 1]);
const outputTensor = tf.tensor2d(yArray, [yArray.length, 1]);
return {
inputs: inputTensor,
outputs: outputTensor,
};
});
};
async function trainModel(model, inputs, trueValues) {
model.compile({
optimizer: tf.train.adam(),
loss: tf.losses.meanSquaredError,
metrics: ["mse"]
});
return await model.fit(inputs, trueValues, {
batchSize: 2,
epochs: 5,
learningRate: 0.04
});
}
function testModel(model, testValue) {
return tf.tidy(() => model.predict(tf.tensor2d([testValue], [1, 1]));
}
const run = async testValue => {
const model = createModel();
const tensorData = convertToTensor();
await trainModel(model, tensorData.inputs, tensorData.outputs);
const prediction = testModel(model, testValue);
console.log(prediction.toString());
};
run(5);
答案 0 :(得分:0)
您的代码有两个问题:
function ParseErrorForResponseBody($Error) {
if ($PSVersionTable.PSVersion.Major -lt 6) {
if ($Error.Exception.Response) {
$Reader = New-Object System.IO.StreamReader($Error.Exception.Response.GetResponseStream())
$Reader.BaseStream.Position = 0
$Reader.DiscardBufferedData()
$ResponseBody = $Reader.ReadToEnd()
if ($ResponseBody.StartsWith('{')) {
$ResponseBody = $ResponseBody | ConvertFrom-Json
}
return $ResponseBody
}
}
else {
return $Error.ErrorDetails.Message
}
}
try
{
Invoke-restmethod -method post -uri $uri -infile $fullpath -headers $Headers -credential $credential
}
catch
{
ParseErrorForResponseBody($_)
}
值。您需要将其作为第一个参数传递给tf.train.adam()
函数learningRate
个时代,这对您来说还不够。我从下面的代码中删除了不必要的代码。您可以更改5
和epochs
的值,以查看它如何影响learning rate
的预测结果。我将纪元的默认值更改为5
。预测最终非常接近50
。
5
document.querySelector('button').addEventListener('click', async () => {
const learningRate = document.querySelector('#learning_rate').value;
const epochs = document.querySelector('#epochs').value;
const xArray = [0,1,2,3,4,5,6,7,8,9];
const yArray = [0,1,2,3,4,5,6,7,8,9];
const createModel = () => {
const model = tf.sequential();
model.add(tf.layers.dense({ inputShape: [1], units: 1, useBias: true }));
model.add(tf.layers.dense({ units: 1, useBias: true }));
return model;
};
const convertToTensor = () => {
return tf.tidy(() => {
const inputTensor = tf.tensor2d(xArray, [xArray.length, 1]);
const outputTensor = tf.tensor2d(yArray, [yArray.length, 1]);
return {
inputs: inputTensor,
outputs: outputTensor,
};
});
};
async function trainModel(model, inputs, trueValues) {
model.compile({
optimizer: tf.train.adam(learningRate),
loss: tf.losses.meanSquaredError,
metrics: ["mse"]
});
const batchSize = 2;
return await model.fit(inputs, trueValues, {
batchSize,
epochs,
});
}
function testModel(model, testValue) {
return tf.tidy(() => model.predict(tf.tensor2d([testValue], [1, 1])));
}
const run = async testValue => {
const model = createModel();
const tensorData = convertToTensor();
await trainModel(model, tensorData.inputs, tensorData.outputs);
const prediction = testModel(model, testValue);
console.log(`Predction for 5: ${prediction.toString()}`);
};
run(5);
});