keras安装与使用所遇到的问题【持续更新】
2018-07-31 本文已影响72人
purewater2014
在mac上通过Anaconda安装keras。
运行命令行时,提示:
/Users/leebenson/.anaconda/navigator/a.tool: line 1: syntax error near unexpected token `('
/Users/leebenson/.anaconda/navigator/a.tool: line 1: `bash --init-file <(echo "source activate /Users/leebenson/anaconda/envs/Dev;")'
原因是:安装zsh,导致。
解决方案:chsh -s /bin/bash
调试keras的examples的示例
1.问题:调试mnist_mlp.py,国内无法下载mnist.npz数据
修改:
image.png
import numpy as np
path='./mnist.npz'
f = np.load(path)
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
f.close()
文件需要事先下载好
2. TypeError: softmax() got an unexpected keyword argument 'axis'
解决方案:
image.png image.png
去掉axis参数
最后运行结果:
EricdeMacBook-Pro:keras eric$ python mnist_mlp.py
Using TensorFlow backend.
60000 train samples
10000 test samples
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 512) 401920
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 512) 262656
_________________________________________________________________
dropout_2 (Dropout) (None, 512) 0
_________________________________________________________________
dense_3 (Dense) (None, 10) 5130
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
2018-07-31 19:05:42.865865: 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.
2018-07-31 19:05:42.865898: 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.
2018-07-31 19:05:42.865909: 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.
2018-07-31 19:05:42.865918: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-07-31 19:05:42.865926: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
60000/60000 [==============================] - 6s 104us/step - loss: 0.2468 - acc: 0.9246 - val_loss: 0.1178 - val_acc: 0.9635
Epoch 2/20
60000/60000 [==============================] - 7s 114us/step - loss: 0.1048 - acc: 0.9681 - val_loss: 0.0822 - val_acc: 0.9754
Epoch 3/20
60000/60000 [==============================] - 6s 107us/step - loss: 0.0750 - acc: 0.9769 - val_loss: 0.0672 - val_acc: 0.9793
Epoch 4/20
60000/60000 [==============================] - 7s 124us/step - loss: 0.0614 - acc: 0.9816 - val_loss: 0.0775 - val_acc: 0.9789
Epoch 5/20
60000/60000 [==============================] - 6s 107us/step - loss: 0.0516 - acc: 0.9847 - val_loss: 0.0759 - val_acc: 0.9796
Epoch 6/20
60000/60000 [==============================] - 7s 123us/step - loss: 0.0432 - acc: 0.9873 - val_loss: 0.0735 - val_acc: 0.9807
Epoch 7/20
60000/60000 [==============================] - 8s 127us/step - loss: 0.0390 - acc: 0.9886 - val_loss: 0.0730 - val_acc: 0.9825
Epoch 8/20
60000/60000 [==============================] - 7s 120us/step - loss: 0.0331 - acc: 0.9900 - val_loss: 0.0858 - val_acc: 0.9808
Epoch 9/20
60000/60000 [==============================] - 7s 123us/step - loss: 0.0310 - acc: 0.9910 - val_loss: 0.0935 - val_acc: 0.9791
Epoch 10/20
60000/60000 [==============================] - 7s 124us/step - loss: 0.0280 - acc: 0.9918 - val_loss: 0.0872 - val_acc: 0.9808
Epoch 11/20
60000/60000 [==============================] - 7s 121us/step - loss: 0.0260 - acc: 0.9926 - val_loss: 0.0880 - val_acc: 0.9826
Epoch 12/20
60000/60000 [==============================] - 7s 120us/step - loss: 0.0255 - acc: 0.9931 - val_loss: 0.0921 - val_acc: 0.9834
Epoch 13/20
60000/60000 [==============================] - 7s 110us/step - loss: 0.0230 - acc: 0.9937 - val_loss: 0.1013 - val_acc: 0.9812
Epoch 14/20
60000/60000 [==============================] - 7s 110us/step - loss: 0.0222 - acc: 0.9940 - val_loss: 0.0900 - val_acc: 0.9832
Epoch 15/20
60000/60000 [==============================] - 6s 105us/step - loss: 0.0223 - acc: 0.9940 - val_loss: 0.0966 - val_acc: 0.9848
Epoch 16/20
60000/60000 [==============================] - 8s 127us/step - loss: 0.0190 - acc: 0.9950 - val_loss: 0.1020 - val_acc: 0.9830
Epoch 17/20
60000/60000 [==============================] - 6s 107us/step - loss: 0.0195 - acc: 0.9946 - val_loss: 0.1088 - val_acc: 0.9830
Epoch 18/20
60000/60000 [==============================] - 7s 118us/step - loss: 0.0182 - acc: 0.9950 - val_loss: 0.1061 - val_acc: 0.9832
Epoch 19/20
60000/60000 [==============================] - 8s 134us/step - loss: 0.0186 - acc: 0.9954 - val_loss: 0.1119 - val_acc: 0.9842
Epoch 20/20
60000/60000 [==============================] - 8s 132us/step - loss: 0.0180 - acc: 0.9956 - val_loss: 0.1260 - val_acc: 0.9802
Test loss: 0.12595016931
Test accuracy: 0.9802
3.TypeError: write() argument must be str, not bytes
解决:open(join(log_dir, 'metadata.tsv'), 'wb+')
4.TypeError: assign() got an unexpected keyword argument 'name'
解决: image.png去掉参数:name=name
5.tf.nn.leaky_relu not defined
解决:在keras 2.1.5以上版本不支持leaky_relu的方法。可以在文件:tensorflow_backend.py
image.png
x = tf.maximum(alpha * x, x)