AssertionError: 在Keras中使用multi_gpu_model()时无法计算输出张量

我有两个Keras子模型(model_1, model_2),我通过逻辑上将它们“串联”起来,使用keras.models.Model()构建了我的完整model。我的意思是,model_2接受model_1的输出以及一个额外的输入张量,而model_2的输出就是我的完整model的输出。完整的model成功创建,并且我也可以使用compile/train/predict

然而,我希望通过在2个GPU上运行来并行化model的训练,因此我使用了multi_gpu_model(),但这导致了错误:

AssertionError: Could not compute output Tensor("model_2/Dense_Decoder/truediv:0", shape=(?, 33, 22), dtype=float32)

我尝试过分别使用multi_gpu_model(model_1, gpus=2)multi_gpu_model(model_2, gpus=2)来并行化这两个子模型,两者都成功了。问题出现在完整模型上。

我使用的是Tensorflow 1.12.0Keras 2.2.4。一个展示问题的代码片段(至少在我的机器上)是:

from keras.layers import Input, Dense,TimeDistributed, BatchNormalizationfrom keras.layers import CuDNNLSTM as LSTMfrom keras.models import Modelfrom keras.utils import multi_gpu_modeldec_layers = 2codelayer_dim = 11bn_momentum = 0.9lstm_dim = 128td_dense_dim = 0output_dims = 22dec_input_shape = [33, 44]# MODEL 1latent_input = Input(shape=(codelayer_dim,), name="Latent_Input")# Initialize list of state tensors for the decoderdecoder_state_list = []for dec_layer in range(dec_layers):    # The tensors for the initial states of the decoder    name = "Dense_h_" + str(dec_layer)    h_decoder = Dense(lstm_dim, activation="relu", name=name)(latent_input)    name = "BN_h_" + str(dec_layer)    decoder_state_list.append(BatchNormalization(momentum=bn_momentum, name=name)(h_decoder))    name = "Dense_c_" + str(dec_layer)    c_decoder = Dense(lstm_dim, activation="relu", name=name)(latent_input)    name = "BN_c_" + str(dec_layer)    decoder_state_list.append(BatchNormalization(momentum=bn_momentum, name=name)(c_decoder))# Define model_1model_1 = Model(latent_input, decoder_state_list)# MODEL 2inputs = []decoder_inputs = Input(shape=dec_input_shape, name="Decoder_Inputs")inputs.append(decoder_inputs)xo = decoder_inputsfor dec_layer in range(dec_layers):    name = "Decoder_State_h_" + str(dec_layer)    state_h = Input(shape=[lstm_dim], name=name)    inputs.append(state_h)    name = "Decoder_State_c_" + str(dec_layer)    state_c = Input(shape=[lstm_dim], name=name)    inputs.append(state_c)    # RNN layer    decoder_lstm = LSTM(lstm_dim,                   return_sequences=True,                   name="Decoder_LSTM_" + str(dec_layer))    xo = decoder_lstm(xo, initial_state=[state_h, state_c])    xo = BatchNormalization(momentum=bn_momentum, name="BN_Decoder_" + str(dec_layer))(xo)    if td_dense_dim > 0: # Squeeze LSTM interconnections using Dense layers        xo = TimeDistributed(Dense(td_dense_dim), name="Time_Distributed_" + str(dec_layer))(xo)# Final Dense layer to return probabilitiesoutputs = Dense(output_dims, activation='softmax', name="Dense_Decoder")(xo)# Define model_2model_2 = Model(inputs=inputs, outputs=[outputs])# FULL MODELlatent_input = Input(shape=(codelayer_dim,), name="Latent_Input")decoder_inputs = Input(shape=dec_input_shape, name="Decoder_Inputs")# Stack the two models# Propagate tensors through 1st modelx = model_1(latent_input)# Insert decoder_inputs as the first input of the 2nd modelx.insert(0, decoder_inputs)# Propagate tensors through 2nd modelx = model_2(x)# Define full modelmodel = Model(inputs=[latent_input, decoder_inputs], outputs=[x])# Parallelize the modelparallel_model = multi_gpu_model(model, gpus=2)parallel_model.summary()

非常感谢任何帮助/建议。


回答:

我找到了问题的解决方案,但我还不确定如何解释这个解决方案。

问题是由x.insert(0, decoder_inputs)引起的,我将其替换为x = [decoder_inputs] + x。这两种方法似乎都得到了相同的张量列表,但是multi_gpu_model在第一种情况下会报错。

Related Posts

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

如何向神经网络输入两个不同大小的输入?

我想向神经网络输入两个数据集。第一个数据集(元素)具有…

逻辑回归与机器学习有何关联

我们正在开会讨论聘请一位我们信任的顾问来做机器学习。一…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注