Error when checking target: expected dense_Dense2 to have shape x, but got array with shape y

Viewed 486

It is my first steps in the tensorflow.

Idea

There is some pattern of numbers (the array of numbers: Pattern = number[]). And the category that corresponds to this pattern (the number from 0 to 2: Category = 0 | 1 | 2). I have follow the structure data: xs = Pattern[], ys = Category[].

For example:

xs = [[1, 2, 3, 4], [5, 6, 7, 8], ..., [9, 10, 11, 12]];
ys = [1, 0, ..., 2];

I want the neural network to find a match between xs[0] and xy[0], and so on. I want to pass the neural network data like [1, 2, 3, 4] and get a result close to 1.

model.predict(tf.tensor([1, 2, 3, 4])) // ≈1

My code

import * as tf from '@tensorflow/tfjs';
require('@tensorflow/tfjs-node');

const xs = tf.tensor2d([
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
]);
const ys = tf.tensor1d([0, 1, 2]);

const model = tf.sequential();
model.add(tf.layers.dense({ units: 4, inputShape: xs.shape, activation: 'relu' }));
                                   ^ - Pattern length, it is constant
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });

model.fit(xs, ys, { epochs: 500 });

I get follow error:

Error when checking input: expected dense_Dense1_input to have 3 dimension(s). but got array with shape 3,4

I don't understand how to explain my data structure for the neural network.

2 Answers

The model inputShape is [3,4] . To fit or predict with this model, it needs a data with the form [b, 3, 4] where b is the batch shape. The batch shape is missing when trying to fit your model with xs.

The model inputShape should rather be [4] so that xs can be used for prediction. Instead of using xs.shape, it could be xs.shape.slice(-1).

const xs = tf.tensor2d([
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
]);
const ys = tf.tensor1d([0, 1, 2]);

const model = tf.sequential();
model.add(tf.layers.dense({ units: 4, inputShape: xs.shape.slice(1), activation: 'relu' }));
                                  
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });

model.fit(xs, ys);
model.predict(xs).print()

Besides, if the goal of the model is to predict a category as indicated by the use of softmax and categoricalCrossentropy, then, the label should be one-hot encoded.

Similar answers:

I found the right solution for my task. Just need to use the dataset

https://js.tensorflow.org/api/latest/#tf.Sequential.fitDataset

import * as tf from '@tensorflow/tfjs';
require('@tensorflow/tfjs-node');

const xArray = [
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
];

const yArray = [0, 1, 2];
const { length } = yArray;

const xs = tf.data.array(xArray);
const ys = tf.data.array(yArray);

const xyDataset = tf.data.zip({ xs: xDataset, ys: yDataset }).batch(length).shuffle(length);

const model = tf.sequential();
model.add(tf.layers.dense({ units: length, inputShape: [length], activation: 'relu' }));
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });

model.fitDataset(xyDataset, { epochs: 500 });
Related