将模型输出用作其他模型的输入

将模型输出用作其他模型的输入

本文介绍了Keras:将模型输出用作其他模型的输入:将符号张量馈送到模型时,我们期望张量具有静态批大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个模型,其中首先训练model_A,然后使用model_A的输出来训练model_C:

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训练似乎很好,但是在训练model_C时出现以下错误:

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!

推荐答案

将模型的输出(符号张量)放入model.fit没有意义,因为那里没有输入数据.您应该首先从模型A获取预测,然后使用它们拟合模型C:

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'])

这篇关于Keras:将模型输出用作其他模型的输入:将符号张量馈送到模型时,我们期望张量具有静态批大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:06