在Google Colaboratory中创建TensorFlow变量张量时遇AttributeError: module 'tensorflow' has no attribute 'variable'错误的解决方案咨询
解决TensorFlow中
AttributeError: module 'tensorflow' has no attribute 'variable'的问题 哈哈,这就是个典型的大小写小疏忽!你代码里的tf.variable写错了,TensorFlow里用来定义变量的正确写法是大写V开头的tf.Variable。
修正后的代码:
import tensorflow as tf var_1 = tf.Variable(tf.ones([2,3]))
为啥会出这个错?
TensorFlow的API命名有明确规范:类名采用大驼峰命名法(首字母大写),Variable是一个类,所以必须大写开头;而小写的tf.variable并不是TensorFlow提供的属性,所以Python会抛出属性不存在的错误。
Colab默认使用TensorFlow 2.x环境,修正后直接运行代码就能顺利创建变量张量了~
内容的提问来源于stack exchange,提问作者arfan rifqi
相关产品推荐
相关产品推荐

