How to plot and train WideAndDeepModel model

In Wk04 Lab01: we have plotted model build using functional API. The issue arises when the model build using subclass WideAndDeepModel is plotted or trained: the reference code is as follows:

class WideAndDeepModel(Model):
def init(self, units=30, activation= ‘relu’, **kwargs):
self.hidden_1 = Dense(units=30, activation= activation)
self.hidden_2 = Dense(units=30, activation= activation)
self.main_output = Dense(1)
self.aux_output = Dense(1)

def call(self, inputs):
input_a, input_b = inputs
hidden_1 = self.hidden_1(input_b)
hidden_2 = self.hidden_2(hidden_1)
concat = concatenate(input_a, hidden_2)
main_output = self.main_output(concat)
aux_output = self.aux_output(hidden_2)

return main_output, aux_output

x1 = np.array([-1,0,1,2,3])
x2 = np.array([3,2,1,0,-1])
xs =
y = np.array([2,2,0,-2,-4])
model = WideAndDeepModel()
model.compile(optimizer= ‘adam’, loss= ‘mse’)[xs[0],xs[1]], y, epochs= 500, verbose = 0)


ValueError Traceback (most recent call last)

in ()
28 model = WideAndDeepModel()
29 model.compile(optimizer= ‘adam’, loss= ‘mse’)
—> 30[xs[0],xs[1]], y, epochs= 500, verbose = 0)
31 model.summary()
32 #plot_model(model, show_layer_names= True, show_shapes= True)

1 frames

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ in autograph_handler(*args, **kwargs)
1127 except Exception as e: # pylint:disable=broad-except
1128 if hasattr(e, “ag_error_metadata”):
→ 1129 raise e.ag_error_metadata.to_exception(e)
1130 else:
1131 raise

ValueError: in user code:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 878, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 867, in step_function  **
    outputs =, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 860, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 808, in train_step
    y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None

ValueError: Exception encountered when calling layer "wide_and_deep_model_19" (type WideAndDeepModel).

in user code:

    File "<ipython-input-33-9a5a7dab557a>", line 14, in call  *
        hidden_1 = self.hidden_1(input_b)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/", line 67, in error_handler  **

my guess is: the way to introduce multiple inputs is different for subclasses? kindly guide.

One mistake i picked up at concatenate the parameters should be inside a list.

Hello, don’t know if you still have these questions, but I still prefer to point the mistakes in your code out.
First, the number of parameters returned in the call function is two, so it is not plausible that you only have one list of y. You should have two lists of y and gather them into one list like y=[y1, y2] and both y1, y2 are lists.
Second, even if I did the first step but there were still bug reports on data type. It might be tricky sometimes when you put some numpy arrays into tensorflow functions or other DL functions. So a good way to avoid these data type bugs is that you can set all your lists with ‘float32’ type elements, if you insist using numpy array. Or you may use tf.Variable to change those lists into Tensors.

Here is what I did with your code:

import numpy as np
x1 = np.array([-1.,0.,1.,2.,3.])
x2 = np.array([3.,2.,1.,0.,-1.])
xs = []
y1 = np.array([2.,2.,0.,-2.,-4.])
y2 = np.array([1.,2.,3.,2.,1.])
model = WideAndDeepModel()
model.compile(optimizer= 'adam', loss= 'mse')[xs[0],xs[1]], [y1, y2], epochs= 500, verbose = 1)

Initially I was just curious about how to plot the WideAndDeepModel because when I used the plot_model function, and I only got one block instead of elaborate model structure. I am still trying to do something, but it seems that plot_model or model.summary() do not support the subclass of model so well as the functional API.