Answers.Body: <p>Based on the code and model architecture you have provided. It seems your model takes a single input and gives four outputs the shape of the input layer is (batch_size,2) while the output shape of each node seems to be (batch_size,1).</p>
  <p>your output should be a list of four outputs means a list that contains the output tensors of all nodes. but you are getting the tensor of shape (4,1,4).</p>
  <p>I think it may be an issue due to the way you are giving the input to the model can you please check the input shape since I have used the same input as you and I got a valid shape means a list of output tensors each contains tensor of shape (batch_size,1). and also, I think you should verify that the denormalize_out function is not stacking your output list.</p>
  <p>also to verify the model architecture I have loaded your model on Netron it seems to have the correct architecture you can see it below</p>
  <p><a href="https://i.sstatic.net/vtaZb.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vtaZb.png" alt="enter image description here" /></a></p>
  <p>Here below is my code and its output</p>
  <pre><code>inputs = Input(shape = 2, name = &quot;Input&quot;)
  
  #hidden layers
  x = Dense(6, activation = &quot;relu&quot;, name = &quot;dense_layer_1&quot;)(inputs)
  x = Dense(4, activation = &quot;relu&quot;, name = &quot;dense_layer_2&quot;)(x)
  punishment = Dense(3, activation = &quot;relu&quot;, name = &quot;punishment&quot;)(x)
  
  #output layers
  y_1 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_1&quot;)(punishment)
  y_2 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_2&quot;)(punishment)
  y_3 = Dense(1, activation = &quot;sigmoid&quot;, name= &quot;y_3&quot;)(punishment)
  y_4 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_4&quot;)(punishment)
  
  #functional model declaration
  model = Model(inputs = inputs, outputs = [y_1, y_2, y_3, y_4])
  
  #Dummy Input
  dummy_input = np.random.rand(1,2)
  print(&quot;Input shape&quot;,dummy_input.shape)
  
  #If we store the output in diffrent variables it store each as output tensors
  out1,out2,out3,out4 = model.predict(dummy_input)  
  
  #If we store all outputs in a single varialbe it store it as list of output tensors
  preds = model.predict(dummy_input)
  
  print(&quot;output 1 shape&quot;,out1.shape)
  print(&quot;output 2 shape&quot;,out2.shape)
  print(&quot;output 3 shape&quot;,out3.shape)
  print(&quot;output 4 shape&quot;,out4.shape)
  
  #making predictions 
  dummy_input_2 = np.array([[1.724373  , -0.39440534]])
  print(&quot;dummy_input_2 shape&quot;,dummy_input_2.shape)
  predictions = model(dummy_input_2)
  print(predictions)
  </code></pre>
  <p>The output of the above script is</p>
  <pre><code>Input shape (1, 2)
  output 1 shape (1, 1)
  output 2 shape (1, 1)
  output 3 shape (1, 1)
  output 4 shape (1, 1)
  dummy_input_2 shape (1, 2)
  [&lt;tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.3822088]], dtype=float32)&gt;, &lt;tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.60975766]], dtype=float32)&gt;, &lt;tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.5706901]], dtype=float32)&gt;, &lt;tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.45608807]], dtype=float32)&gt;]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-13T10:30:18.550Z
Answers.LastActivityDate: 2023-12-13T10:53:31.107Z
Answers.LastEditDate: 2023-12-13T10:53:31.107Z
Answers.UserDisplayName: Keval Pandya
Answers.Body: <p>Based on the code you provided, it appears that the input shape is correctly set to <code>(None, 2)</code>, indicating that the model expects inputs of shape <code>(batch_size, 2)</code>.</p>
  <p>Regarding the output shape, the model correctly has four output layers (<code>y_1</code>, <code>y_2</code>, <code>y_3</code>, <code>y_4</code>). Each of these layers has an output shape of <code>(None, 1)</code>, indicating that the model will produce an output of shape <code>(batch_size, 1)</code> for each output layer.</p>
  <p>When you call your model with an input of shape <code>(1, 2)</code>, you indeed get an output of shape <code>(1, 2)</code> as expected.</p>
  <p>However, when you use <code>denormalize_out</code> on the model output, the resulting shape is <code>(4, 1, 4)</code>. This indicates that the output shape is <code>(num_outputs, batch_size, num_classes)</code>.</p>
  <p>This unexpected output shape may be caused by discrepancies in your normalization and denormalization functions or a mismatch between the expected output shape and the denormalize function's handling of the output.</p>
  <p>I would recommend double-checking your normalization and denormalization functions to ensure they are correctly handling the output shape. Additionally, verify that the denormalization function expects an output shape of <code>(batch_size, 4)</code> instead of <code>(4, 1, 4)</code>. Adjusting the function accordingly may help resolve the issue.</p>
  <p>If the issue persists or you need further assistance, please provide more details on the normalization and denormalization functions, as well as any relevant information about the dataset and the desired output dimensions.</p>
  <p>It may be helpful to refer to the Keras documentation for further guidance.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-11T08:26:14.757Z
