Search code examples
tensorflowneural-networkartificial-intelligencetensorflow.js

TensorflowJS: different shape for output


I'm completely new to Tensorflow.

My goal is quite simple: I have a 3d tensor as an input/training value, and I'd like to "map" that to a 1d output tensor.

When running my model, I get an error, that the 1d output tensor can't be assigned to the defined [5, 5] shape:

const model = tf.sequential({
  layers: [
    tf.layers.dense({
      inputShape: [5, 5],
      units: 32,
      activation: "relu"
    }),
    tf.layers.dense({ units: 1, activation: "softmax" }),
  ]
});

Is it possible to have different shapes for output/input? I want the 3d tensor to be like "groups of numbers" resolve to a single (1d tensor) number.


Solution

  • To map a high dimensional tensor (superior to 1) to a 1d tensor, a flatten layers needs to be used in between

    const model = tf.sequential({
      layers: [
        tf.layers.dense({
          inputShape: [5, 5],
          units: 32,
          activation: "relu"
        }),
        tf.layers.flatten(),
        tf.layers.dense({ units: 1, activation: "softmax" }),
      ]
    });