如何从基本操作构建TensorFlow循环?

2024-04-16 08:08:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在TensorFlow中创建一个非常简单的循环,使用5个基本操作(Enter、Merge、Switch、Exit、NextIteration)。 问题是,当一个节点(操作)被添加到图中时,它的所有输入都必须给出。因为它是一个循环(合并开关NextIteration Merge),在这个过程中的某个地方,我必须在没有输入可用的情况下创建一个节点

我在一个博客(https://blog.csdn.net/mydear_11000/article/details/53693065)中找到的唯一文档是:

During the graph construction, the inputs to the "Merge" nodes are two copies of each enter node; when the NextIteration nodes are constructed, the Merge nodes are fixed by replacing one of the Enter inputs with a NextIteration input.

但是,它没有说明如何“修复”它。所有文档和答案(例如Is it possible to modify an existing TensorFlow computation graph?)都说,节点一旦添加,就不能再更改。 有人能帮忙吗,如何从这5个操作(而不是其他更高级别的while_循环结构等)生成一个循环

根据要求,一种元代码,我试图实现:

Graph.AddPlaceholder('input');
Graph.AddEnter('input','myframe');
Graph.AddMerge(['enteroutput','nextiterationoutput'],2);
Graph.AddGreater('mergeoutput','ten');
Graph.AddSwitch('mergeoutput','greateroutput');
Graph.AddExit('swithtrueoutput');
Graph.AddAdd('switchfalseoutput','one');
Graph.AddNextIteration('addoutput');

问题是,AddMerge失败了,因为当时还没有创建NextIteration节点,所以TF_AddInput函数(Graph.AddMerge中的内容)无法添加输入。这就是为什么引用的博客建议使用一些“临时”值创建合并节点,例如

Graph.AddMerge(['enteroutput','enteroutput'],2);

一旦在最后一步将NextIteration添加到图形中,“修复”合并节点,这是我不能做的


Tags: the文档input节点tensorflowmergearegraph