วิธีการติดตั้ง Keras + Tensorflow (GPU version) บน windows 10 บทความนี้เขียนขึ้นเพื่อหลายๆ…
Pisit Bee
388

‘Trains a simple convnet on the MNIST dataset.\n\nGets to 99.25% test accuracy after 12 epochs\n(there is still a lot of margin for parameter tuning).\n16 seconds per epoch on a GRID K520 GPU.\n’
>>>
>>> from __future__ import print_function
>>> import keras
Using TensorFlow backend.
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\__init__.py”, line 3, in <module>
 from . import utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\__init__.py”, line 6, in <module>
 from . import conv_utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\conv_utils.py”, line 9, in <module>
 from .. import backend as K
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\__init__.py”, line 89, in <module>
 from .tensorflow_backend import *
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\tensorflow_backend.py”, line 5, in <module>
 import tensorflow as tf
ModuleNotFoundError: No module named ‘tensorflow’
>>> from keras.datasets import mnist
Using TensorFlow backend.
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\__init__.py”, line 3, in <module>
 from . import utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\__init__.py”, line 6, in <module>
 from . import conv_utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\conv_utils.py”, line 9, in <module>
 from .. import backend as K
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\__init__.py”, line 89, in <module>
 from .tensorflow_backend import *
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\tensorflow_backend.py”, line 5, in <module>
 import tensorflow as tf
ModuleNotFoundError: No module named ‘tensorflow’
>>> from keras.models import Sequential
Using TensorFlow backend.
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\__init__.py”, line 3, in <module>
 from . import utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\__init__.py”, line 6, in <module>
 from . import conv_utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\conv_utils.py”, line 9, in <module>
 from .. import backend as K
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\__init__.py”, line 89, in <module>
 from .tensorflow_backend import *
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\tensorflow_backend.py”, line 5, in <module>
 import tensorflow as tf
ModuleNotFoundError: No module named ‘tensorflow’
>>> from keras.layers import Dense, Dropout, Flatten
Using TensorFlow backend.
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\__init__.py”, line 3, in <module>
 from . import utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\__init__.py”, line 6, in <module>
 from . import conv_utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\conv_utils.py”, line 9, in <module>
 from .. import backend as K
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\__init__.py”, line 89, in <module>
 from .tensorflow_backend import *
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\tensorflow_backend.py”, line 5, in <module>
 import tensorflow as tf
ModuleNotFoundError: No module named ‘tensorflow’
>>> from keras.layers import Conv2D, MaxPooling2D
Using TensorFlow backend.
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\__init__.py”, line 3, in <module>
 from . import utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\__init__.py”, line 6, in <module>
 from . import conv_utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\conv_utils.py”, line 9, in <module>
 from .. import backend as K
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\__init__.py”, line 89, in <module>
 from .tensorflow_backend import *
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\tensorflow_backend.py”, line 5, in <module>
 import tensorflow as tf
ModuleNotFoundError: No module named ‘tensorflow’
>>> from keras import backend as K
Using TensorFlow backend.
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\__init__.py”, line 3, in <module>
 from . import utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\__init__.py”, line 6, in <module>
 from . import conv_utils
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\utils\conv_utils.py”, line 9, in <module>
 from .. import backend as K
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\__init__.py”, line 89, in <module>
 from .tensorflow_backend import *
 File “C:\Anaconda3\envs\py37tf\lib\site-packages\keras\backend\tensorflow_backend.py”, line 5, in <module>
 import tensorflow as tf
ModuleNotFoundError: No module named ‘tensorflow’
>>>
>>> batch_size = 128
>>> num_classes = 10
>>> epochs = 12
>>>
>>> # input image dimensions
… img_rows, img_cols = 28, 28
>>>
>>> # the data, shuffled and split between train and test sets
… (x_train, y_train), (x_test, y_test) = mnist.load_data()
Traceback (most recent call last):
 File “<stdin>”, line 2, in <module>
NameError: name ‘mnist’ is not defined
>>>
>>> if K.image_data_format() == ‘channels_first’:
… 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)

Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘K’ is not defined
>>> x_train = x_train.astype(‘float32’)
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘x_train’ is not defined
>>> x_test = x_test.astype(‘float32’)
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘x_test’ is not defined
>>> x_train /= 255
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘x_train’ is not defined
>>> x_test /= 255
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘x_test’ is not defined
>>> print(‘x_train shape:’, x_train.shape)
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘x_train’ is not defined
>>> print(x_train.shape[0], ‘train samples’)
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘x_train’ is not defined
>>> print(x_test.shape[0], ‘test samples’)
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘x_test’ is not defined
>>>
>>> # convert class vectors to binary class matrices
… y_train = keras.utils.to_categorical(y_train, num_classes)
Traceback (most recent call last):
 File “<stdin>”, line 2, in <module>
NameError: name ‘keras’ is not defined
>>> y_test = keras.utils.to_categorical(y_test, num_classes)
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘keras’ is not defined
>>>
>>> model = Sequential()
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘Sequential’ is not defined
>>> model.add(Conv2D(32, kernel_size=(3, 3),
… activation=’relu’,
… input_shape=input_shape))
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> model.add(Conv2D(64, (3, 3), activation=’relu’))
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> model.add(MaxPooling2D(pool_size=(2, 2)))
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> model.add(Dropout(0.25))
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> model.add(Flatten())
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> model.add(Dense(128, activation=’relu’))
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> model.add(Dropout(0.5))
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> model.add(Dense(num_classes, activation=’softmax’))
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>>
>>> model.compile(loss=keras.losses.categorical_crossentropy,
… optimizer=keras.optimizers.Adadelta(),
… metrics=[‘accuracy’])
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>>
>>> model.fit(x_train, y_train,
… batch_size=batch_size,
… epochs=epochs,
… verbose=1,
… validation_data=(x_test, y_test))
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> score = model.evaluate(x_test, y_test, verbose=0)
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘model’ is not defined
>>> print(‘Test loss:’, score[0])
Traceback (most recent call last):
 File “<stdin>”, line 1, in <module>
NameError: name ‘score’ is not defined
>>> print(‘Test accuracy:’, score[1])