# Unexpected output shape from NN

**URL:** <https://discuss.ai.google.dev/t/unexpected-output-shape-from-nn/30430>\
**Category:** TF.js\
**Tags:** help\_request\
**Created:** [February 8, 2023, 5:45pm UTC](https://discuss.ai.google.dev/t/unexpected-output-shape-from-nn/30430 "2023-02-08T17:45:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mah\_Neh](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/mah_neh/32/11663_2.png) [@Mah\_Neh](https://discuss.ai.google.dev/u/Mah_Neh)\
**Post date:** [February 8, 2023, 5:45pm UTC](https://discuss.ai.google.dev/t/unexpected-output-shape-from-nn/30430/1 "2023-02-08T17:45:17Z")

</div>

I am using a few layers on top of VGG16, to tune the net for recognizing only if there is or not a specific object on an image.

```auto
const outputLayer = tf.layers
  .dense({ units: 1, activation: "sigmoid", dtype: "float32" })
  .apply(dense2);

const detection = tf.model({
  inputs: baseModel.inputs,
  outputs: outputLayer as tf.SymbolicTensor[],
});

detection.compile({
  optimizer: tf.train.adam(0.0001), //could be a parameter as well.
  loss: tf.losses.sigmoidCrossEntropy,
  metrics: ["mse"],
});

```

The output is `[0,1]` when I do a prediction. Why isn’t the output a single number, when the last layer has a single unit ?

Just in case this helps to solve, the labels have this format:

```auto
[
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1,
  ... more items
]

```

---

<div class="post-metadata">

**Author:** ![Mah\_Neh](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/mah_neh/32/11663_2.png) [@Mah\_Neh](https://discuss.ai.google.dev/u/Mah_Neh)\
**Post date:** [February 8, 2023, 6:00pm UTC](https://discuss.ai.google.dev/t/unexpected-output-shape-from-nn/30430/2 "2023-02-08T18:00:51Z")

</div>

My bad, at some point changed the directories and it was reading from an old model, by directing it to the right model it outputs the expected single value 0-1.

However, the network needs a modification

The output layer should be changed to use 2 units

```auto
const outputLayer = tf.layers
  .dense({ units: 2, activation: "softmax" }) // or sigmoid
  .apply(dense2);

```

And the compilation as well:

```auto
detection.compile({
  optimizer: tf.train.adam(0.0001), //could be a parameter as well.
  loss: "categoricalCrossEntropy", // ------ this line
  metrics: ["mse"],
});

```

And the labels should be passed as a oneHot vector, in my case a 0,1 array when there is an airplane, or 1,0 array when there is a stop sign, and could also be passed the remaining 2 combinations if we have training set images.

Now the output makes sense to some extent.
