More complex reality than the theoretical

We imagine a practical matter, the spring, we buy clothes, at the same time, as clothing manufacturers have started releasing new clothes, if you as a garment manufacturer's technical adviser, you analyze what kind of clothes belonging to this year's fashion trends, how would you do?

First, as a technology otaku of you, I do not think you will pop elements have so much attention, not Paris Fashion Week to see what you can do is analysis and forecasting based on a variety of data. You might do some scan code to complete the questionnaire release some coupons may also go to the expense of some major fashion website articles critical analysis, may also go public this microblogging social platform Grilled number of fashion bloggers selfie in the store sharing, you can find ways to get all kinds of data, you have to do is analyze trends make good use of these data.

For this analysis predict the depth of learning is undoubtedly appropriate modeling tools, the problem is that both the survey data obtained for common layer stack model for training, there is such a critical analysis of this article text data, for Recurrent neural networks articles, pictures and various needs analysis, convolution model for neural networks, how can this do? According to our current knowledge, we can consider different network models were trained, they were weighted analysis, which of course is one way, but this right is too random, and also between the various data in isolation, and were treated not really a good way. How to do that?

They will unite for joint learning, roughly the way is this:

image

Of course, not only the joint model simultaneously receiving multiple input, output may be given at the same time, such as the popular price elements and, a plurality of outputs a plurality of inputs, it is not simply a variety of linear networks we mentioned earlier, It feels more like a graph, right, that is similar to the data structure in FIG. Figure than just linear list data structure is much more complex, as the depth study of this combination than a single model of a model that is much more complex.

Functional API

This is prior knowledge, so-called functional way, lucky Keras supported in such a way that the layer as function, function parameters and return values ​​are tensor, such functions can be combined to form a network model, see below the code examples:

def run():
    # 我们学过的用 Sequential 定义层
    seq_model = Sequential()
    seq_model.add(layers.Dense(32, activation='relu', input_shape=(64,)))
    seq_model.add(layers.Dense(32, activation='relu'))
    seq_model.add(layers.Dense(10, activation='softmax'))
    seq_model.summary()
​
    # 用函数的方式定义层
    input_tensor = Input(shape=(64,))
    x = layers.Dense(32, activation='relu')(input_tensor)
    x = layers.Dense(32, activation='relu')(x)
    output_tensor = layers.Dense(10, activation='softmax')(x)
    model = Model(input_tensor, output_tensor)
    model.summary()

These two definitions are equivalent network model of, Input tensor is used to define the raw data can be converted into tensor for network training, Model is used to find the path from input_tensor to output_tensor, and added to network.

Multi-input model

Suppose a model during a simplified version of the Turing test, this model to answer a question, the answer to this question in an article to be processed by this model, then the model is to have a network of multiple inputs, as follows :

image

Many of which knowledge we discussed before, such as the LSTM is circulating neural network for processing text. Concatenate here requires some explanation: Concatenate to connect, you can enter text questions and answers associated with them. Carried out summary view of network build in code looks like this:

image

#!/usr/bin/env python3
​
import time
​
import keras
​import numpy as np
from keras import Input
from keras import layers
from keras.models import Model
​
​
def run():
    text_vocabulary_size = 10000
    question_vocabulary_size = 10000
    answer_vocabulary_size = 500
    # 文本输入
    text_input = Input(shape=(None,), dtype='int32', name='text')
    # 将输入转换为向量
    embedded_text = layers.Embedding(text_vocabulary_size, 64)(text_input)
    # 将输入转换为单个向量
    encoded_text = layers.LSTM(32)(embedded_text)
    # 对问题进行如上处理
    question_input = Input(shape=(None,), dtype='int32', name='question')
    embedded_question = layers.Embedding(question_vocabulary_size, 32)(question_input)
    encoded_question = layers.LSTM(16)(embedded_question)
​
    # 将问题和文本联系起来
    concatenated = layers.concatenate([encoded_text, encoded_question], axis=-1)
    # 添加一个 softmax 分类器
    answer = layers.Dense(answer_vocabulary_size, activation='softmax')(concatenated)
    # 构建模型
    model = Model([text_input, question_input], answer)
    model.summary()
​
    model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'])
​
    # 生成虚拟训练数据,参考意义不大
    num_samples = 1000
    max_length = 100
    text = np.random.randint(1, text_vocabulary_size, size=(num_samples, max_length))
    question = np.random.randint(1, question_vocabulary_size, size=(num_samples, max_length))
    answers = np.random.randint(answer_vocabulary_size, size=(num_samples))
    answers = keras.utils.to_categorical(answers, answer_vocabulary_size)
    model.fit([text, question], answers, epochs=10, batch_size=128)
    model.fit({'text': text, 'question': question}, answers, epochs=10, batch_size=128)
​
​
if __name__ == "__main__":
    time_start = time.time()
    run()
    time_end = time.time()
    print('Time Used: ', time_end - time_start)

Multi-output model

To address this issue, actually used very frequently, such as user portrait, my father's Baidu home and my mom is not the same, although they did not fill out personal information in the settings, but still know what they are like, this is how do it?

image

