Keras How to construct a simple CNN Network

Source: Internet
Author: User
Tags keras

1. Import various modules

The basic form is:

Import Module Name

Import a module from a file

2. Import data (take two types of classification issues as an example, Numclass = 2)

Training Set Data

As you can see, data is a four-dimensional ndarray

Tags for training sets

3. Convert the imported data to the data format I keras acceptable

The label format required for Keras should be binary class matrices, so you need to convert the input label data to take advantage of the Keras enhanced to_categorical function

Label = np_utils.to_categorical (Label, Numclass

The label now changes to the following form

(Note: Pycharm cannot display so much data, so only 1000 data is shown below, in fact the data set shown in this example has 1223 data)

  

4. Building a CNN Model

As an example of the CNN network shown

#生成一个modelmodel = Sequential () #layer1-conv1model.add (convolution2d (3, 3, border_mode= ' valid ', input_shape= Data.shape[-3:]) Model.add (Activation (' Tanh ')) #tanh # Layer2-conv2model.add (convolution2d (+ 3, 3, border_mode= ') Valid ') Model.add (Activation (' Tanh ')) #tanh # Layer3-conv3model.add (convolution2d (3, 3, border_mode= ' valid ')) Model.add (Activation (' Tanh ')) #tanh # Layer4model.add (Flatten ()) Model.add (dense (+, init= ' normal ')) Model.add ( Activation (' Tanh ')) #tanh # layer5-fully Connectmodel.add (Dense (numclass, init= ' normal ')) Model.add (Activation (' Softmax '))

#  
SGD = SGD (l2=0.1,lr=0.001, decay=1e-6, momentum=0.9, Nesterov=true)
Model.compile (loss= ' categorical_crossentropy ', optimizer=sgd,class_mode= "categorical")

5. Start Training model

Using Model.train_on_batch or Model.fit

  

Keras How to construct a simple CNN Network

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.