Answers.LastActivityDate: 2023-12-11T08:27:36.040Z
Answers.LastEditDate: 2023-12-11T08:27:36.040Z
Answers.UserDisplayName: han
Body: <p>So, this is my model:</p>
  <pre><code>#input layers
  inputs = Input(shape = 2, name = &quot;Input&quot;)
  
  #hidden layers
  x = Dense(6, activation = &quot;relu&quot;, name = &quot;dense_layer_1&quot;)(inputs)
  x = Dense(4, activation = &quot;relu&quot;, name = &quot;dense_layer_2&quot;)(x)
  punishment = Dense(3, activation = &quot;relu&quot;, name = &quot;punishment&quot;)(x)
  
  #output layers
  y_1 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_1&quot;)(punishment)
  y_2 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_2&quot;)(punishment)
  y_3 = Dense(1, activation = &quot;sigmoid&quot;, name= &quot;y_3&quot;)(punishment)
  y_4 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_4&quot;)(x)
  
  #functional model declaration
  model = Model(inputs = inputs, outputs = [y_1, y_2, y_3, y_4])
  </code></pre>
  <p>and, when I call input_shape and output_shape on this:</p>
  <pre><code>print(model.input_shape)
  print(model.output_shape)
  </code></pre>
  <pre><code>(None, 2)
  [(None, 1), (None, 1), (None, 1), (None, 1)]
  </code></pre>
  <p>But, when I call my model:</p>
  <pre><code>print(np.array([normalize_in([290360000, 0])], dtype = &quot;float32&quot;)[0].shape)
  print(denormalize_out(model(np.array([normalize_in([290360000, 0])], dtype = &quot;float32&quot;)[0])))
  </code></pre>
  <p>it gives me:</p>
  <pre><code>[[ 1.724373   -0.39440534]]
  (1, 2)
  tf.Tensor(
  [[[8.4842375e+05 1.7916626e+01 3.6546925e+01 1.6306080e+01]]
  
   [[7.9671431e+05 1.7595743e+01 3.5045170e+01 1.5826001e+01]]
  
   [[7.6195538e+05 1.7380047e+01 3.4035694e+01 1.5503292e+01]]
  
   [[9.3114094e+05 1.8429928e+01 3.8949211e+01 1.7074041e+01]]], shape=(4, 1, 4), dtype=float32)
  </code></pre>
  <p>I've been trying to go around the internet to find it, but there was just way too few examples about multi-output keras functional models, and I couldn't figure out what is wrong with my model.</p>
  
