如何从导入的TensorFlow元图中检索输入变量名称?
I’ve been in this exact situation before—importing a pre-trained model without access to the original code can feel like guessing in the dark, but there are straightforward ways to figure out the tensor names you need. Here’s what you can do:
1. List All Tensor Nodes in the Graph
First, after restoring the model, you can dump out every node name in the TensorFlow graph. This will give you a full list to sift through:
graph = tf.get_default_graph() # Iterate through all nodes and print their names for node in graph.as_graph_def().node: print(node.name)
You’ll get a long list, but look for keywords that make sense for your use case (since your model is from a VizDoom RL project, think game-related terms).
2. Filter for Likely Input Tensors
For RL models built on VizDoom, inputs are almost always game frames/screens. So search for names containing terms like:
inputobservationscreenframestate
To narrow things down quickly, add a filter to your loop:
for node in graph.as_graph_def().node: if any(keyword in node.name.lower() for keyword in ["input", "observation", "screen"]): print(node.name)
Remember: When you want to fetch the actual tensor, you need to append :0 to the node name (this refers to the first output tensor of that node). For example, if you see input_layer, the tensor would be input_layer:0.
3. Find Output Tensors
For reinforcement learning models, outputs are typically actions, Q-values, or policy distributions. Look for names with:
actionq_valuepolicylogit
Again, use a filter to speed this up:
for node in graph.as_graph_def().node: if any(keyword in node.name.lower() for keyword in ["action", "q_value", "policy"]): print(node.name)
4. Validate the Tensors
Once you have candidate names, test them by feeding in dummy data that matches the expected input shape (VizDoom models often use 4 stacked 84x84 grayscale frames, so shape (1, 84, 84, 4)):
import numpy as np # Replace with your candidate input/output names input_tensor = graph.get_tensor_by_name("your_input_node:0") output_tensor = graph.get_tensor_by_name("your_output_node:0") # Generate dummy input test_input = np.random.rand(1, 84, 84, 4).astype(np.float32) # Run the model result = session.run(output_tensor, feed_dict={input_tensor: test_input}) print("Model output:", result)
If you get a reasonable output (like a discrete action index or Q-values for each action), you’ve found the right tensors!
内容的提问来源于stack exchange,提问作者AFanthomme

