[Keras] Python core API

不错的公开课,只是用的PyTorch。

 

  1. Keras Prerequisites
  2. Change Keras Backend To Theano
  3. Preprocess Data For Training With Keras
  4. Create An Artificial Neural Network With Keras
  5. Train An Artificial Neural Network With Keras
  6. Build A Validation Set With Keras
  7. Make Predictions With An Artificial Neural Network Using Keras
  8. Create Confusion Matrix For Predictions From Keras Model
  9. Save And Load A Keras Model
  10. Image Preparation For CNN Image Classifier With Keras
  11. Create And Train A CNN Image Classifier With Keras
  12. Make Predictions With A Keras CNN Image Classifier
  13. Fine-Tune VGG16 Image Classifier With Keras | Part 1: Build
  14. Fine-Tune VGG16 Image Classifier With Keras | Part 2: Train
  15. Fine-Tune VGG16 Image Classifier With Keras | Part 3: Predict
  16. Data Augmentation With Keras
  17. Mapping Keras Labels To Image Classes
  18. Reproducible Results With Keras
  19. Initializing And Accessing Bias With Keras
  20. Learnable Parameters ("Trainable Params") In A Keras Model
  21. Learnable Parameters ("Trainable Params") In A Keras Convolutional Neural Network
  22. Deploy Keras Neural Network To Flask Web Service | Part 1 - Overview
  23. Deploy Keras Neural Network To Flask Web Service | Part 2 - Build Your First Flask App
  24. Deploy Keras Neural Network To Flask Web Service | Part 3 - Send And Receive Data With Flask
  25. Deploy Keras Neural Network To Flask Web Service | Part 4 - Build A Front End Web Application
  26. Deploy Keras Neural Network To Flask Web Service | Part 5 - Host VGG16 Model With Flask
  27. Deploy Keras Neural Network To Flask Web Service | Part 6 - Build Web App To Send Images To VGG16
  28. Deploy Keras Neural Network To Flask Web Service | Part 7 - Visualizations With D3, DC, Crossfilter
  29. Deploy Keras Neural Network To Flask Web Service | Part 8 - Access Model From Powershell, Curl
  30. Deploy Keras Neural Network To Flask Web Service | Part 9 - Information Privacy, Data Protection

 

之后的内容请见: [Web CV] Retrain MobileNet for TensorFlow.js

 

 

 

 

Keras 预测模型 


一、资源

Ref: keras-boston房价回归

Ref: [Feature] Preprocessing tutorial

Ref: Keras 101: A simple Neural Network for House Pricing regression [regression 挺好用]

 

 

二、Keras 基础学习

  • 基础知识

model = Sequential([
    Dense(units=16, input_shape=(1,), activation='relu'),
    Dense(units=32, activation='relu'),
    Dense(units=2, activation='softmax')
])

  

We can verify that the loaded model has the same architecture and weights as the saved model by calling summary() and get_weights() on the model.

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 16)                32        
_________________________________________________________________
dense_1 (Dense)              (None, 32)                544       
_________________________________________________________________
dense_2 (Dense)              (None, 2)                 66        
=================================================================
Total params: 642
Trainable params: 642
Non-trainable params: 0
_________________________________________________________________

 

 

三、Sequential 序贯模型例子

Ref: 深入学习Keras中Sequential模型及方法

在训练模型之前,我们需要配置学习过程,这是通过compile方法完成的,他接收三个参数:

    • 优化器 optimizer:它可以是现有优化器的字符串标识符,如 rmsprop 或 adagrad,也可以是 Optimizer 类的实例。详见:optimizers
    • 损失函数 loss:模型试图最小化的目标函数。它可以是现有损失函数的字符串标识符,如 categorical_crossentropy 或 mse,也可以是一个目标函数。详见:losses
    • 评估标准 metrics:对于任何分类问题,你都希望将其设置为 metrics = ['accuracy']。评估标准可以是现有的标准的字符串标识符,也可以是自定义的评估标准函数。

 

  • Dropout

Goto: http://localhost:8888/notebooks/10_dropout.ipynb

class CustomModel(keras.Model):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.input_layer = keras.layers.Flatten(input_shape=(28,28))
        self.hidden1 = keras.layers.Dense(200, activation='relu')
        self.hidden2 = keras.layers.Dense(100, activation='relu')
        self.hidden3 = keras.layers.Dense(60, activation='relu')
        self.output_layer = keras.layers.Dense(10, activation='softmax')
        self.dropout_layer = keras.layers.Dropout(rate=0.2)
    
    def call(self, input, training=None):
        input_layer = self.input_layer(input)
        input_layer = self.dropout_layer(input_layer)
hidden1
= self.hidden1(input_layer) hidden1 = self.dropout_layer(hidden1, training=training)
hidden2
= self.hidden2(hidden1) hidden2 = self.dropout_layer(hidden2, training=training)
hidden3
= self.hidden3(hidden2) hidden3 = self.dropout_layer(hidden3, training=training)
output_layer
= self.output_layer(hidden3) return output_layer

 

# dropout in hidden layers with weight constraint
def create_model():
    # create model
    model = Sequential()
    model.add(Dense(60, input_dim=60, activation='relu', kernel_constraint=maxnorm(3)))
    model.add(Dropout(0.2))
    model.add(Dense(30, activation='relu', kernel_constraint=maxnorm(3)))
    model.add(Dropout(0.2))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    sgd = SGD(lr=0.1, momentum=0.9)
    model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])
    return model

 

  • Batch Normalisation

Goto: http://localhost:8888/notebooks/04_batch_normalisation.ipynb

# Placing batch normalization layer before the activation layers
model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),
    keras.layers.Dense(300, use_bias=False),
    keras.layers.BatchNormalization(),
    keras.layers.Dense(200, use_bias=False),
    keras.layers.BatchNormalization(),
    keras.layers.Activation(keras.activations.relu),
    keras.layers.Dense(100, use_bias=False),
    keras.layers.BatchNormalization(),
    keras.layers.Activation(keras.activations.relu),
    keras.layers.Dense(10, activation=keras.activations.softmax)
])

 

 

/* implement */

 

posted @ 2020-01-13 11:57  郝壹贰叁  阅读(347)  评论(0)    收藏  举报