0
$\begingroup$

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.

$\endgroup$
0

2 Answers 2

0
$\begingroup$

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

enter image description here

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)
[<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.3822088]], dtype=float32)>, <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.60975766]], dtype=float32)>, <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.5706901]], dtype=float32)>, <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.45608807]], dtype=float32)>]
$\endgroup$
-1
$\begingroup$

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged .