1. Introduction
1, window10 python environment Anaconda installation
2, keras installation
3. tensorflow installation
4, eclipse python development plug-in PyDev installation, configuration
5. Keras Convolutional Neural Network for Handwritten Digit Recognition
2. Environment installation
1、Anaconda
Anaconda refers to an open source Python distribution that includes more than 180 scientific packages such as conda, Python, and their dependencies. Very functional.
Download address: https://www.anaconda.com/distribution/#download-section
Download and install
After the installation is successful, configure the environment variables, open cmd, and use the command python --version to view the version number
Among them, environment variables are very important, and the environment variables of ssl must be configured, otherwise an exception will occur when using pip (pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. )
Special note: After installation, some packages may be faulty, for example: matplotlib and Pillow cannot be used normally, and the following exception will occur
File "E:\Program Files\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 32, in <module>
import matplotlib.colorbar
File "E:\Program Files\Anaconda3\lib\site-packages\matplotlib\colorbar.py", line 32, in <module>
import matplotlib.contour as contour
File "E:\Program Files\Anaconda3\lib\site-packages\matplotlib\contour.py", line 18, in <module>
import matplotlib.font_manager as font_manager
File "E:\Program Files\Anaconda3\lib\site-packages\matplotlib\font_manager.py", line 48, in <module>
from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
ImportError: DLL load failed: 找不到指定的模块。
At this time, follow the abnormal code line to see which libraries are not normally dependent on, and then reinstall them. The following operations can be analogized to perform
pip uninstall matplotlib
pip install matplotlib
pip uninstall Pillow
pip install Pillow
2, keras installation
Install using the following command:
pip install hard
3. Install tensorflow
Since keras itself does not provide running, it depends on other computing engines, such as TensorFlow, Theano, CNTK, etc. Here, we install TensorFlow as the backend of keras.
Install with the following command:
Tensorflow has cpu version and gpu version
cpu版:pip install --user --ignore-installed --upgrade tensorflow
gpu版:pip install --user --ignore-installed --upgrade tensorflow-gpu
Here, the cpu version is installed, and the --user parameter is due to a permission problem, so add
4, eclipse PyDev plugin installation
(1), open eclipse, Help->Eclipse Marketplace, search and select PyDev, install
(2), configure the eclipse development environment
window->preferences->PyDev->interpreters->Python Interpreter
After the configuration is complete, click OK, and you can develop python programs on eclipse, which is very convenient like developing java.
3. Keras convolutional neural network for mnist handwritten digit recognition
1. Load the dataset first
(x_train, y_train), (x_test, y_test) = mnist.load_data()
Description: If mnist.load_data() is not available locally, it will download the dataset
Print the shape of x_train to see what the dataset has
print(x_train.shape)
2. Show the pictures of the samples
#将画板分为1行2列,本幅图位于第一个位置
plt.subplot(1,2,1)
# 打印两个个样本
plt.imshow(x_train[5], cmap='gray')
plt.subplot(1,2,2)
plt.imshow(x_train[10], cmap='gray')
plt.show()
3. Stack a CNN network
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=(img_x, img_y, 1)))
model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Like the previous stacking of CNNs with dl4j, "Interesting Convolutional Neural Networks"
4. Complete code
from keras.datasets import mnist
from keras.layers import Conv2D, MaxPool2D
from keras.layers import Dense, Flatten
from keras.models import Sequential
from keras.utils import to_categorical
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape)
#将画板分为1行2列,本幅图位于第一个位置
plt.subplot(1,2,1)
# 打印两个个样本
plt.imshow(x_train[5], cmap='gray')
plt.subplot(1,2,2)
plt.imshow(x_train[10], cmap='gray')
plt.show()
img_x, img_y = 28, 28
x_train = x_train.reshape(x_train.shape[0], img_x, img_y, 1)
x_test = x_test.reshape(x_test.shape[0], img_x, img_y, 1)
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=(img_x, img_y, 1)))
model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=128, epochs=1)
score = model.evaluate(x_test, y_test)
print('accuracy', score[1])
5. Running results
eclipse run as Python Run, the result of iterating 1 batch is as follows. (Of course, you can also debug python programs in eclipse)
Happiness comes from sharing.
This blog is original by the author, please indicate the source for reprinting