深度学习的坑--module 'tensorflow' has no attribute 'global_variables_initializer'

最近自己的深度学习基于scikit-neuralnetwork的已经学完,在学习tensorflow的时候遇到了一个很狗血的问题。


import tensorflow as tf

data1 =  tf.constant(2.5)

data2 = tf.Variable(10,name='var')

print(data1)

print(data2)

tf.compat.v1.disable_eager_execution()

sess= tf.compat.v1.Session()

sess.run(tf.global_variables_initializer())

print(sess.run(data1))

print(sess.run(data2))

如下错误:

module 'tensorflow' has no attribute 'global_variables_initializer'

后来又在网上搜索,网友给出的答案是使用variables_all_initializer,但是依然报同样的错误。

于是我自己开始琢磨,查找了tf中的方法也没有初始化变量的。后来我想上述错误是因为版本不一致导致的,那么如果让版本一致不就可以了吗,于是我把代码改成了tf.compat.v1.global_variables_initializer().

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容