分享(簡易版)的tensorflow win10 + keras安裝步驟
gpu的安裝環節很多有興趣的人安裝如果有問題可以討論喔~
1.anaconda 3.6
安裝網址https://www.continuum.io/downloads
建議下載python 3.6
2.加入環境變數path %SystemRoot%\system32
- 不然後續有些東西無法設定好
3.Tensorflow只支援python 3.5環境,所以要把環境設為3.5
請打開cmd然後請輸入下列指令
1.conda create --name tensorflow python=3.5 [建立一個anaconda的python3.5的虛擬環境] 2.activate tensorflow [啟用環境] 3.conda install jupyter scipy mingw libpython [scipy是必要的jupyter是建議安裝的,這樣後續開發比較好用,mingw libpython是安裝keras會用到在這邊先裝]
(4)pip install tensorflow
Or pip install tensorflow-gpu [看是要安裝CPU還是GPU版]
4.安裝keras
[1]設定環境變數path\(要依照你安裝的位置更改喔\)
[C:\Anaconda2;C:\Anaconda2\Scripts;C:\Anaconda2\MinGW\bin;
C:\Anaconda2\MinGW\x86\_64-w64-mingw32\lib;]
[2] pip install keras
[3]打開`C:\Users\(username)\.keras`,修改文件内的`keras.json`文件如下:
{
"image\_dim\_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
[4]keras.backend.backend() 或是直接 import keras 也會show出來
5.下載範例檔
git keras example(https://github.com/fchollet/keras/tree/master/examples)
如果不能git 請先安裝git或是直接用網址抓 conda install -c anaconda git=2.11.1
6.然後執行mnist_cnn.py(python mnist_cnn.py)
- 成功就會看到下面的畫面
PS1 -- Debug
可以先import tensorflow或是import keras看有沒有錯誤
有的話就看看錯誤訊息來debug
(請先進入python後輸入……)
PS2 : GPU安裝事項
可以參考這個網站http://www.netinstructions.com/how-to-install-and-run-gpu-enabled-tensorflow-on-windows/
----安裝CUDA8.0(driver不要裝,然後設定環境變數)
----安裝CUDNN
沒有問題的話應該可以直接run…….
有問題的話要看問題在哪………太多點了…….
PS :(1) VM 不能使用GPU
\(2\)Bash on Ubuntu on Windows 也不行
\(3\)https://developer.nvidia.com/cuda-gpus
showPICs Code
# Plot ad hoc mnist instances
from keras.datasets import mnist
import matplotlib.pyplot as plt
# load (downloaded if needed) the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# plot 4 images as gray scale
plt.subplot(221)
plt.imshow(X_train[8], cmap=plt.get_cmap('gray'))
plt.subplot(222)
plt.imshow(X_train[9], cmap=plt.get_cmap('gray'))
plt.subplot(223)
plt.imshow(X_train[20], cmap=plt.get_cmap('gray'))
plt.subplot(224)
plt.imshow(X_train[25], cmap=plt.get_cmap('gray'))
# show the plot
plt.show()
tensorboard Code(最後要在起一個cmd進入virtualEnv後執行註解的那段指令)
'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
import keras.callbacks
import keras.backend.tensorflow_backend as KTF
batch_size = 128
nb_classes = 10
nb_epoch = 12
log_filepath = '/tmp/keras_log'
# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
if K.image_dim_ordering() == 'th':
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
border_mode='valid',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, histogram_freq=1)
cbks = [tb_cb]
history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test),callbacks=cbks)
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
#tensorboard --logdir=/tmp/keras_log