温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行Tensorflow中Session和InteractiveSession的比较

发布时间:2021-12-17 16:18:56 来源:亿速云 阅读:106 作者:柒染 栏目:大数据

如何进行Tensorflow中Session和InteractiveSession的比较,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。


01

Session



每一个Session都维护各自变量的副本。

如下所示:


W = tf.Variable(10)

sess1 = tf.Session()

sess2 = tf.Session()

sess1.run(W.initializer)

sess2.run(W.initializer)

print sess1.run(W.assign_add(10)) # >> 20

print sess2.run(W.assign_sub(2)) # >> ?


?等号8,sess1和sess2各自维护W,所以sess1中W增加10,不会影响sess2的W,所以它等于10-2=8.




02

Session vs InteractiveSession


有时候我们会看到:InteractiveSession,而不是Session,它们区别是?


One major change is the use of an InteractiveSession, which allows us to run variables without needing to constantly refer to the session object (less typing!). 


InteractiveSession()


sess = tf.InteractiveSession()

a = tf.constant(5.0)

b = tf.constant(6.0)

c = a * b

# We can just use 'c.eval()' without specifying the context 'sess'

print(c.eval())

sess.close()


Session()


sess = tf.Session()

a = tf.constant(5.0)

b = tf.constant(6.0)

c = a * b

with tf.Session() as sess: 

    sess.run(print(c.eval()))

看完上述内容,你们掌握如何进行Tensorflow中Session和InteractiveSession的比较的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI