第一章、异常问题
跑一个AI算法之前的版本基本信息:
- Pycharm2019..3.4
- python3.7.2
- pip18.1
- numpy1.18.2
- 已安装Microsoft Visual C++ Redistributable for Visual Studio 2019
然后跑下面的程序:
# 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))
警告信息、错误信息如下:
![](https://img-blog.csdnimg.cn/img_convert/4c50aeaa557e645f18ee5fcf2a1ea01c.png)
第二章、异常信息
"D:\Program Files\Python37\python.exe" E:/PycharmProjects/3-11TesorFlow/sources/test.py
2020-04-18 09:57:07.670145: 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-18 09:57:07.680411: 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
2020-04-18 09:57:22.186079: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-04-18 09:57:22.186320: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
2020-04-18 09:57:22.194163: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-5FDTSAD
2020-04-18 09:57:22.194599: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-5FDTSAD
2020-04-18 09:57:22.206499: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
File "E:/PycharmProjects/3-11TesorFlow/sources/test.py", line 7, in
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
AttributeError: module 'tensorflow_core._api.v2.train' has no attribute 'GradientDescentOptimizer'
Process finished with exit code 1
第三章、异常原因
-
-
- 警告信息——跑TensorFlow算法需要cudart64_101.dll、nvcuda.dll等,此前,我pycharm没有安装相关的支持软件,导致出错。
- 错误信息——由于版本tensorFlow-cpu2.1.0升级了,模块
GradientDescentOptimizer是存在的,但是tensorFlow-cpu2.1.0语法改变了导致找不到模块。
-
第四章、异常解决
4.1、 警告信息的解决方法
- 显卡不支持GPU,警告信息可以忽略它。
- 若你的显卡支持GPU,可以在这里解决掉警告信息。我们参考下面的这篇非常非常非常非常非常非常非常非常非常非常非常非常非常详细的文章来解决跑TensorFlow算法所有需要安装的软件包。
4.2、 错误信息
换言之,显卡只支持CPU计算的情况下,也就是我现在遇到的情况。解决方法:
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))
'''
# 参考demo,以便解决tf.Session()问题
import tensorflow as tf
tf.compat.v1.disable_eager_execution() # 必须要这行,否者会发生错误信息: untimeError: The Session graph is empty. Add operations to the graph before calling run().
hello = tf.constant('hello,tensorflow')
sess= tf.compat.v1.Session()
print(sess.run(hello))
'''