将 TF2.0 中的 feed_dict 替换为函数中张量的张量输入
Replacing feed_dict in TF2.0 for tensor inputs to tensors in a function
我有一个 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() 调用。我想从 'fetches' 张量中获取输出,但它们的输入是模型中更高层的其他张量。我知道哪些张量是我的输入张量,但我如何将数据传递给它们并从后面的层中的张量中获得我想要的输出?
我阅读了有关此主题的 conversion documentation,但它非常简洁且没有帮助。我找不到更多关于 Whosebug 的信息。
可以通过这种方式从模型中获取特定层的输出
#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]
我有一个 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() 调用。我想从 'fetches' 张量中获取输出,但它们的输入是模型中更高层的其他张量。我知道哪些张量是我的输入张量,但我如何将数据传递给它们并从后面的层中的张量中获得我想要的输出?
我阅读了有关此主题的 conversion documentation,但它非常简洁且没有帮助。我找不到更多关于 Whosebug 的信息。
可以通过这种方式从模型中获取特定层的输出
#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]