升级到tensorflow2.0,我整个人都不好了

 

版本升级到 tensorflow 2.0 的悲惨经历.....

没事别升级

Tensorflow 2.0发布已经有一段时间了,各种基于新API的教程看上去的确简单易用,一个简单的mnist手写识别只需要下面不到20行代码就OK了,

import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([  tf.keras.layers.Flatten(input_shape=(28, 28)),  tf.keras.layers.Dense(512, activation=tf.nn.relu),  tf.keras.layers.Dropout(0.2),  tf.keras.layers.Dense(10, activation=tf.nn.softmax)])model.compile(optimizer='adam',              loss='sparse_categorical_crossentropy',              metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)model.evaluate(x_test, y_test)

于是我一激动,直接更新到了最新版本,直接执行

  •  
pip install –upgrade tensorflow-gpu

完成更新,打开以前写的程序,然后我就悲剧了。不管是简单的还是复杂的代码演示,惊讶的发现没有一个可以跑的,最后发现我以前写的tensorflow+Kears教程居然可以跑,结果一跑一个更大的悲剧等着我,直接跟我说CUDA版本不是10.0的版本,版本太低。于是我就认真重新看了tensorflow2.0的版本release说明,发现这么一句话:

Many APIs are either gone or moved in TF 2.0. Some of the major changes include removing tf.app, tf.flags, and tf.logging in favor of the now open-source absl-py, rehoming projects that lived in tf.contrib, and cleaning up the main tf.* namespace by moving lesser used functions into subpackages like tf.math.

我终于对这段话有了很深刻与痛苦的领悟。是真的该删的删,该移的移!该抛弃的抛弃、完全没有考虑到开发者的切身感受。

当你开始运行程序时候,一般会顺序给你下面几个惊喜!

AttributeError: module 'tensorflow' has no attribute 'get_variable'AttributeError: module 'tensorflow' has no attribute 'placeholder'AttributeError: module 'tensorflow' has no attribute 'Session'

还有没有天理了,这些不是在tensorflow1.x中必须的吗,怎么说没就没有了,告诉你是真的没有,在tensorflow2.0中,如果还想让它有怎么办?

用tf.compat.v1.xxxx上面的那些no attribute错误就会解决了。举例

​​​​​​​

tf.Session()改为tf.compat.v1.Session()

然后我很高兴的去继续运行程序,就发现一个大BUG在等我

tensorflow.python.framework.errors_impl.internalerror: cudagetdevice() failed. status: cudageterrorstring symbol not found.

原因:

找不到cudart64_100.dll,这个是CUDA10.0的,我之前安装的是CUDA9.0,tensorflow2.0不支持了,所以这个必须换,怎么办,一顿卸载安装+配置猛如虎,我终于全部搞定了。在windows10系统下面 Tensorflow 2.0 + VS2015 + CUDA10.0 终于工作了,这个我只是改好了第一个代码,这样改下去,什么时候才完,别担心,后来我又发现了tensorflow官方提供的另外一个神器,可以帮助它代码自动的从v1版本转换到v2版本,可能连tensorflow官方自己也不好意思它跨度这么大的版本更新,所以还算提供了一个贴心的工具。直接cmd之后在命令行运行即可实现代码的自动转换:

如果你完全不想改动v1版本的代码,怎么办,这么操作即可:​​​​​​​

import tensorflow.compat.v1 as tftf.disable_v2_behavior()

亲测好用!但是我有个疑问,如果这样我升级干嘛,就是为了版本号吗?

总之一句话,升级不谨慎、代码靠手改!

后来我又看了看tensorflow2.0的宣传,它说强大、易用、可扩展!但是它没告诉我从tensorflow1.x 到tensorflow 2.0 都是坑!

最后还有个福利送给大家,录了个视频,教大家如何安装、升级、配置tensorflow2.0 + CUDA10.0支持,需要自取:

​​​​​​​https://www.bilibili.com/video/av70734671/

发布了62 篇原创文章 · 获赞 235 · 访问量 169万+

猜你喜欢

转载自blog.csdn.net/javastart/article/details/102525102
今日推荐