1 / introduction

Keras is an open source artificial neural network library written in Python. Keras is a third-party library in the PYPI repository. Keras can be used as a high-level application interface for the design, debugging, evaluation, application and visualization of deep learning models for Tensorflow, Microsoft-CNTK and Theano. Keras is similar to other third-party libraries (pandas, Sklearn, etc.), except that Kears is a deep learning framework for deep learning, such as artificial neural networks. Keras is written in an object-oriented approach to code structure, fully modular and extensible. Keras supports mainstream algorithms in modern ARTIFICIAL intelligence, including feedforward and recursive neural networks, and can also participate in the construction of statistical learning models through encapsulation. In terms of hardware and development environment, Keras supports multi-GPU parallel computing under multi-operating systems, and can be converted into components under Tensorflow, Microsoft-CNTK and other systems according to background Settings. Keras has been supported by the Tensorflow team since 2017, with most of its components integrated into Tensorflow's Python API. With the release of Tensorflow 2.0.0 in 2018, Keras was officially established as a Tensorflow high-level API, known as TF-KerAS. In addition, Keras has been supported by CNTK 2.0 since July 2017. Keras supports Python versions 2.7-3.6, and requires at least one other optional preinstalled module from TensorFlow, Theano, and Microsoft-CNTK to be installed: H5py, used to save Keras model as HDF file cuDNN, used for GPU computation PyDot, used for model drawing Keras can be installed via PIPy, Anaconda, or download source code from GitHubCopy the code

2/ Get a quick start on Kears

The core data structure of Keras is the "model," which is a pattern that stacks layers of networks together. The main model in Keras is the Sequential model, which is a Sequential stack of network layers. You can also look at functional models to learn how to build more complex modelsCopy the code

<1> Build an empty model

   # Sequential model is as follows
   
   from keras.models import Sequential  # Models There are many models, and Sequential is used here
   houzhen_model = Sequential()         Build an empty model first
Copy the code

<2> Add a network layer to the empty model

    # Stack some network layers using the.add() method and add them to the empty model to form a real model
    
    from keras.layers import Dense, Activation  # 
    houzhen_model.add(Dense(units=64, input_dim=100))
    houzhen_model.add(Activation("relu"))  # activation function
    houzhen_model.add(Dense(units=10))
    houzhen_model.add(Activation("softmax"))  # activation function
Copy the code

<3> Compile the model

Once the model is built, we need to use the.compile() method to compile the model and specify the loss function and optimizer, or you can customize the loss function if you want. Houzhen_model.pile (Loss ="categorical_crossentropy", Optimizer =" SGD ", metrics=["accuracy"]) A core philosophy of Keras is simplicity and ease of use. At the same time, users are guaranteed absolute control over Keras. Users can customize their own models, network layers, and even modify the source code according to their own needs. from keras.optimizers import SGD houzhen_model.compile(loss="categorical_crossentropy", Optimizer = SGD (lr = 0.01, momentum = 0.9, nesterov = True))Copy the code

<4> Use training data to train the model

    After compiling the model, we conducted a certain number of iterations on the training data in batch to train the network
    houzhen_model.fit(x_train,y_train,epochs=5,batch_size=32)
    
    Of course, we can also manually send batch of data into the network for training. At this time, we need to use:
    houzhen_model.train_on_batch(x_batch, y_batch)
Copy the code

< 5 > assessment

And then we can use a line of code to evaluate our model to see if it meets our requirements and when we evaluate it, Loss_and_metrics = houzhen_model. Evaluate (X_test,y_test, batCH_size =128) or we can use our model to predict the new data:  classes = houzhen_model.predict(x_test, batch_size=128)Copy the code

<6> Some other points to note: