安装环境:WIN10 AMD集显😭😭
Anaconda版本:Anaconda3-4.4.0 (64位)(建议不要装最新的,历史版本可在 清华大学开源软件镜像站 下载
Python版本:Python 3.5(在安装Anaconda3时勾选了3.6)
图一
Tensorflow版本:tensorflow 1.8
图二
一. 安装Anaconda
1. 根据上面清华大学开源软件镜像站下载对应的安装包,并安装,(看挺多博文建议安装在C盘,所以我的也是安装在了C盘)
2. 检查是否安装成功:conda --version
(在Anaconda Prompt 或者 命令提示符 输入命令行均可)
图三
3. 检测当前安装环境:conda info --envs
*注意* 本图是安装之后的图片,安装前,并没有
tensorflow C:\Users\xxx\Anaconda3\envs\tensorflow 一行
图四
4. 检查目前有哪些版本的python可以安装:
conda search --full-name python
5. 选择一个python版本安装(这里选择python3.5.4版本):
conda create --name tensorflow python=3.5.4
6. 激活tensorflow :
activate tensorflow
7. tensorflow环境被成功添加:(如图四所示) ;
conda info --envs
查看tensorflow环境下的python版本:python --version
(如图五所示)
图五
二. Tensorflow 1.8 安装
1. 打开anaconda prompt
2. 安装 cpu 版本的 tensorflow
注意:需要激活tensorflow下
python -m pip install tensorflow==1.8.0 -i https://pypi.douban.com/simple
3.安装成功后测试
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
期间有个提示:
解决方法:可参考此文
三.安装keras
1. 打开anacoda prompt
2. 激活tensorflow
activate tensorflow
3. 输入python
4. 安装 keras
pip install keras
5. 测试
安装完成后,输入
import keras
若返回 Using TensorFlow backend, 则说明安装成功
6. 测试
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout
# Generate dummy data
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100, 20))
y_test = np.random.randint(2, size=(100, 1))
model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(x_train, y_train,
epochs=20,
batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
测试结果
1000/1000 [==============================] - 0s 282us/step - loss: 0.7214 - acc: 0.5000
Epoch 2/20
1000/1000 [==============================] - 0s 18us/step - loss: 0.7102 - acc: 0.4870
Epoch 3/20
1000/1000 [==============================] - 0s 17us/step - loss: 0.7049 - acc: 0.5080
Epoch 4/20
1000/1000 [==============================] - 0s 15us/step - loss: 0.6985 - acc: 0.5110
Epoch 5/20
1000/1000 [==============================] - 0s 15us/step - loss: 0.6963 - acc: 0.5120
Epoch 6/20
1000/1000 [==============================] - 0s 15us/step - loss: 0.6952 - acc: 0.5100
Epoch 7/20
1000/1000 [==============================] - 0s 14us/step - loss: 0.6970 - acc: 0.5170
Epoch 8/20
1000/1000 [==============================] - 0s 13us/step - loss: 0.6894 - acc: 0.5240
Epoch 9/20
1000/1000 [==============================] - 0s 13us/step - loss: 0.6976 - acc: 0.5150
Epoch 10/20
1000/1000 [==============================] - 0s 14us/step - loss: 0.6922 - acc: 0.5320
Epoch 11/20
1000/1000 [==============================] - 0s 14us/step - loss: 0.6889 - acc: 0.5340
Epoch 12/20
1000/1000 [==============================] - 0s 14us/step - loss: 0.6916 - acc: 0.5150
Epoch 13/20
1000/1000 [==============================] - 0s 14us/step - loss: 0.6920 - acc: 0.5180
Epoch 14/20
1000/1000 [==============================] - 0s 15us/step - loss: 0.6917 - acc: 0.5410
Epoch 15/20
1000/1000 [==============================] - 0s 13us/step - loss: 0.6897 - acc: 0.5410
Epoch 16/20
1000/1000 [==============================] - 0s 12us/step - loss: 0.6983 - acc: 0.4920
Epoch 17/20
1000/1000 [==============================] - 0s 13us/step - loss: 0.6910 - acc: 0.5230
Epoch 18/20
1000/1000 [==============================] - 0s 13us/step - loss: 0.6873 - acc: 0.5390
Epoch 19/20
1000/1000 [==============================] - 0s 14us/step - loss: 0.6920 - acc: 0.5250
Epoch 20/20
1000/1000 [==============================] - 0s 14us/step - loss: 0.6910 - acc: 0.5490
100/100 [==============================] - 0s 460us/step
7.遇到的一个问题:keras版本过高
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via
目前版本太高,需要降版本到2.2
pip install keras==2.2 -i https://pypi.douban.com/simple
欧克,安装完成,大功告成!
参考文章: