当前位置:   article > 正文

TensorFlow2和keras详细安装教程_tensorflow2,kares库

tensorflow2,kares库

环境配置:

系统:win10

1、安装 Anaconda,安装教程请自行百度。这里我使用的版本是:

  1. C:\Users\HaiBin>conda --version
  2. conda 4.8.3

2、安装python

  1. C:\Users\HaiBin>python --version
  2. Python 3.7.6

3、在查找中,输入Anaconda Prompt命令,并运行它。

运行后

准备工作到这里,基本完成,接下来,安装TensorFlow2和Keras。

安装TensorFlow2

1、在Anaconda prompt窗口输入下面的命令,创建一个环境

(base) C:\Users\HaiBin>conda create -n tf2 python=3.7.6

这是新建一个名为tf2,并且python版本是3.7.6的一个环境

2、切换到刚刚创建的tf2环境中,准备安装TensorFlow2,输入如下命令:

(base) C:\Users\HaiBin>conda activate tf2

3、安装TensorFlow2

(tf2) C:\Users\HaiBin>pip install tensorflow==2.0.0

安装Keras

1、安装Keras前,先依次安装下面的这个库

  1. (tf2) C:\Users\HaiBin>conda install mingw libpython
  2. (tf2) C:\Users\HaiBin>pip install theano

2、最后安装keras

(tf2) C:\Users\HaiBin>pip install keras==2.3.1

注意:keras一定要和你的TensorFlow版本匹配,因为我安装的TensorFlow是2.0.0版本,与它对应的是keras2.3.1

以上命令均在Anaconda prompt窗口中完成,否则有可能安装不成功。

测试

运行python,输入import keras回车后,结果出来Using TensorFlow backend.表示TensorFlow安装成功。

  1. (tf2_keras) C:\Users\HaiBin>python
  2. Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import keras
  5. Using TensorFlow backend.
  6. >>>

接下来,需要将上面新建的环境配置到pycharm中

PyCharm配置

1、创建一个新的项目,如下图:

 到始,TensorFlow2和Keras已经成功安装完成了。

来段代码测试,试一试:

  1. from keras.datasets import mnist
  2. from keras.utils import to_categorical
  3. train_X, train_y = mnist.load_data()[0]
  4. train_X = train_X.reshape(-1, 28, 28, 1)
  5. train_X = train_X.astype('float32')
  6. train_X /= 255
  7. train_y = to_categorical(train_y, 10)
  8. from keras.models import Sequential
  9. from keras.layers import Conv2D, MaxPool2D, Flatten, Dropout, Dense
  10. from keras.losses import categorical_crossentropy
  11. from keras.optimizers import Adadelta
  12. model = Sequential()
  13. model.add(Conv2D(32, (5,5), activation='relu', input_shape=[28, 28, 1]))
  14. model.add(Conv2D(64, (5,5), activation='relu'))
  15. model.add(MaxPool2D(pool_size=(2,2)))
  16. model.add(Flatten())
  17. model.add(Dropout(0.5))
  18. model.add(Dense(128, activation='relu'))
  19. model.add(Dropout(0.5))
  20. model.add(Dense(10, activation='softmax'))
  21. model.compile(loss=categorical_crossentropy,
  22. optimizer=Adadelta(),
  23. metrics=['accuracy'])
  24. batch_size = 100
  25. epochs = 8
  26. model.fit(train_X, train_y,
  27. batch_size=batch_size,
  28. epochs=epochs)
  29. test_X, test_y = mnist.load_data()[1]
  30. test_X = test_X.reshape(-1, 28, 28, 1)
  31. test_X = test_X.astype('float32')
  32. test_X /= 255
  33. test_y = to_categorical(test_y, 10)
  34. loss, accuracy = model.evaluate(test_X, test_y, verbose=1)
  35. print('loss:%.4f accuracy:%.4f' %(loss, accuracy))

运行结果:

 

安装Keras时,遇到一些小Bug,最后贴出解决方法,报错如下:

  1. TypeError: Descriptors cannot not be created directly.
  2. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
  3. If you cannot immediately regenerate your protos, some other possible workarounds are:
  4. 1. Downgrade the protobuf package to 3.20.x or lower.
  5. 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

这说明我们使用的protobuf库的版本高了,可以卸载已经安装过的protobuf 版本,再安装3.20.x以下的版本,我们可以使用3.19.0版本即可,命令如下:

1、卸载protobuf 已经安装的版本

 (tf2) C:\Users\HaiBin>pip uninstall protobuf

2、安装3.19.0版本

(tf2) C:\Users\HaiBin>pip install protobuf==3.19.0

3、测试keras是否安装成功

  1. (tf2) C:\Users\HaiBin>python
  2.  Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
  3.  Type "help", "copyright", "credits" or "license" for more information.
  4.  >>> import keras
  5.  Using TensorFlow backend.
  6.  >>>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/181558
推荐阅读
相关标签
  

闽ICP备14008679号