Keras: Using model output as the input for another: When feeding symbolic tensors to a model, we expect thetensors to have a static batch size

我是研究僧i 提交于 2021-01-29 17:27:47

问题


I have the following two models, where model_A is trained first, then the output of model_A is used to train the model_C:

import keras
from keras.layers import Input, Dense
from keras.models import Model

inputs = Input(shape=(12,))

# ---------------------------------------
# model_A
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions_A = Dense(3, activation='softmax')(x)
model_A = Model(inputs=inputs, outputs=predictions_A)

model_A.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model_A.fit(my_data_x, axis = 1), pd.get_dummies(my_data['target_cate'],prefix=['cate_']))
#----------------------------------------

input_C_out_A = Input(shape=(3,))
# Concatenating the two input layers
concat = keras.layers.concatenate([inputs, input_C_out_A])
x1 = Dense(64, activation='relu')(concat)
x1 = Dense(64, activation='relu')(x1)
predictions_C= Dense(1, activation='sigmoid')(x1)

model_C = Model(inputs=[inputs, input_C_out_A], outputs=predictions_C)
model_C.compile(loss='mean_squared_error', optimizer='adam')
model_C.fit([my_data_x,predictions_A], my_data['target_numeric'])

model_A training seems to be fine, but then I got the following errors when training the model_C:

Epoch 1/1
374667/374667 [==============================] - 11s 30us/step - loss: 0.3157 - acc: 0.9119
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-78-8df7b1dec93f> in <module>
     28 model_C = Model(inputs=[inputs, input_C_out_A], outputs=predictions_C)
     29 model_C.compile(loss='mean_squared_error', optimizer='adam')
---> 30 model_C.fit([my_data_x,predictions_A], my_data['target_numeric'])

~/workspace/git/tensorplay/venv/lib/python3.7/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

~/workspace/git/tensorplay/venv/lib/python3.7/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    749             feed_input_shapes,
    750             check_batch_axis=False,  # Don't enforce the batch size.
--> 751             exception_prefix='input')
    752 
    753         if y is not None:

~/workspace/git/tensorplay/venv/lib/python3.7/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
     90         data = data.values if data.__class__.__name__ == 'DataFrame' else data
     91         data = [data]
---> 92     data = [standardize_single_array(x) for x in data]
     93 
     94     if len(data) != len(names):

~/workspace/git/tensorplay/venv/lib/python3.7/site-packages/keras/engine/training_utils.py in <listcomp>(.0)
     90         data = data.values if data.__class__.__name__ == 'DataFrame' else data
     91         data = [data]
---> 92     data = [standardize_single_array(x) for x in data]
     93 
     94     if len(data) != len(names):

~/workspace/git/tensorplay/venv/lib/python3.7/site-packages/keras/engine/training_utils.py in standardize_single_array(x)
     23                 'When feeding symbolic tensors to a model, we expect the'
     24                 'tensors to have a static batch size. '
---> 25                 'Got tensor with shape: %s' % str(shape))
     26         return x
     27     elif x.ndim == 1:

ValueError: When feeding symbolic tensors to a model, we expect thetensors to have a static batch size. Got tensor with shape: (None, 3)

Any idea what I missed? Thanks!


回答1:


It makes no sense to put the output of a model (a symbolic tensor) into model.fit, since there is no input data there. You should first obtain predictions from model A and then use them to fit model C:

pred_a = model_A.predict(my_data_x)

model_C.fit([my_data_x, pred_a], my_data['target_numeric'])



回答2:


The model_C works by:

  1. Giving the input data to model_A,
  2. Getting output of model_A, and
  3. Feeding that, along with the original inputs, to the first Dense layer.

So implement just what you said (and try to keep models separate, i.e. each with its own input/output layers):

input_C = Input(shape=(12,))
out_A = model_A(input_C) # get the output of model_A
concat = keras.layers.concatenate([input_C, out_A])
x1 = Dense(64, activation='relu')(concat)
x1 = Dense(64, activation='relu')(x1)
predictions_C= Dense(1, activation='sigmoid')(x1)

model_C = Model(inputs=input_C, outputs=predictions_C)
model_C.compile(loss='mean_squared_error', optimizer='adam')
model_C.fit(my_data_x, my_data['target_numeric'])

If you don't want the model_A to be trained when training model_C (i.e. if you have already trained model_A and don't want its weights to be changed), just set model_A.trainable = False before compiling model_C.



来源:https://stackoverflow.com/questions/57483198/keras-using-model-output-as-the-input-for-another-when-feeding-symbolic-tensor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!