Keras Study (I.) _keras

Source: Internet
Author: User
Tags theano keras

I see that Keras is good, based on Python, the background is based on Theano or TensorFlow. Installation

Environment: ubuntu14.04
First, install the Python environment, Theano, and Keras

sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ the git
sudo pip libopenblas-dev All Theano
sudo pip install Keras
Data and Code Preparation

According to the blog, download Mnist.zip data and GitHub on the cnn.py and data.py, extract to the same directory:

Note: Keras default background is TensorFlow, changed to Theano, modify ~/.keras/keras.json file can be:

Image_dim_ordering and backend two parameters are modified (the default is TF and TensorFlow). Training CNN Model

Execute the Python cnn.py file to perform training:

Ran 10 epoch, accuracy rate nearly 99%.
Here's what CNN looks like with the code:

############### #开始建立CNN模型 ############### #生成一个model model = sequential () #第一个卷积层, 4 convolution cores, each convolution nucleus size 5*5.
1 represents the channel of the imported picture, and the grayscale image is 1 channels. #border_mode可以是valid或者full, specific look here Description: http://deeplearning.net/software/theano/library/tensor/nnet/conv.html# theano.tensor.nnet.conv.conv2d #激活函数用tanh #你还可以在model. Add (Activation (' tanh ') with dropout tips: model.add (Dropout (0.5 ) Model.add (convolution2d (4, 5, 5, border_mode= ' valid ', input_shape= (1,28,28))) Model.add (Activation (' Tanh ')) # The second convolution layer, 8 convolution cores, each volume kernel size 3*3. 4 represents the number of feature graphs entered, equal to the number of convolution cores on the previous layer #激活函数用tanh #采用maxpooling, poolsize (2,2) Model.add (convolution2d (8, 3, 3, border_mode= ' valid ') ) Model.add (Activation (' Tanh ')) Model.add (Maxpooling2d (pool_size= (2, 2)) #第三个卷积层, 16 convolution cores, each volume kernel size 3*3 #激活函数用tanh #
Using Maxpooling,poolsize (2,2) Model.add (convolution2d (3, 3, border_mode= ' valid ')) Model.add (Activation (' tanh '))
Model.add (Maxpooling2d (pool_size= (2, 2)) #全连接层, the first layer of the output of the two-dimensional feature map flatten as one-dimensional. #Dense就是隐藏层. 16 is the number of feature graphs that are printed on the previous layer. 4 is calculated according to each convolution layer: (28-5+1) gets 24, (24-3+1)/2 gets 11, (11-3+1)/2 gets 4 #全连接有128个神经元节点, beginningThe initialization mode is normal model.add (Flatten ()) Model.add (dense (128, init= ' normal ')) Model.add (Activation (' Tanh ')) #Softmax分类, Output is 10 category Model.add (dense (init= ' normal ')) Model.add (Activation (' Softmax ')) ############# #开始训练模型 ############## #
Using SGD + momentum #model. Compile parameters loss is the loss function (target function) SGD = SGD (lr=0.05, decay=1e-6, momentum=0.9, Nesterov=true) Model.compile (loss= ' categorical_crossentropy ', OPTIMIZER=SGD) #调用fit方法, is a training process.
The number of epoch for training is set to 10,batch_size 100. #数据经过随机打乱shuffle =true. verbose=1, the information output in the training process, 0, 1, 23 ways can be irrelevant.
Show_accuracy=true, every epoch is output accuracy during training.
#validation_split = 0.2, 20% of the data as a validation set.

 Model.fit (data, label, BATCH_SIZE=100, nb_epoch=10,shuffle=true,verbose=1,validation_split=0.2)

: http://blog.csdn.net/zhoubl668/article/details/45559955
Keras Chinese document: http://keras-cn.readthedocs.io/en/latest/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.