Skip to content Skip to sidebar Skip to footer

Creating A Multi-channel Network: 'concatenate' Object Has No Attribute 'shape'

I'm trying to make a multiple input model as follow but am having trouble defining the following: The actual input shapes for each separate input When flatten should be used or no

Solution 1:

you need to add the brackets to the Concatenate layer. it's Concatenate()([x.output, y.output])

you can also write your model without the usage of flatten operation. your data are 2D so you don't need to do strange manipulations. you need to use the flatten to pass from 3D (or bigger dimension) to 2D but in your case, you can start from 2D without problems

here a full example

n_sample = 2400
X1 = np.random.uniform(0,1, (n_sample,))  # (2400,)
X2 = np.random.uniform(0,1, (n_sample,3840))  # (2400,3840)
Y = np.random.uniform(0,1, (n_sample,))  # (2400,)

input1= Input(shape = (1, ))
input2= Input(shape = (3840, ))

# The first branch operates on the first input
x = Dense(units = 128, activation="relu")(input1)
x = BatchNormalization()(x)
x = Dense(units = 128, activation="relu")(x)
x = BatchNormalization()(x)
x = Model(inputs=input1, outputs=x)

# The second branch operates on the second input (Protein Embeddings)
y = Dense(units = 128, activation="relu")(input2)
y = BatchNormalization()(y)
y = Dense(units = 128, activation="relu")(y)
y = BatchNormalization()(y)  
y = Model(inputs=input2, outputs=y)

# combine the output of the two branches
combined = Concatenate()([x.output, y.output])

out = Dense(128, activation='relu')(combined)
out = Dropout(0.5)(out)
out = Dense(1)(out)

# The model will accept the inputs of the two branches and then output a single value
model = Model(inputs = [x.input, y.input], outputs = out)
model.compile(loss='mse', optimizer = Adam(lr = 0.001), metrics = ['mse'])

model.fit([X1,X2], Y, epochs=3)

here the notebook

Solution 2:

try np.expand_dims(x,1) for input1 to get Shape: (2400,1)

then np.column_stack((input1,input2))

sample code:

import numpy as np

x = np.array([55., 46., 46.])

input1 = np.expand_dims(x,1)

input2=np.array([[-2.00370455, -2.35689664, -1.96147382,  2.11014128,
         2.59383321,  1.24209607],
       [-1.97130549, -2.19063663, -2.02996445, 2.32125568,
         2.27316046,  1.48600614],
       [-2.01526666, -2.40440917, -1.94321752, 2.15266657,
         2.68460488,  1.23534095]])

print(input1)
print(input2)
input_cs=np.column_stack((input1,input2))
print(input_cs)

it outs

[[55.]
 [46.]
 [46.]][[-2.00370455 -2.35689664 -1.96147382  2.11014128  2.59383321  1.24209607]
 [-1.97130549 -2.19063663 -2.02996445  2.32125568  2.27316046  1.48600614]
 [-2.01526666 -2.40440917 -1.94321752  2.15266657  2.68460488  1.23534095]][[55.         -2.00370455 -2.35689664 -1.96147382  2.11014128  2.59383321
   1.24209607]
 [46.         -1.97130549 -2.19063663 -2.02996445  2.32125568  2.27316046
   1.48600614]
 [46.         -2.01526666 -2.40440917 -1.94321752  2.15266657  2.68460488
   1.23534095]]
>>> 

Post a Comment for "Creating A Multi-channel Network: 'concatenate' Object Has No Attribute 'shape'"