Create a TensorFlow graph with multiple random for

2019-08-25 08:34发布

问题:

Is it possible to create a graph in TensorFlow containing multiple RandomForestGraphs?

Instead of one random forest with num_classes=3 I want to have three random forests, one classifying only classes 1 and 2, the second one classes 2 and 3 and the third classes 3 and 1. In front of these classifiers is a arbitrator element deciding which forest to train or infer based on the current class (i.e., class 1 -> tree 1, class 2 -> tree 2, ...). This way i hope to restrict the possible outcomes in a simple way: input of (previously) class 1 can only result in class 1 or 2, etc.

Now, the problem is that Tensorflow internally uses a graph and as soon as I try to create a second RandomForestGraphs, I get an error:

ValueError: Variable device_dummy_0 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?

My understanding is, that the internal default graph is configured after the first call of RandomForestGraphs and the second call is seen as redundant, basically orverwriting the first one. Code used:

hp0 = tensor_forest.ForestHParams(num_classes=2, num_features=num_features, regression=False, num_trees=num_trees, max_nodes=max_nodes).fill()
hp1 = tensor_forest.ForestHParams(num_classes=2, num_features=num_features, regression=False, num_trees=num_trees, max_nodes=max_nodes).fill()

forest_graph0 = tensor_forest.RandomForestGraphs(hp0)
forest_graph1 = tensor_forest.RandomForestGraphs(hp1) # ERROR

Is there a more elegant way to solve this? Or can I create and combine two graphs in TensorFlow somehow?