AttributeError: 'module' object has no attribute 'truncated_normal'
一:AttributeError: ‘module’ object has no attribute ‘truncated_normal’
tensorflow2.0中修改了这个函数名,因此会报错。
正态分布这个函数truncated_normal()在C:\ProgramData\Anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\python\ops\random_ops.py
打开这个文件后查看源代码,看到下面代码:函数废弃了!!!
@tf_export("random.truncated_normal",
v1=["random.truncated_normal", "truncated_normal"])
@deprecation.deprecated_endpoints("truncated_normal")
def truncated_normal(shape,
mean=0.0,
stddev=1.0,
dtype=dtypes.float32,
seed=None,
name=None):
在TensorFlow2.0官方文档中找到解决方案,地址如下:点击查看官方文档
https://www.tensorflow.org/api_docs/python/tf/random/truncated_normal
解决方案
将tf.truncated_normal([image_size * image_size, num_labels]))修改为:
tf.compat.v1.random.truncated_normal([image_size * image_size, num_labels]))
不再报错!!!
二:module ‘tensorflow_core._api.v2.train’ has no attribute ‘gradientdescentoptimizer’
在TensorFlow2.0,Keras 嵌入到TensorFlow中,变成默认的API,函数调用也发生了变化~!
In TensorFlow 2.0, Keras became the default high-level API, and optimizer functions migrated from tf.keras.optimizers into separate API called tf.optimizers. They inherit from Keras class Optimizer. Relevant functions from tf.train aren’t included into TF 2.0. So to access GradientDescentOptimizer, call tf.optimizers.SGD。。
在TensorFlow2.0官方文档中找到解决方案,地址如下:点击查看官方文档
https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/SGD
解决方案:
将optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)修改为:
optimizer = tf.optimizers.SGD(learning_rate=0.5, momentum=0.0, nesterov=False, name='SGD')
不再报错!!!
上一篇: Unity3D学习笔记(四)分别使用IMGUI和UGUI实现血条的预制设计
下一篇: AttributeError: module 'urllib' has no attribute 'request'
推荐阅读
-
module ‘seaborn‘ has no attribute ‘scatterplot‘解决方案
-
AttributeError: module ‘community‘ has no attribute ‘best_partition‘ 问题解决方法
-
module ‘community‘ has no attribute ‘best_partition‘ [已解决]
-
【python】解决AttributeError: module ‘scipy.misc‘ has no attribute ‘toimage‘问题
-
【Tensorflow】Linux下Tensorflow报错:AttributeError: module ‘tensorflow‘ has no attribute ‘xxxx‘
-
AttributeError: 'module' object has no attribute 'main'
-
AttributeError: module 'sklearn' has no attribute 'linear_model'
-
解决Keras报错AttributeError: 'NoneType' object has no attribute 'inbound_nodes'
-
解决'DataFrame' object has no attribute 'sort'
-
‘MyObject‘ object has no attribute ‘***‘