Keras 2.0.4@OSX 10.12.5(Python 3.6.1)
環境
$ sw_vers ProductName: Mac OS X ProductVersion: 10.12.5 BuildVersion: 16F73 $ pyenv versions system * 3.6.1 (set by /Users/dela/.pyenv/version) $ pip freeze | grep Keras Keras==2.0.4
セットアップ
pip install -U tensorflow pip install -U keras pip install h5py pip install matplotlib
コード
from keras.datasets import mnist from keras.models import Sequential from keras.layers.core import Dense, Activation from keras.utils import np_utils import matplotlib.pyplot as plt #ValueError: Error when checking target: expected activation_2 to have shape (None, 10) but got array with shape (60000, 1) (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(60000, 784) / 255 x_test = x_test.reshape(10000, 784) / 255 model = Sequential([ Dense(512, input_shape=(784,)), Activation('sigmoid'), Dense(10), Activation('softmax') ]) model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) hist = model.fit(x_train, y_train, batch_size=200, verbose=1, epochs=20, validation_split=0.1) score = model.evaluate(x_test, y_test, verbose=1) print("\ntest accuracy : ", score[1]) loss = hist.history['loss'] val_loss = hist.history['val_loss'] plt.plot(range(20), loss, marker = '.', label = 'loss') plt.plot(range(20), val_loss, marker = '.', label = 'val_loss') plt.legend(loc = 'best', fontsize = 10) plt.grid() plt.xlabel('epoch') plt.ylabel('loss') plt.show() acc = hist.history['acc'] val_acc = hist.history['val_acc'] plt.plot(range(20), acc, marker = '.', label = 'acc') plt.plot(range(20), val_acc, marker = '.', label = 'val_acc') plt.legend(loc = 'best', fontsize = 10) plt.grid() plt.xlabel('epoch') plt.ylabel('acc') plt.show()
実行ログ
python multi_layer_perceptron.py Using TensorFlow backend. Train on 54000 samples, validate on 6000 samples Epoch 1/20 2017-06-11 09:45:37.849024: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-11 09:45:37.849085: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-11 09:45:37.849100: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 54000/54000 [==============================] - 5s - loss: 2.0821 - acc: 0.4838 - val_loss: 1.8598 - val_acc: 0.6797 Epoch 2/20 54000/54000 [==============================] - 4s - loss: 1.7041 - acc: 0.7005 - val_loss: 1.5052 - val_acc: 0.7832 Epoch 3/20 54000/54000 [==============================] - 4s - loss: 1.4035 - acc: 0.7591 - val_loss: 1.2284 - val_acc: 0.8223 Epoch 4/20 54000/54000 [==============================] - 4s - loss: 1.1764 - acc: 0.7901 - val_loss: 1.0275 - val_acc: 0.8498 Epoch 5/20 54000/54000 [==============================] - 4s - loss: 1.0121 - acc: 0.8095 - val_loss: 0.8814 - val_acc: 0.8563 Epoch 6/20 54000/54000 [==============================] - 4s - loss: 0.8931 - acc: 0.8239 - val_loss: 0.7762 - val_acc: 0.8685 Epoch 7/20 54000/54000 [==============================] - 4s - loss: 0.8052 - acc: 0.8338 - val_loss: 0.6971 - val_acc: 0.8773 Epoch 8/20 54000/54000 [==============================] - 4s - loss: 0.7386 - acc: 0.8405 - val_loss: 0.6370 - val_acc: 0.8825 Epoch 9/20 54000/54000 [==============================] - 4s - loss: 0.6867 - acc: 0.8474 - val_loss: 0.5895 - val_acc: 0.8853 Epoch 10/20 54000/54000 [==============================] - 4s - loss: 0.6451 - acc: 0.8526 - val_loss: 0.5518 - val_acc: 0.8870 Epoch 11/20 54000/54000 [==============================] - 5s - loss: 0.6113 - acc: 0.8565 - val_loss: 0.5204 - val_acc: 0.8915 Epoch 12/20 54000/54000 [==============================] - 4s - loss: 0.5832 - acc: 0.8602 - val_loss: 0.4947 - val_acc: 0.8942 Epoch 13/20 54000/54000 [==============================] - 4s - loss: 0.5595 - acc: 0.8637 - val_loss: 0.4726 - val_acc: 0.8958 Epoch 14/20 54000/54000 [==============================] - 5s - loss: 0.5393 - acc: 0.8668 - val_loss: 0.4542 - val_acc: 0.8985 Epoch 15/20 54000/54000 [==============================] - 4s - loss: 0.5218 - acc: 0.8697 - val_loss: 0.4383 - val_acc: 0.8995 Epoch 16/20 54000/54000 [==============================] - 4s - loss: 0.5066 - acc: 0.8720 - val_loss: 0.4245 - val_acc: 0.8998 Epoch 17/20 54000/54000 [==============================] - 5s - loss: 0.4931 - acc: 0.8737 - val_loss: 0.4119 - val_acc: 0.9013 Epoch 18/20 54000/54000 [==============================] - 4s - loss: 0.4813 - acc: 0.8748 - val_loss: 0.4010 - val_acc: 0.9028 Epoch 19/20 54000/54000 [==============================] - 4s - loss: 0.4705 - acc: 0.8772 - val_loss: 0.3921 - val_acc: 0.9038 Epoch 20/20 54000/54000 [==============================] - 6s - loss: 0.4610 - acc: 0.8788 - val_loss: 0.3831 - val_acc: 0.9052 9984/10000 [============================>.] - ETA: 0s test accuracy : 0.8905
グラフ描画
以下のエラーが発生するので、対応が必要
エラー
Fontconfig warning: line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf /Users/dela/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/font_manager.py:280: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. 'Matplotlib is building the font cache using fc-list. ' Traceback (most recent call last): File "multi_layer_perceptron.py", line 5, in <module> import matplotlib.pyplot as plt File "/Users/dela/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module> _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File "/Users/dela/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup globals(),locals(),[backend_name],0) File "/Users/dela/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 19, in <module> from matplotlib.backends import _macosx RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
対応
$ python -c "import matplotlib;print(matplotlib.matplotlib_fname())" $ vim /Users/dela/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc line:38 #backend : macosx line:39 backend : Tkagg
グラフ