You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Check if build() returns the model instance correctly

Your call is fittt(model_method.build(self,...),...) — make sure the build method in model_method.py actually returns self at the end. If it doesn't, the model parameter passed to fittt will be None (or some unrelated value), and calling model.fit() would break before even getting to sess.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!
2. Verify graph consistency between 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.graph in 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 in with self.graph.as_default(): so all operations are added to the correct graph.
3. Missing variable initialization

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={...})
4. Validate 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_A and function_B return valid TensorFlow tensors/ops, not plain Python variables.
  • If you're using placeholders, confirm you're passing all required values via feed_dict in sess.run().
5. Confirm the correct 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:34:08