h4nk
h4nk

Reputation: 35

Concatenation layer in Keras

I am trying to accept 2 inputs into my model. But it has a weird problem.

x1= layers.Input((20000,))
x2= layers.Reshape((200,100), input_shape=(20000,))(x1)

y1= layers.Input((200000,))
y2= layers.Reshape((2000,100), input_shape=(200000,))(y1)

combine = layers.Concatenate(axis=1)([x2, y2])
model = tf.keras.Model(inputs=[x1, y1], outputs=combine)
model.predict([datasetA, datasetB])

If I accept one input, the model can run.

model = tf.keras.Model(inputs=[x1], output=x2)
model.predict(datasetA)

But if I accept two input, the model is dead.

Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.PrefetchDataset'>"}), <class 'NoneType'>

i.e. The structure of my dataset is:

<PrefetchDataset shapes: ((None, 20000), (None,)), types: (tf.int64, tf.int32)>
1 Dataset -> 5 Batche_data and 5 batch_Label
Each Batch_data -> 3 records
Each record -> [20000]

Each batch_Label -> 3 records
Each label -> 0 / 1 for classification

How can I solve this issue?

model.summary()
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_3 (InputLayer)            [(None, 20000)]      0                                            
__________________________________________________________________________________________________
input_4 (InputLayer)            [(None, 200000)]     0                                            
__________________________________________________________________________________________________
reshape_2 (Reshape)             (None, 200, 100)     0           input_3[0][0]                    
__________________________________________________________________________________________________
reshape_3 (Reshape)             (None, 2000, 100)    0           input_4[0][0]                    
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 2200, 100)    0           reshape_2[0][0]                  
                                                                 reshape_3[0][0]                  

Upvotes: 0

Views: 680

Answers (1)

Pouria Nikvand
Pouria Nikvand

Reputation: 370

did you use tensoflow.keras for importing layers? and also can you print the model.summary The only thing that may be a problem is the shapes you pass through the model.

This code is working properly. tensorflow == 2.0.0

import tensorflow as tf
import numpy as np

datasetA = np.zeros((10,20000),dtype=int)
datasetB = np.zeros((10,200000),dtype=int)

x1= tf.keras.layers.Input((20000,))
x2= tf.keras.layers.Reshape((200,100), input_shape=(20000,))(x1)

y1= tf.keras.layers.Input((200000,))
y2= tf.keras.layers.Reshape((2000,100), input_shape=(200000,))(y1)

combine = tf.keras.layers.Concatenate(axis=1)([x2, y2])
model = tf.keras.Model(inputs=[x1, y1], outputs=combine)
model.summary()
model.predict([datasetA, datasetB])
print('the predict done')], outputs=combine)

Upvotes: 1

Related Questions