TensorFlow2和keras详细安装教程
环境配置:
系统:win10
1、安装 Anaconda,安装教程请自行百度。这里我使用的版本是:
C:\Users\HaiBin>conda --version
conda 4.8.3
2、安装python
C:\Users\HaiBin>python --version
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前,先依次安装下面的这个库
(tf2) C:\Users\HaiBin>conda install mingw libpython
(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安装成功。
(tf2_keras) C:\Users\HaiBin>python
Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>>
接下来,需要将上面新建的环境配置到pycharm中
PyCharm配置
1、创建一个新的项目,如下图:
到始,TensorFlow2和Keras已经成功安装完成了。
来段代码测试,试一试:
from keras.datasets import mnist
from keras.utils import to_categorical
train_X, train_y = mnist.load_data()[0]
train_X = train_X.reshape(-1, 28, 28, 1)
train_X = train_X.astype('float32')
train_X /= 255
train_y = to_categorical(train_y, 10)
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Flatten, Dropout, Dense
from keras.losses import categorical_crossentropy
from keras.optimizers import Adadelta
model = Sequential()
model.add(Conv2D(32, (5,5), activation='relu', input_shape=[28, 28, 1]))
model.add(Conv2D(64, (5,5), activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss=categorical_crossentropy,
optimizer=Adadelta(),
metrics=['accuracy'])
batch_size = 100
epochs = 8
model.fit(train_X, train_y,
batch_size=batch_size,
epochs=epochs)
test_X, test_y = mnist.load_data()[1]
test_X = test_X.reshape(-1, 28, 28, 1)
test_X = test_X.astype('float32')
test_X /= 255
test_y = to_categorical(test_y, 10)
loss, accuracy = model.evaluate(test_X, test_y, verbose=1)
print('loss:%.4f accuracy:%.4f' %(loss, accuracy))
运行结果:
安装Keras时,遇到一些小Bug,最后贴出解决方法,报错如下:
TypeError: Descriptors cannot not be created directly.
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.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
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是否安装成功
(tf2) C:\Users\HaiBin>python
Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>>
版权声明:本文为qq_39312146原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。