LSTM:找出库? [英] LSTM: Figuring out the library?
本文介绍了LSTM:找出库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用库 https://github.com/cazala/synaptic >
我正在尝试预测以下系列中的下一个值(值X):
0 0 0 1 0 0 0 1 0 0 0 X
应该是1
.
这是代码:
const options = {
peepholes: Layer.connectionType.ALL_TO_ALL,
hiddenToHidden: false,
outputToHidden: false,
outputToGates: false,
inputToOutput: true,
};
// 1 input, 3 hidden layers (4 nodes per layer), 1 output
const lstm = new Architect.LSTM(1, 4, 4, 4, 1, options);
const trainingArray = [
{
input: [0],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [1],
},
{
input: [1],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [1],
},
{
input: [1],
output: [0],
},
];
const trainingOptions = {
rate: 0.1,
iterations: 100000,
error: 0.05,
cost: null,
crossValidate: null,
};
let results = lstm.trainer.train(trainingArray, trainingOptions);
console.log(results);
array = [
0,
0,
0,
1,
0,
0,
0,
1,
0,
0,
0,
];
results = lstm.activate(array);
console.log(results);
控制台中的输出:
{ error: 0.049765018466871494, iterations: 673, time: 392 }
[ 0.05010961302724895 ]
我期望激活的结果是一个比1接近0的值(更接近).我不知道这是否是我缺乏LSTM知识的图书馆.有人可以指出我正确的方向吗?
解决方案
我仔细阅读了源代码并弄清楚了.
const synaptic = require('synaptic');
const Architect = synaptic.Architect;
const Layer = synaptic.Layer;
const lstmOptions = {
peepholes: Layer.connectionType.ALL_TO_ALL,
hiddenToHidden: false,
outputToHidden: false,
outputToGates: false,
inputToOutput: true,
};
const lstm = new Architect.LSTM(1, 4, 4, 4, 1, lstmOptions);
const trainSet = [
{ input: [0], output: [0.1] },
{ input: [1], output: [0.2] },
{ input: [0], output: [0.3] },
{ input: [1], output: [0.4] },
{ input: [0], output: [0.5] },
];
const trainOptions = {
rate: 0.2,
iterations: 10000,
error: 0.005,
cost: null,
crossValidate: null,
};
const trainResults = lstm.trainer.train(trainSet, trainOptions);
console.log(trainResults);
const testResults = [];
testResults[0] = lstm.activate([0]);
testResults[1] = lstm.activate([1]);
testResults[2] = lstm.activate([0]);
testResults[3] = lstm.activate([1]);
testResults[4] = lstm.activate([0]);
console.log(testResults);
结果:
{ error: 0.004982436660844655, iterations: 2010, time: 384 }
[ [ 0.18288280009908592 ],
[ 0.2948083898027347 ],
[ 0.35061782593064206 ],
[ 0.3900799575806566 ],
[ 0.49454852760556606 ] ]
是准确的.
I'm using the library https://github.com/cazala/synaptic
I am trying to predict the next value (value X) in the following series:
0 0 0 1 0 0 0 1 0 0 0 X
Which should be a 1
.
Here is the code:
const options = {
peepholes: Layer.connectionType.ALL_TO_ALL,
hiddenToHidden: false,
outputToHidden: false,
outputToGates: false,
inputToOutput: true,
};
// 1 input, 3 hidden layers (4 nodes per layer), 1 output
const lstm = new Architect.LSTM(1, 4, 4, 4, 1, options);
const trainingArray = [
{
input: [0],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [1],
},
{
input: [1],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [1],
},
{
input: [1],
output: [0],
},
];
const trainingOptions = {
rate: 0.1,
iterations: 100000,
error: 0.05,
cost: null,
crossValidate: null,
};
let results = lstm.trainer.train(trainingArray, trainingOptions);
console.log(results);
array = [
0,
0,
0,
1,
0,
0,
0,
1,
0,
0,
0,
];
results = lstm.activate(array);
console.log(results);
The output in the console:
{ error: 0.049765018466871494, iterations: 673, time: 392 }
[ 0.05010961302724895 ]
I was expecting the result of the activation to be a value closer to 1 than 0 (much closer). I don't know if this is the library of my lack of knowledge with LSTM. Can someone point me in the correct direction?
解决方案
I read through the source code and figured it out.
const synaptic = require('synaptic');
const Architect = synaptic.Architect;
const Layer = synaptic.Layer;
const lstmOptions = {
peepholes: Layer.connectionType.ALL_TO_ALL,
hiddenToHidden: false,
outputToHidden: false,
outputToGates: false,
inputToOutput: true,
};
const lstm = new Architect.LSTM(1, 4, 4, 4, 1, lstmOptions);
const trainSet = [
{ input: [0], output: [0.1] },
{ input: [1], output: [0.2] },
{ input: [0], output: [0.3] },
{ input: [1], output: [0.4] },
{ input: [0], output: [0.5] },
];
const trainOptions = {
rate: 0.2,
iterations: 10000,
error: 0.005,
cost: null,
crossValidate: null,
};
const trainResults = lstm.trainer.train(trainSet, trainOptions);
console.log(trainResults);
const testResults = [];
testResults[0] = lstm.activate([0]);
testResults[1] = lstm.activate([1]);
testResults[2] = lstm.activate([0]);
testResults[3] = lstm.activate([1]);
testResults[4] = lstm.activate([0]);
console.log(testResults);
Results in:
{ error: 0.004982436660844655, iterations: 2010, time: 384 }
[ [ 0.18288280009908592 ],
[ 0.2948083898027347 ],
[ 0.35061782593064206 ],
[ 0.3900799575806566 ],
[ 0.49454852760556606 ] ]
Which is accurate.
这篇关于LSTM:找出库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文