Keras API

    xiaoxiao2021-12-04  47

    Keras功能API是定义复杂模型的一个途径,例如多输出模型、有向非循环图,具有共性层的模型。 示例一: fully connected network Sequential 模型可能是一个更好的选择来实现一个这样的网络,但是从简单的开始更有帮助; 一个层实例是可调用的,它返回一个向量; 输入向量和输出向量能够用来定义一个模型; 如此的模型能够被训练,就像Keras模型似的;

    from keras.layers import Input, Dense from keras.models import Model # this returns a tensor inputs = Input(shape=(784,)) # a layer instance is callable on a tensor, and returns a tensor x = Dense(64, activation='relu')(inputs) x = Dense(64, activation='relu')(x) predictions = Dense(10, activation='softmax')(x) # this creates a model that includes # the Input layer and three Dense layers model = Model(input=inputs, output=predictions) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(data, labels) # starts training

    所有的模型都是可调用的,就像层实例一样; 关于功能API,重用被训练的模型是简单的:你能够把任何模型视为一个层,使用一个向量来调用它。值得注意的是调用一个模型不仅仅复用这个模型的架构,而且复用它的权值;

    x = Input(shape=(784,)) # this works, and returns the 10-way softmax we defined above. y = model(x)

    例如,这能够快速的创建能够处理输入序列的模型。能够使用一行语句把图像分类问题模型转化为音频分类模型

    from keras.layers import TimeDistributed # input tensor for sequences of 20 timesteps, # each containing a 784-dimensional vector input_sequences = Input(shape=(20, 784)) # this applies our previous model to every timestep in the input sequences. # the output of the previous model was a 10-way softmax, # so the output of the layer below will be a sequence of 20 vectors of size 10. processed_sequences = TimeDistributed(model)(input_sequences)

    多输入和多输出模型: 这是一个功能API的好的应用实例:模型带有多输出和多输出;功能API使处理大量的交织在一起的数据流更加简单; 让我们考虑下面的模型.我们试图预测在推特上的一个新闻标题能够获得多少次转发和点赞。模型输入主要是新闻标题本身,作为一个单词序列。为了得到更好的效果,我们的模型添加了辅助的输入,接收额外的数据,例如标题发布的时间;这个模型还是通过两个损失函数进行监督学习,在一个模型中更早的使用主损失函数对于深度学习模型还说是一个好的正则化机制; 我们的模型看起来像: 让我们使用功能API实现它: 主输入将要接受打字标题,例如一个整数序列(每一个整数编码一个单词)。这个整数介于1和10000之间(单词表大小为10000),序列长度是100个单词

    from keras.layers import Input, Embedding, LSTM, Dense, merge from keras.models import Model # headline input: meant to receive sequences of 100 integers, between 1 and 10000. # note that we can name any layer by passing it a "name" argument. main_input = Input(shape=(100,), dtype='int32', name='main_input') # this embedding layer will encode the input sequence # into a sequence of dense 512-dimensional vectors. x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input) # a LSTM will transform the vector sequence into a single vector, # containing information about the entire sequence lstm_out = LSTM(32)(x)

    下面插入辅助损失。使得即使模型中主要损失非常高,LSTM和嵌入层平稳也能的训练

    auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)

    这时,我们通过把辅助输入数据与LSTM输出级联进行辅助输入数据的添加;

    auxiliary_input = Input(shape=(5,), name='aux_input') x = merge([lstm_out, auxiliary_input], mode='concat') # we stack a deep fully-connected network on top x = Dense(64, activation='relu')(x) x = Dense(64, activation='relu')(x) x = Dense(64, activation='relu')(x) # and finally we add the main logistic regression layer main_output = Dense(1, activation='sigmoid', name='main_output')(x)

    我们定义一个模型,带有两个输入和两个输出

    model = Model(input=[main_input, auxiliary_input], output=[main_output, auxiliary_output])

    我们编译这个模型,给辅助输出设置一个权重为0.2.为了指出不同输出的loss_weights和loss,可以使用一个列表或字典;这里传递了一个单一的损失参数,所以所有的暑促使用相同的损失函数;

    model.compile(optimizer='rmsprop', loss='binary_crossentropy', loss_weights=[1., 0.2])

    我们能够通过传递输入序列和输出序列来训练模型:

    model.fit([headline_data, additional_data], [labels, labels], nb_epoch=50, batch_size=32)

    由于我们的输入和输出被命名,我们还可以编译模型通过:

    model.compile(optimizer='rmsprop', loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'}, loss_weights={'main_output': 1., 'aux_output': 0.2}) # and trained it via: model.fit({'main_input': headline_data, 'aux_input': additional_data}, {'main_output': labels, 'aux_output': labels}, nb_epoch=50, batch_size=32)
    转载请注明原文地址: https://ju.6miu.com/read-680311.html

    最新回复(0)