我有一个Keras回调,它可以从特定的Keras图层中检索值,如下所示:
def run(self, fetches, next_batch):
"""Run fetches using the validation data passed in during initialization."""
input_data, target_data = self.sess.run(next_batch)
feed_dict = {self.model.inputs[0]: input_data,
self.model._targets[0]: target_data}
result = self.sess.run(fetches=fetches, feed_dict=feed_dict)
return result
next_batch
是tf1中的Dataset.make_one_shot_iterator.get_next()调用。我已将其替换为next(iter(ds))。那部分工作正常。
但是我不知道如何重写sess.run()调用。我想从“获取”张量中获取输出,但它们的输入是模型中更高的其他张量。我知道哪些张量是我的输入张量,但是如何将数据传递到它们中并从后面的张量中获得所需的输出?
我阅读了有关此主题的转换文档,但它确实非常简洁且无济于事。我无法找到有关stackoverflow的更多信息。
特定层的输出可以通过这种方式从模型中获取
#get the output from the layer1
out1 = model.get_layer(layer1_name).output
#get the output from the layer2
out2 = model.get_layer(layer2_name).output
#a new model with outputs of the layers
MyModel = Model(inputs=model.input,outputs=[out1,out2])
现在您可以传递类似的值
#call the model
mymodel = MyModel()
#pass your inputs
outputs = mymodel(inputs)
请记住,outputs
是两个输出的数组,可以通过
output1 = outputs[0]
output2 = outputs[1]