Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

plunkerでtensorflow.js その16

More than 1 year has passed since last update.

概要

plunkerでtensorflow.jsやってみた。
lstm使ってみた。
abならc、bcならd、xyならzと学習させて、
abでcが出たら、bcにして、推定繰り返して、
abcを答えさせる実験。

サンプルコード

tf.setBackend('cpu');
let index2alphabet = [];
let alphabet2vector = {};
let trainXArray = [], 
  trainYArray = [];
for (let i = 0; i < 26; i++) 
{
  index2alphabet[i] = String.fromCharCode('a'.charCodeAt(0) + i);
}
for (let i = 0; i < 26; i++) 
{
  alphabet2vector[index2alphabet[i]] = Array(26).fill(0);
  alphabet2vector[index2alphabet[i]][i] = 1;
}
trainXArray.push([alphabet2vector['y'], alphabet2vector['z']]);
trainYArray.push(alphabet2vector['a']);
trainXArray.push([alphabet2vector['z'], alphabet2vector['a']]);
trainYArray.push(alphabet2vector['b']);
for (let i = 0; i < 26 - 2; i++)
{
  trainXArray.push([alphabet2vector[index2alphabet[i]], alphabet2vector[index2alphabet[i + 1]]]);
  trainYArray.push(alphabet2vector[index2alphabet[i + 2]]);
}
const trainX = tf.tensor3d(trainXArray);
const trainY = tf.tensor2d(trainYArray);
const model = tf.sequential();
model.add(tf.layers.lstm({ 
  units: 64,
  returnSequences: false,
  inputShape: [2, 26]
}));
model.add(tf.layers.dense({
  units: 64, 
  activation: "relu"
}));
model.add(tf.layers.dense({
  units: 26, 
  activation: "softmax"
}));
model.compile({
  optimizer: 'adam',
  loss: 'categoricalCrossentropy'
});
model.fit(trainX, trainY, {
  epochs: 160,
  verbose: 0,
  validationData: [trainX, trainY],
}).then(() => {
  let i = 0;
  let p0 = 'y';
  let p1 = 'z';
  for (i = 0; i < 26; i++)
  {
    let testXArray = trainXArray;
    testXArray[0] = [alphabet2vector[p0], alphabet2vector[p1]];
    const testX = tf.tensor3d(testXArray);
    const predicts = model.predict(testX);
    const predictsArray = predicts.argMax().arraySync();
    const predictAlphabet = String.fromCharCode('a'.charCodeAt(0) + predictsArray[0]);
    document.write(predictAlphabet);
    p0 = p1;
    p1 = predictAlphabet;
    alert(p0 + ' ' + p1)
  }
});

成果物

https://embed.plnkr.co/8NZDUG8JJOjX1eo7iGlS/

以上。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away