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