Keras CNN Convolution Neural Network (III.)

Source: Internet
Author: User
Tags random seed keras

To import the desired lib:

Import NumPy as NP from
keras.datasets import mnist to
keras.utils import np_utils from
keras.models Import Sequential
from keras.optimizers import Adam
from keras.layers import dense,activation,convolution2d, Maxpooling2d,flatten,dropout

To set a random seed:

Np.random.seed (1337) #设置随机种子

Load data:

(X_train,y_train), (x_test,y_test) =mnist.load_data () #加载数据

Data preprocessing:

#数据预处理
X_train=x_train.reshape ( -1,28,28,1) #  (n, Monochrome Road, 28,28)
x_test=x_test.reshape ( -1,28,28,1) 
Y_ Train=np_utils.to_categorical (y_train,num_classes=10)
y_test=np_utils.to_categorical (y_test,num_classes=10 )

Standardization:

# convert X_train, x_test data format to float32
X_train = X_train.astype (' float32 ')
x_test = X_test.astype (' float32 ')
# Normalized
x_train/= 255
x_test/= 255


To create a model:

#建模
Model =sequential ()

Build Layer:

#卷积 1
-Layer Model.add (convolution2d (
    filters=32, # 32 filters-) generates 32 depth
    kernel_size=3, # Filter window size (3,3)
    strides= 1,
    padding= ' same ', # Filter Mode
#     activation= ' Relu ',    # Activate function (which can be written here)
    input_shape= (28,28,1),  # input shape is picture shape
)
Model.add (Activation (' Relu ')) #激活函数
#池化 1-Layer
model.add (maxpooling2d (
    pool_size= (2, 2),  # Pool Scan window size
    strides=2,   # Scan window Step 2 for each move 
    padding= ' same ' #默认 is valid
))
#卷积 2
-layer Model.add (convolution2d (
    filters=64, # 64 filters-) generates 64 depth
    kernel_size=3, # Filter window size (3,3)
    padding= ' Same ', # Filter mode
    activation= ' Relu ',    # Activate function (can be written here)
)
#池化 2-layer
Model.add (maxpooling2d (
    pool_size= (2, 2),  # Pool Scan window size
    strides=2,
    padding= ' same ', #默认 is Valid)
)
Model.add (Dropout (0.25)) #防止过拟合
#扁平化
Model.add (Flatten ())
#全连接
Model.add (dense (128))
Model.add (Activation (' Relu '))
Model.add (Dropout (0.5))

Model.add (Dense
) Model.add (Activation (' Softmax '))
#自己定义 optimizer
Adam=adam (lr=1e-4)

# Output model parameter information
model.summary ()


Build the Model:

#搭建模型
model.compile (optimizer= ' Adam ', loss= ' categorical_crossentropy ', metrics=[' accuracy '])

Training:

Model.fit (x_train,y_train,epochs=1,batch_size=32)
#评估
loss,accuracy=model.evaluate (X_test,Y_test)
Print (loss)
print (accuracy)


Results:

10000/10000 [==============================]-eta:0s
0.0454092171811
0.9847




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.