Tags: <machine-learning><keras>
Title: Keras functional model returning unexpected output dimensions
UserDisplayName: Aliquis
ThreadText: # Keras functional model returning unexpected output dimensions
  
  (Asked by: username_0 on 2023-12-08)
  
  So, this is my model:
  #input layers
  inputs = Input(shape = 2, name = "Input")
  
  #hidden layers
  x = Dense(6, activation = "relu", name = "dense_layer_1")(inputs)
  x = Dense(4, activation = "relu", name = "dense_layer_2")(x)
  punishment = Dense(3, activation = "relu", name = "punishment")(x)
  
  #output layers
  y_1 = Dense(1, activation = "sigmoid", name = "y_1")(punishment)
  y_2 = Dense(1, activation = "sigmoid", name = "y_2")(punishment)
  y_3 = Dense(1, activation = "sigmoid", name= "y_3")(punishment)
  y_4 = Dense(1, activation = "sigmoid", name = "y_4")(x)
  
  #functional model declaration
  model = Model(inputs = inputs, outputs = [y_1, y_2, y_3, y_4])
  
  and, when I call input_shape and output_shape on this:
  print(model.input_shape)
  print(model.output_shape)
  
  (None, 2)
  [(None, 1), (None, 1), (None, 1), (None, 1)]
  
  But, when I call my model:
  print(np.array([normalize_in([290360000, 0])], dtype = "float32")[0].shape)
  print(denormalize_out(model(np.array([normalize_in([290360000, 0])], dtype = "float32")[0])))
  
  it gives me:
  [[ 1.724373   -0.39440534]]
  (1, 2)
  tf.Tensor(
  [[[8.4842375e+05 1.7916626e+01 3.6546925e+01 1.6306080e+01]]
  
   [[7.9671431e+05 1.7595743e+01 3.5045170e+01 1.5826001e+01]]
  
   [[7.6195538e+05 1.7380047e+01 3.4035694e+01 1.5503292e+01]]
  
   [[9.3114094e+05 1.8429928e+01 3.8949211e+01 1.7074041e+01]]], shape=(4, 1, 4), dtype=float32)
  
  I've been trying to go around the internet to find it, but there was just way too few examples about multi-output keras functional models, and I couldn't figure out what is wrong with my model.
  
  **Tags:** machine-learning, keras
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-12-13. Score: 0 | Accepted answer
  
  Based on the code and model architecture you have provided. It seems your model takes a single input and gives four outputs the shape of the input layer is (batch_size,2) while the output shape of each node seems to be (batch_size,1).
  your output should be a list of four outputs means a list that contains the output tensors of all nodes. but you are getting the tensor of shape (4,1,4).
  I think it may be an issue due to the way you are giving the input to the model can you please check the input shape since I have used the same input as you and I got a valid shape means a list of output tensors each contains tensor of shape (batch_size,1). and also, I think you should verify that the denormalize_out function is not stacking your output list.
  also to verify the model architecture I have loaded your model on Netron it seems to have the correct architecture you can see it below
  
  Here below is my code and its output
  inputs = Input(shape = 2, name = "Input")
  
  #hidden layers
  x = Dense(6, activation = "relu", name = "dense_layer_1")(inputs)
  x = Dense(4, activation = "relu", name = "dense_layer_2")(x)
  punishment = Dense(3, activation = "relu", name = "punishment")(x)
  
  #output layers
  y_1 = Dense(1, activation = "sigmoid", name = "y_1")(punishment)
  y_2 = Dense(1, activation = "sigmoid", name = "y_2")(punishment)
  y_3 = Dense(1, activation = "sigmoid", name= "y_3")(punishment)
  y_4 = Dense(1, activation = "sigmoid", name = "y_4")(punishment)
  
  #functional model declaration
  model = Model(inputs = inputs, outputs = [y_1, y_2, y_3, y_4])
  
  #Dummy Input
  dummy_input = np.random.rand(1,2)
  print("Input shape",dummy_input.shape)
  
  #If we store the output in diffrent variables it store each as output tensors
  out1,out2,out3,out4 = model.predict(dummy_input)  
  
  #If we store all outputs in a single varialbe it store it as list of output tensors
  preds = model.predict(dummy_input)
  
  print("output 1 shape",out1.shape)
  print("output 2 shape",out2.shape)
  print("output 3 shape",out3.shape)
  print("output 4 shape",out4.shape)
  
  #making predictions 
  dummy_input_2 = np.array([[1.724373  , -0.39440534]])
  print("dummy_input_2 shape",dummy_input_2.shape)
  predictions = model(dummy_input_2)
  print(predictions)
  
  The output of the above script is
  Input shape (1, 2)
  output 1 shape (1, 1)
  output 2 shape (1, 1)
  output 3 shape (1, 1)
  output 4 shape (1, 1)
  dummy_input_2 shape (1, 2)
  [, , , ]
  
  
  ---
  
  ### Answer by username_2 on 2023-12-11. Score: -1
  
  Based on the code you provided, it appears that the input shape is correctly set to `(None, 2)`, indicating that the model expects inputs of shape `(batch_size, 2)`.
  
  Regarding the output shape, the model correctly has four output layers (`y_1`, `y_2`, `y_3`, `y_4`). Each of these layers has an output shape of `(None, 1)`, indicating that the model will produce an output of shape `(batch_size, 1)` for each output layer.
  
  When you call your model with an input of shape `(1, 2)`, you indeed get an output of shape `(1, 2)` as expected.
  
  However, when you use `denormalize_out` on the model output, the resulting shape is `(4, 1, 4)`. This indicates that the output shape is `(num_outputs, batch_size, num_classes)`.
  
  This unexpected output shape may be caused by discrepancies in your normalization and denormalization functions or a mismatch between the expected output shape and the denormalize function's handling of the output.
  
  I would recommend double-checking your normalization and denormalization functions to ensure they are correctly handling the output shape. Additionally, verify that the denormalization function expects an output shape of `(batch_size, 4)` instead of `(4, 1, 4)`. Adjusting the function accordingly may help resolve the issue.
  
  If the issue persists or you need further assistance, please provide more details on the normalization and denormalization functions, as well as any relevant information about the dataset and the desired output dimensions.
  
  It may be helpful to refer to the Keras documentation for further guidance.
  
  
  ---
  
  