Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将TF2.0中的feed_dict替换为函数中张量的张量输入_Python_Tensorflow_Machine Learning_Keras_Tensorflow2.0 - Fatal编程技术网

Python 将TF2.0中的feed_dict替换为函数中张量的张量输入

Python 将TF2.0中的feed_dict替换为函数中张量的张量输入,python,tensorflow,machine-learning,keras,tensorflow2.0,Python,Tensorflow,Machine Learning,Keras,Tensorflow2.0,我有一个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_

我有一个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\u batch
是一个数据集。在tf1中调用one\u shot\u迭代器。我用next(iter(ds))代替了它。那部分很好用

但是,我不知道如何重写sess.run()调用。我想从“fetches”张量中获取输出,但它们的输入是模型中更高的其他张量。我知道哪些张量是我的输入张量,但如何将数据传递给它们,并从后面的层中的张量中获得我想要的输出


我读了关于这个问题的文章,但它确实简明扼要,毫无帮助。我找不到更多关于stackoverflow的信息。

可以通过这种方式从模型中获取特定层的输出


#从第1层获取输出
out1=model.get\u层(layer1\u名称)。输出
#从层2获取输出
out2=模型。获取层(层2\u名称)。输出
#具有层输出的新模型
MyModel=Model(输入=模型。输入,输出=[out1,out2])
现在您可以像这样传递值

#调用模型
mymodel=mymodel()
#传递您的输入
输出=mymodel(输入)
记住,
outputs
是两个输出的数组,可以通过

output1=输出[0]
输出2=输出[1]

TF2.0不需要
session()
eagent execution
我知道,但如何将数据传递到TF2.0中的模型中?我必须在函数中定义整个图形吗?您可以通过调用model
model=MyModel()
在模型中传递数据,然后传递数据
model(input_data)
,但这不起作用,因为我不想只运行模型-我需要从模型中间返回某些张量的值,不仅仅是输出层。在TF1中,可以通过将这些张量传递给session.run()和feed命令。如果需要层的输出,则可以使用
out1=model.get\u layer(layer\u name)。输出
,然后通过
model(输入=输入,输出=out1)