TensorFlow JS 3D-данные дают ошибки - PullRequest
0 голосов
/ 13 июля 2020

Я пытаюсь начать работу с многомерными данными в TensorFlow JS, но у меня возникают проблемы с определением модели, которая работает без ошибок. Вот мой текущий код:

const tf = require('@tensorflow/tfjs-node');

// Each prediction should be made on one top level element, which consists of
// a fixed number of internal arrays (3) each with a with a fixed number of elements (4).
const inputs = [
  [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
  ],
  [
    [1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1],
  ],
  [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
  ],
  [
    [1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1],
  ],
  [
    [2, 2, 2, 2],
    [2, 2, 2, 2],
    [2, 2, 2, 2],
  ],
];

// The labels are arrays of 3 elements. A 1 in a given index indicates that 
// index represents the most common value found in the sample data. 
// All values are between 0 and 2.
const labels = [
  [1, 0, 0],
  [0, 1, 0],
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
];

const model = tf.sequential();

model.add(
  tf.layers.dense({
    units: 15,
    activation: 'relu',
    inputShape: [3, 4],
  }),
);

model.add(
  tf.layers.dense({
    units: 9,
    activation: 'relu',
  }),
);

model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));

model.compile({
  optimizer: tf.train.adam(),
  loss: 'categoricalCrossentropy',
  metrics: ['accuracy'],
});

async function run() {
  model
    .fit(tf.tensor(inputs), tf.tensor(labels), {
      epochs: 5,
      batchSize: 2,
      shuffle: true,
    })
    .then((info) => {
      console.log('Final Accuracy:', info.history.acc);
    });
}

module.exports = { run };

В настоящее время я получаю следующую ошибку:

Error: Error when checking target: expected dense_Dense3 to have 3 dimension(s). but got array with shape 5,3

Я думаю, это означает, что предоставленные мной метки не являются допустимой структурой. , но я не уверен, как правильно сопоставить метки с многомерными данными. Я пробовал несколько подходов к этой простой проблеме, но еще не нашел способ запустить этот код без ошибок. Буду признателен за любую помощь, которую вы можете предложить, чтобы запустить это.

...