TensorFlow中sess.run()无法运行?新手代码问题求助
Hey there, let's work through this TensorFlow issue step by step. Based on the code snippets you shared, here are some common pitfalls that might be causing your sess.run() to fail:
build() returns the model instance correctly Your call is
fittt(model_method.build(self,...),...)— make sure thebuildmethod inmodel_method.pyactually returnsselfat the end. If it doesn't, themodelparameter passed tofitttwill beNone(or some unrelated value), and callingmodel.fit()would break before even getting tosess.run().Fix example:
def build(self,...): self.op_C,self.op_A = self.function_A(...) self.op_B = self.function_B(self.op_C,...) return self # Don't forget this critical line!
build() and fit() In your fit method, you're using sess = tf.Session(graph=self.graph, config=config). But did you initialize self.graph before creating operations in build()? If self.graph wasn't set up properly, the ops op_C, op_A, op_B might be getting created in TensorFlow's default graph instead of self.graph — which means the session can't find them when you run sess.run().
- Fix steps:
- Initialize
self.graphin your model's__init__method:def __init__(self,...): self.graph = tf.Graph() # Wrap build in the graph context to ensure all ops go here with self.graph.as_default(): self.build(...) # Or call build later, but always wrap it in this context - If you call
build()outside__init__, make sure to wrap that call inwith self.graph.as_default():so all operations are added to the correct graph.
- Initialize
TensorFlow requires all variables to be initialized before running operations. If function_A or function_B create variables (like tf.Variable), you need to run the initializer in your session first:
Modify your fit method:
def fit(self,...): with tf.Session(graph=self.graph, config=config) as sess: # Initialize all variables in the graph sess.run(tf.global_variables_initializer()) # Now run your operations (don't forget feed_dict if you're using placeholders!) BB, AA = sess.run([self.op_B, self.op_A], feed_dict={...})
sess.run() arguments Make sure you're passing valid TensorFlow ops/tensors to sess.run(). If self.op_B or self.op_A aren't actual TensorFlow objects (e.g., they're raw Python values or uninitialized placeholders), sess.run() will throw an error.
- Double-check that
function_Aandfunction_Breturn valid TensorFlow tensors/ops, not plain Python variables. - If you're using placeholders, confirm you're passing all required values via
feed_dictinsess.run().
self is passed in main.py When you call fittt(model_method.build(self,...),...) in main.py, ensure that self here refers to an instance of your model class from model_method.py. If you're accidentally passing the wrong self (like a non-existent self from the main module), build() will fail to set up the operations correctly.
If you still hit an error after checking these points, share the exact error message — that will help pinpoint the issue even faster!
内容的提问来源于stack exchange,提问作者Flo

