Pycharm异常管理(八)tensorflow2.1.0(CPU版):错误1 AttributeError: module 'tensorflow_core._api.v2.train' has n...

第一章、异常问题

       参考的吴恩达的课程,练习代码,用pycharm + TensorFlow-cpu2.1.0运行下面的代码,会得到下面的系列错误(逐步注释调试):

import numpy as np
import tensorflow as tf

print("hello world")
w = tf.Variable(0.0, dtype=tf.float32)
cost = tf.add(tf.add(w**2, tf.multiply(-10., w)), 25)
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost) 
init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
print(session.run(w))

session.run(train)
print(session.run(w))

for i in range(1000):
    session.run(train)
print(session.run(w))



Pycharm异常管理(八)tensorflow2.1.0(CPU版):错误1 AttributeError: module 'tensorflow_core._api.v2.train' has n..._第1张图片

第二章、异常信息

       逐步注释,多次运行后,得到下面的错误信息汇总:

错误1——AttributeError: module 'tensorflow_core._api.v2.train' has no attribute 'GradientDescentOptimizer'
错误2——AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'
错误3—— ttributeError: module 'tensorflow' has no attribute 'Session'



第三章、异常问题

        经过多次百度,发现程序没错,是tensoflow-cpu2.1.0搞的鬼。原来tensoflow-cpu2.1.0版本的import语法,与之前的版本不一样了。

第四章、异常解决

改写成下面的形式即可,亲自测试,有效:

import numpy as np
# import tensorflow as tf  # 旧版本的语法
import tensorflow.compat.v1 as tf  # TensorFlow2.1.0版本的语法

tf.compat.v1.disable_eager_execution()  # 必须要这行,否者会发生错误信息: RuntimeError: `loss` passed to Optimizer.compute_gradients should be a function when eager execution is enabled.
print("hello world")
w = tf.Variable(0.0, dtype=tf.float32)
cost = tf.add(tf.add(w**2, tf.multiply(-10., w)), 25)
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

init = tf.global_variables_initializer()
# session = tf.Session() # 旧版本的语法
session = tf.compat.v1.Session()  # TensorFlow2.1.0版本的语法
session.run(init)
print(session.run(w))

session.run(train)
print(session.run(w))

for i in range(1000):
    session.run(train)
print(session.run(w))


        运行结果

"D:\Program Files\Python37\python.exe" E:\PycharmProjects\3-11TesorFlow\sources\mytest.py
 2020-04-22 23:58:27.464366: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
 2020-04-22 23:58:27.464647: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
 hello world
 WARNING:tensorflow:From C:\Users\Administrator\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1635: calling BaseResourceVariable.init (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
 Instructions for updating:
 If using Keras pass *_constraint arguments to layers.
 2020-04-22 23:58:32.745598: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
 2020-04-22 23:58:32.745828: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
 2020-04-22 23:58:32.752420: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-5FDTSAD
 2020-04-22 23:58:32.752868: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-5FDTSAD
 2020-04-22 23:58:32.754668: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
 0.0
 0.099999994
 4.9999886
 Process finished with exit code 0

你可能感兴趣的:(python,tensorflow,深度学习,cuda,bug)