首页 > 解决方案 > 当为多个输入调用 model.fit 函数时,会引发基数问题

问题描述

    def build_model():
    
       #input1 = Input(shape=(50,), dtype=tf.int32, name='x1')
      inputs = keras.Input(shape=(150,150,3))
      input2=keras.Input(shape = (248064,))
    
      x = layers.Conv2D(16, 3, activation="relu")(inputs)
      x = layers.Conv2D(32, 3, activation="relu")(x)
      x = layers.MaxPooling2D(3)(x)
      x = layers.Conv2D(32, 3, activation="relu")(x)
    
      flat=layers.Flatten()(x) 
      print("Flat Shape:",flat.shape)
      print("Type of flat",type(flat))
    
      print("feature vector Shape:",input2.shape)
      print("Type of input2",type(flat))
      concatenatedFeatures = tf.concat([flat, input2],-1)
    #concatenatedFeatures=np.append(flat,input2,axis=1)
      print("Type of concatfeature",type(concatenatedFeatures))
    
    
      print("concatenatedFeatures Shape:",concatenatedFeatures.shape)
      dense = Dense(128)(concatenatedFeatures)
    
      print("dense Shape:",dense.shape)
    #y = layers.Dense(4)(dense)
      y = layers.Dense(4,activation='softmax')(dense)
      print("y Shape:",y.shape)
    
      return (Model(inputs=[inputs,input2], outputs=y))

model = build_model()
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam')
histrory=model.fit([x_train,File_data],y_train)
 

它遇到了一个model.fit()错误

ValueError: Data cardinality is ambiguous:
  x sizes: 20, 248064
  y sizes: 20
Make sure all arrays contain the same number of samples.

我试图将 flatten 层的输出与来自 file_data 的额外功能连接起来......输入的形状如下:

Flat Shape: (None, 67712)
feature vector Shape: (None, 248064)
concatenatedFeatures Shape: (None, 315776)

标签: pythonconcatenation

解决方案


推荐阅读