2
votes

I'm trying to concatenate two parallel models in keras, each with different inputs. The relevant code is below.

# model 1
model1_in = Input(shape=(train_x_1.shape[1], train_x_1.shape[2]))
model1_out = LSTM(50, activation='relu',return_sequences=False, name='layer_1')(model1_in)
model1 = Model(model1_in, model1_out)

# model 2
model2_in = Input(shape=(1))
model2_out = Dense(8, activation='relu', name='layer_2')(model2_in)
model2 = Model(model2_in, model2_out)

concatenated = concatenate(inputs=[model1.output, model2.output])
out = Dense(1, activation='relu', name='output_layer')(concatenated)
model = Model([model1_in, model2_in], out)
model.compile(loss='mean_absolute_error', optimizer='adam')


# fit network
history = model.fit([train_x_1,train_x_2], train_y, epochs=100, batch_size=72, validation_data=([test_x_1,test_x_2], test_y), verbose=2, shuffle=False)

The error I'm getting is

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).

and occurs at the model.fit line.

I'm running in IDLE. The train and test values are all arrays, and I've checked that all training inputs are of the same length:

#train_x_1.shape[0]
15465
#train_y.shape[0]
15465
#train_x_2.shape[0]
15465
#test_x_1.shape[0]
1719
#test_x_2.shape[0]
1719
#test_y.shape[0]
1719
#test_x_1
array([[[0.6243922 ],
        [0.5463666 ],
        [0.7083546 ], ... etc ...

Any help would be greatly appreciated- thanks in advance!

Full error trace is as below:

Traceback (most recent call last): File "filepath.py", line 220, in history = model.fit([train_x_1,train_x_2], train_y, epochs=100, batch_size=72, validation_data=([test_x_1,test_x_2], test_y), verbose=2, shuffle=False) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 728, in fit use_multiprocessing=use_multiprocessing) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 224, in fit distribution_strategy=strategy) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 547, in _process_training_inputs use_multiprocessing=use_multiprocessing) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 606, in _process_inputs use_multiprocessing=use_multiprocessing) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/data_adapter.py", line 217, in init x = _process_numpy_inputs(x) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/data_adapter.py", line 703, in _process_numpy_inputs inputs = nest.map_structure(_convert_non_tensor, inputs) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/util/nest.py", line 535, in map_structure structure[0], [func(*x) for x in entries], File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/util/nest.py", line 535, in structure[0], [func(*x) for x in entries], File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/data_adapter.py", line 700, in _convert_non_tensor return ops.convert_to_tensor(x) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1184, in convert_to_tensor return convert_to_tensor_v2(value, dtype, preferred_dtype, name) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1242, in convert_to_tensor_v2 as_ref=False) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1296, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_conversion_registry.py", line 52, in _default_conversion_function return constant_op.constant(value, dtype, name=name) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 227, in constant allow_broadcast=True) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 235, in _constant_impl t = convert_to_eager_tensor(value, ctx, dtype) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 96, in convert_to_eager_tensor return ops.EagerTensor(value, ctx.device_name, dtype) ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).

1
please add your full error trace.Vivek Mehta
@VivekMehta thanks - have just addedbbbrodie1919
SOLVED: The values of x_2 were all as type float, whilst the x_1 values were as float32. .astype('float32') solved my issuebbbrodie1919

1 Answers

1
votes

Specifying the Solution in Answer Section (even though it is present in the Comments Section), for the benefit of the Community.

The values of x_2 were all as type float, whilst the x_1 values were as float32.

Modifying x2 to float32 using x2.astype('float32') has resolved the issue.