简体   繁体   中英

How input should relate/map to label y if Keras Model.fit() is given a list of input train arrays>

I am trying to work with a Deep learning model in two of the following scenarios, where two different inputs are given. I want to achieve following:

  1. Train two models (with different weights but same architecture) with same input and concatenate the result. So in model.fit(), I am passing just the trainX value. Code is given below. It works fine.

     def create_model(input_tensor): x= Conv1D(filters = 16, kernel size=6, strides = 5, kernel_initializer = "uniform", activation = "relu")(input_tensor) x= GlobalMaxPooling1D()(x) x = Dense(2,activation ='softmax')() return x dataframe = pd.read_csv(Filename, index_col=0) X= dataframe.values[:,:].astype(float) Y = dataframe.values[:,1] trainx, testx, trainy, testy = train_test_split(X,Y, test_Szie= 0.2, random_state=200, shuffle =True) input_shape = (33000,1) input_tensor = Input(input_shape) pred_a = create_model(input_tensor) pred_b = create_model(input_tensor) out = keras.layers.Multiply()([pred_a, pred_b]) model =Model(inputs=(input_tensor), outputs=out) model.compile(loss='categorical_crossentropy', optimizer= 'Adam', metrics =['accuracy']) histroy = model.fit(trainX, trainy)
  2. Train same model (with same weights) twice but with different inputs. I am confused how to pass inputs in this case. In normal cases, we have equal number of instances in both trainX and trainy data. If I pass a list like model.fit([x_train_1, x_train_2], trainy) , then the number of instances of combined x_train_1, x_train_2 will be double than y. How trainy corresponds to the input trainx in this case?

The input and corresponding output of a model have shapes as X = (batch_size, ....), y = (batch_size,....) In case of multiple inputs, you can define multiple input layers and feed them to your different model instances as follows

inp_A = Input(shape=(...))
inp_B = Input(shape=(...))

pred_A = create_model(inp_A)
pred_B = create_model(inp_B)

*** Other layers and code ****
model = Model(inputs=[inp_A, inp_B], outputs=out)
*** Other code ***

Then you can call model.fit with passing a list of inputs and a single output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM