###質問内容
以下2つのコードは同義ではないのですか?
Python
sess = tf.Session() # 何らかの処理 sess.close()
Python
with tf.Session() as tf: # 何らかの処理
同義であるのならば、以下2つのコードではなぜ前者だけエラーが発生するのかお聞きしたいです。
###該当のソースコード
Python
import tensorflow as tf x = tf.placeholder(tf.float32) b = tf.Variable(1.) y = x + b t = tf.placeholder(tf.float32) loss = tf.reduce_sum(tf.square(y - t)) train_step = tf.train.AdamOptimizer().minimize(loss) # ここまで2つのコード間の相違はありません、以降の部分を書き換えました sess = tf.Session() sess.run(tf.global_variables_initializer()) train_step.run(feed_dict = {x: 2., t: 5.}) sess.close()
Python
import tensorflow as tf x = tf.placeholder(tf.float32) b = tf.Variable(1.) y = x + b t = tf.placeholder(tf.float32) loss = tf.reduce_sum(tf.square(y - t)) train_step = tf.train.AdamOptimizer().minimize(loss) # ここまで2つのコード間の相違はありません、以降の部分を書き換えました with tf.Session() as sess: sess.run(tf.global_variables_initializer()) train_step.run(feed_dict = {x: 2., t: 5.})
###発生している問題・エラーメッセージ
ValueError: Cannot execute operation using `run()`: No default session is registered. Use `with sess.as_default():` or pass an explicit session to `run(session=sess)`
まだ回答がついていません
会員登録して回答してみよう