【问题标题】:I get an error when I enter new input into the middle layer with keras使用 keras 将新输入输入到中间层时出现错误
【发布时间】:2019-11-17 17:18:51
【问题描述】:

我想知道当新的输入是从已经学习的模型的中间层输入时如何获得输出。

def encoder(input_):
    d1 = Dense(3, activation='relu', name='encoder_input')(input_)
    d2 = Dense(2, name='encoder_output')(d1)
    return d2

def decoder(input_):
    d1 = Dense(3, activation='relu', name='decoder_input')(input_)
    d2 = Dense(2, name='decoder_output')(d1)
    return d2

# input
input = Input(shape=(2,))

# output
output = decoder(encoder(input))

# model
model = Model(inputs=input, outputs=output)

model.compile(optimizer='adam', loss='mean_squared_error')
model_hist = model.fit(x_train, x_test,
                        epochs=n_epoch,
                        batch_size=batch_size,
                        verbose=verbose,
                        shuffle=True)

#error:Graph disconnected: cannot obtain value for tensor 
tmp_model = Model(input=model.get_layer('decoder_input').input, output=model.get_layer('decoder_output').output)
output = tmp_model.predict(data)

【问题讨论】:

  • 也请分享您遇到的错误。这将有助于我们更好地解决您的问题。
  • 谢谢! ValueError: Graph disconnected: 无法在“input”层获取张量 Tensor("input:0", shape=(?, 2), dtype=float32) 的值。访问以下先前层没有问题:[]

标签: python python-3.x tensorflow machine-learning keras


【解决方案1】:

我可以在你的代码中找到两个错误:

  1. d2的编码器中你忘记了name=之前的'encoder_output'

  2. 你忘记了行中的“s”

# model
model = Model(input=input, output=output)

应该是:

model = Model(inputs=input, outputs=output)

否则它对我有用。 希望对您有所帮助!

【讨论】:

  • 谢谢。但是,解决这个问题不会是一个根本的解决方案。我想知道当新的输入是从已经学习的模型的中间层输入时如何获得输出。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2019-11-13
  • 2021-05-20
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-01
  • 2022-08-11
相关资源
最近更新 更多