gpt4 book ai didi

machine-learning - Tensorflow.js/Keras LSTM 具有多个序列?

转载 作者:行者123 更新时间:2023-11-30 09:47:53 25 4
gpt4 key购买 nike

我正在尝试使用基于 Keras 构建的 Layers API 通过 Tensorflow.js 训练 lstm 模型。我很难得到正确的预测。我正在尝试为模型提供一系列 NBA 球员每个赛季的职业生涯得分(例如:[20, 30, 40, 55, 60, 55, 33, 23])。我想用下个赛季的生产分数作为 y 为其提供一系列球员。

var data = tf.tensor([
[[100], [86], [105], [122], [118], [96], [107], [118], [100], [85]],
[[30], [53], [74], [85], [96], [87], [98], [99], [110], [101]],
[[30], [53], [74], [85], [96], [87], [98], [99], [110], [101]],
[[30], [53], [74], [85], [96], [87], [98], [99], [110], [101]],
[[30], [53], [74], [85], [96], [87], [98], [99], [110], [101]],
[[30], [53], [74], [85], [96], [87], [98], [99], [110], [101]],
[[30], [53], [74], [85], [96], [87], [98], [99], [110], [101]],
[[30], [53], [74], [85], [96], [87], [98], [99], [110], [101]]
]);

var y = tf.tensor([[100], [90], [90], [90], [90], [90], [90], [90]]);

const model = tf.sequential();
model.add(
tf.layers.lstm({
units: 1,
inputShape: [10, 1]
})
);

model.compile({ loss: "meanSquaredError", optimizer: "adam" });

model.fit(data, y, { epochs: 1000 }).then(() => {
// Use the model to do inference on a data point the model hasnt
// seen before:
model
.predict(
tf.tensor([
[[30], [53], [74], [85], [96], [87], [98], [99], [110], [101]]
])
)
.print();
});

它正在预测这样的事情:[[0],]

当我期待这样的事情时:[[90]]

最佳答案

迈克.您应该标准化(转换)每个输入数据。神经网络可以理解标准化为其激活函数范围的数字。例如我将使用“sigmoid”:

function normalize(num, min, max) //converts values to the range between values 0 and 1;
{
return (num - min) * (1/(max - min));
}
function denormalize(num, min, max) //reconverts values from range between values 0 and 1 to range between Min and Max;
{
return (num / (1/(max - min))) + min;
}

const MIN = 0;
const MAX = 150; //You can use bigger if you need.

var data = [
[100, 86, 105, 122, 118, 96, 107, 118, 100, 85],
[30, 53, 74, 85, 96, 87, 98, 99, 110, 101],
[30, 53, 74, 85, 96, 87, 98, 99, 110, 101],
[30, 53, 74, 85, 96, 87, 98, 99, 110, 101],
[30, 53, 74, 85, 96, 87, 98, 99, 110, 101],
[30, 53, 74, 85, 96, 87, 98, 99, 110, 101],
[30, 53, 74, 85, 96, 87, 98, 99, 110, 101],
[30, 53, 74, 85, 96, 87, 98, 99, 110, 101]
];
var y = [100, 90, 90, 90, 90, 90, 90, 90];

var trainingData = [];
for(let x = 0; x < data.length; x++)
{
let line = data[x];
let normalized = [];
for(let y = 0 ; y < line.length; y++) //normalize input
normalized[y] = normalize(line[y], MIN, MAX);

trainingData.push({
raw : line,
input : tf.tensor3d(normalized, [1, 10, 1]),
output : tf.tensor2d([normalize(y[x], MIN, MAX)], [1, 1]) //normalize output
});
}

const model = tf.sequential();
model.add(tf.layers.lstm({
units: 50,
inputShape: [10, 1],
requrentSequences: true
}));
model.add(tf.layers.dense({
units: 1,
//activation: 'softplus' // you can taste this too ;)
activation: 'sigmoid'
}));

//better if optimizer is optional like this:
const LEARNING_RATE = 0.0001;
const optimizer = tf.train.adam(LEARNING_RATE);
model.compile({
optimizer: optimizer,
loss: 'meanSquaredError'
});

async function train(epochs, loss, cb)
{
for(let e = 0; e < epochs; e++)
{
var totallLoss = 0;
for(let i = 0; i < trainingData.length; i++)
{
var info = await model.fit(trainingData[i].input, trainingData[i].output, { epochs: 1 });
totallLoss += info.history.loss[0];
}
var avgLoss = totallLoss/trainingData.length;
console.log(`[${e}/${epochs}] Average Loss: ${avgLoss};`);
if(loss >= avgLoss)
break;
}

if(typeof cb === 'function')
cb();
}

async function activate(input)
{
var output = await model.predict(input).dataSync();
return output;
}

console.log('Start training in 5sec...');
setTimeout(function()
{
console.log('Training started.');
console.time('Training');
train(500, 0.00000001, async function()
{
console.timeEnd('Training');
console.log('Training complete.');
console.log('Tests of your training data:');
for(let i = 0; i < trainingData.length; i++)
{
let input = trainingData[i].input;
let act = await activate(input);
for(let i = 0; i < act.length; i++)
{
act[i] = denormalize(act[i], MIN, MAX);
}
console.log(`${JSON.stringify(trainingData[i].raw)}: ${act[0].toFixed(2)}`); //denormalized(decoded/reconverted) output
}
});
}, 5000);
<!DOCTYPE html>
<html>
<head>
<title>Tensorflow.js/Keras LTSM with multiple sequences?</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2"></script>
</head>

<body>
<h2>Open console to see training process.</h2>
</body>
</html>

有关不同激活函数的更多信息:https://conx.readthedocs.io/en/3.5.4/ActivationFunctions.html

关于machine-learning - Tensorflow.js/Keras LSTM 具有多个序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49932980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com