Browse through news, general description of a user's age, income, etc., and then by the background Tag label on the news, the news could push more interested. Note that the gender and income classification is different, is a binary classification, a multi-classification, which also need to define a different loss functions. Want to accomplish this task, when the model is compiled, the definition is a good way. Note here that the imbalance will affect the contribution of losses training model, if a particular value losses particularly large, will cause the model will first be optimized and less consideration or hardly consider optimizing other tasks, this is not our want to see, so we can also define different final loss to the size of the contribution loss, weight loss were:

image

def run():
    vocabulary_size = 50000
    num_income_groups = 10
    posts_input = Input(shape=(None,), dtype='int32', name='posts')
    embedded_posts = layers.Embedding(256, vocabulary_size)(posts_input)
    x = layers.Conv1D(128, 5, activation='relu')(embedded_posts)
    x = layers.MaxPooling1D(5)(x)
    x = layers.Conv1D(256, 5, activation='relu')(x)
    x = layers.Conv1D(256, 5, activation='relu')(x)
    x = layers.MaxPooling1D(5)(x)
    x = layers.Conv1D(256, 5, activation='relu')(x)
    x = layers.Conv1D(256, 5, activation='relu')(x)
    x = layers.GlobalMaxPooling1D()(x)
    x = layers.Dense(128, activation='relu')(x)
    age_prediction = layers.Dense(1, name='age')(x)
    income_prediction = layers.Dense(num_income_groups, activation='softmax', name='income')(x)
    gender_prediction = layers.Dense(1, activation='sigmoid', name='gender')(x)
    model = Model(posts_input, [age_prediction, income_prediction, gender_prediction])
    model.summary()
    
    # 关键在这里
    model.compile(optimizer='rmsprop',
                  loss={'age': 'mse',
                        'income': 'categorical_crossentropy',
                        'gender': 'binary_crossentropy'},
                  loss_weights={'age': 0.25,
                                'income': 1.,
                                'gender': 10.})
    # model.fit(posts, [age_targets, income_targets, gender_targets], epochs=10, batch_size=64)

Figure structure

Of course, here refers to the structure of FIG directed acyclic graph, the acyclic between layers, the inner layer may have a ring (loop neural network), previously mentioned, as a function layer, tensor input is output, then it can be a combination of these vectors like building blocks illustrating the structure of complex networks and multiple-input multiple-output from the above example, you should also know roughly how the combination of it, here are two very well-known examples, as follows:

Inception Module

This helps learning module and wherein the spatial characteristics of each channel, which features more than two associative learning more efficient. This model is built keras.applications.inception_v3.InceptionV3, for processing data set ImageNet good efficiency and accuracy.

image

branch_a = layers.Conv2D(128, 1, activation='relu', strides=2)(x)
branch_b = layers.Conv2D(128, 1, activation='relu')(x)
branch_b = layers.Conv2D(128, 3, activation='relu', strides=2)(branch_b)
branch_c = layers.AveragePooling2D(3, strides=2)(x)
branch_c = layers.Conv2D(128, 3, activation='relu')(branch_c)
branch_d = layers.Conv2D(128, 1, activation='relu')(x)
branch_d = layers.Conv2D(128, 3, activation='relu')(branch_d)
branch_d = layers.Conv2D(128, 3, activation='relu', strides=2)(branch_d)
output = layers.concatenate([branch_a, branch_b, branch_c, branch_d], axis=-1)

Residual connection

This helps solve a gradient disappears module (more feedback signals to the base layer, the signal is very weak) and represents a bottleneck (certain parameters of the layer is too small, will result in permanent loss of certain features, the back layer can not obtain characteristic information ) issue specific approach is to let the previous output as input a layer behind a layer:

image

x = ...
y = layers.Conv2D(128, 3, activation='relu', padding='same')(x)
y = layers.Conv2D(128, 3, activation='relu', padding='same')(y)
y = layers.Conv2D(128, 3, activation='relu', padding='same')(y)
y = layers.add([y, x])

Sharing layer weights

Features function is multiplexed, can be called multiple times, where the functional programming, too, a layer is defined once, reuse many times, this is well understood:

# 定义一次
lstm = layers.LSTM(32)
left_input = Input(shape=(None, 128))
# 使用
left_output = lstm(left_input)
right_input = Input(shape=(None, 128))
# 使用
right_output = lstm(right_input)
merged = layers.concatenate([left_output, right_output], axis=-1)
predictions = layers.Dense(1, activation='sigmoid')(merged)
model = Model([left_input, right_input], predictions)

This is a multiplexing layer, expanding the scope of the model may be output vectors, so that the model also can be used as a layer, which is in front of the article predefined network echoes:

# Xception 是一个网络
xception_base = applications.Xception(weights=None, include_top=False)
left_input = Input(shape=(250, 250, 3))
​right_input = Input(shape=(250, 250, 3))
left_features = xception_base(left_input)
right_input = xception_base(right_input)
merged_features = layers.concatenate([left_features, right_input], axis=-1)

to sum up

Every article I have been controlled at about 1000 words, but this article is a bit Shoubu Zhu, the links between them are too close, no way, that even if a long article. Wherein the content is the same strain, the main thing to remember: Because of this functional API, so you can build complex network structure, rather than a simple network layer stack, based on this, can be constructed out of multiple input, multiple output and various complicated networks, and because of such functional characteristics, therefore with various multiplexing.

  • This article first number from the public: RAIS , welcome attention!

Guess you like

Origin www.cnblogs.com/renyuzhuo/p/12540081.html