【问题标题】:getting error while feeding inferences of one model to the generator of another model in keras将一个模型的推论提供给 keras 中另一个模型的生成器时出错
【发布时间】:2019-10-12 20:30:18
【问题描述】:

我设计了一个自动编码器并对其进行了训练。现在,我想从编码器中提取特征并将它们提供给另一个模型的生成器。

这是我实现的自动编码器。


    x = Conv1D(64, 5, activation="relu", padding="same")(input) 
    #x = BatchNormalization()(x)
    x = MaxPooling1D(2, padding="same")(x)
    x = Conv1D(32, 5, activation="relu", padding="same")(x) 
    #x = BatchNormalization()(x)
    x = MaxPooling1D(2, padding="same")(x)
    x = Conv1D(16, 5, activation="relu", padding="same")(x) 
    x = MaxPooling1D(2, padding="same")(x)
    x = Conv1D(1, 5, activation="relu", padding="same")(x)
    encoded = MaxPooling1D(2, padding="same")(x)


    x = Conv1D(1, 5, activation="relu", padding="same")(encoded) 
    #x = BatchNormalization()(x)
    x = UpSampling1D(2)(x) 
    x = Conv1D(16, 2, activation='relu',padding='same')(x)
    x = UpSampling1D(2)(x) 
    x = Conv1D(32, 2, activation='relu',padding='same')(x) 
    #x = BatchNormalization()(x)
    x = UpSampling1D(2, )(x) 
    x = Conv1D(64, 2, activation='relu',padding='same')(x)
    x = UpSampling1D(2, )(x) 
    decoded = Conv1D(1, 3, activation='sigmoid', padding='same')(x) 

    autoencoder = Model(input, decoded)

这是我得到编码器部分输出的地方。

def extract_features_from_encoder(left,right):
   get_8th_layer_output = K.function([autoencoder.layers[0].input],

      [autoencoder.layers[8].output])

   left_encoded = get_8th_layer_output([left])[0]
   right_encoded = get_8th_layer_output([right])[0]

这是我的发电机:

def generator_encoded():
   left = ...
   right = ...

   left_encoded,right_encoded = 
   extract_features_from_encoder(left,right)

   yield ({'input_1': left_encoded, 'input_2': right_encoded}, 
   {'dense_1': y})

这是我调用生成器的地方:

model.fit_generator(generator_encoded(),......)

这是我的模型。

input_l = Input(shape=(1000,1))
input_r = Input(shape=(1000,1))

shared_lstm = CuDNNLSTM(100)
encoded_l = shared_lstm(input_l)
encoded_r = shared_lstm(input_r)

L1_layer = Lambda(lambda tensors:K.abs(tensors[0] - tensors[1]))
L1_distance = L1_layer([encoded_l, encoded_r])

prediction = Dense(1,activation='sigmoid')(L1_distance)

model = Model(inputs=[input_l,input_r],outputs=prediction)

这是我在运行此模型时遇到的错误。

tensorflow.python.framework.errors_impl.InvalidArgumentError: 
Tensor input_2:0, specified in either feed_devices or 
fetch_devices was not found in the Graph

谁能告诉我哪里出了问题以及如何解决?

【问题讨论】:

    标签: python keras keras-layer pre-trained-model


    【解决方案1】:

    问题已解决。在从自动编码器进行推断之前添加 with tf.Session: 会使问题消失。由于实例化了多个图,因此需要明确告知模型使用默认图。

    【讨论】:

      猜你喜欢
      • 2018-09-24
      • 2019-11-09
      • 2018-05-20
      • 2020-05-28
      • 1970-01-01
      • 2018-07-10
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多