1 入门

2 多个输入和输出

3 共享层

函数式模型有一个很好用的应用实例是:编写拥有多个输入和输出的模型。函数式模型使得在复杂网络中操作巨大的数据流变的简单。

我们实现下面这样的模型

from keras.layers import Input, Embedding, LSTM, Dense
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)

这里有 两个知识点

1、embedding层的使用。这里有个背景知识:我们输入的是100整数,每个整数都是0-1000的。代表的含义是:我们有个1000词的词典,输入的是100词的标题

然后经过embedding层,进行编码,输出512的向量

2、 LSTM(32)返回值是一个model,它可以向layer一样直接被调用

然后我们插入一个辅助层,它可以使得即使在模型的主损失值很大的时候 ,LSTM和Embedding层也可以得到平滑的训练

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

我们加入一个平滑的输入 ,把它和LSTM的输出连接在一起

auxiliary_input = Input(shape=(5,), name='aux_input')
x = keras.layers.concatenate([lstm_out, auxiliary_input]) # We stack a deep densely-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(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])

我们编译我们的模型,并且给平滑损失一个0.2的权重。可以用列表或者字典定义不同输出对应损失权重,如果对loss传入一个数 ,则损失权重会被用于全部的输出。

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

然后fit数据进行训练

model.fit([headline_data, additional_data], [labels, labels],
epochs=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},
epochs=50, batch_size=32)

keras Model 2 多输入和输出的更多相关文章

  1. 理解卷积神经网络中的输入与输出形状(Keras实现)

    即使我们从理论上理解了卷积神经网络,在实际进行将数据拟合到网络时,很多人仍然对其网络的输入和输出形状(shape)感到困惑.本文章将帮助你理解卷积神经网络的输入和输出形状. 让我们看看一个例子.CNN ...

  2. Keras Model Sequential模型接口

    Sequential 模型 API 在阅读这片文档前,请先阅读 Keras Sequential 模型指引. Sequential 模型方法 compile compile(optimizer, lo ...

  3. keras Model 3 共享的层

    1 入门 2 多个输入和输出 3 共享层 考虑这样的一个问题:我们要判断连个tweet是否来源于同一个人. 首先我们对两个tweet进行处理,然后将处理的结构拼接在一起,之后跟一个逻辑回归,输出这两条 ...

  4. keras Model 1 入门篇

    1 入门 2 多个输入和输出 3 共享层 最近在学习keras,它有一些实现好的特征提取的模型:resNet.vgg.而且是带权重的.用来做特诊提取比较方便 首先要知道keras有两种定义模型的方式: ...

  5. CString中Format函数与格式输入与输出

    CString中Format函数与格式输入与输出 Format是一个非经常常使用.却又似乎非常烦的方法,下面是它的完整概貌.以供大家查询之用:   格式化字符串forma("%d" ...

  6. MLPClassifier 隐藏层不包括输入和输出

    多层感知机(MLP)原理简介 多层感知机(MLP,Multilayer Perceptron)也叫人工神经网络(ANN,Artificial Neural Network),除了输入输出层,它中间可以 ...

  7. Pytorch从0开始实现YOLO V3指南 part5——设计输入和输出的流程

    本节翻译自:https://blog.paperspace.com/how-to-implement-a-yolo-v3-object-detector-from-scratch-in-pytorch ...

  8. 了解一下C++输入和输出的概念

    我们经常用到的输入和输出,都是以终端为对象的,即从键盘输入数据,运行结果输出到显示器屏幕上.从操作系统的角度看,每一个与主机相连的输入输出设备都被看作一个文件.除了以终端为对象进行输入和输出外,还经常 ...

  9. [总结] I/O输入,输出

    I/O输入,输出第一:先判断到底是输入还是输出,站在程序的立场第二:判断是传递字节,还是字符,决定管道粗细,字节流是最基本的数据输出管道.字符类型管道专门用来传送文本数据.Java流的四大父类:1.字 ...

随机推荐

  1. PHP message: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted 错误

    php运行一段时间后,部分页面打不开,查看nginx日志里面一直在报PHP message: PHP Fatal error:  Allowed memory size of 134217728 by ...

  2. rabbitmq rabbitmqadmin基本操作

    一.下载管理命令 http://192.168.56.12:15672/cli/rabbitmqadmin 二.上传到mq对应服务器并添加权限 chmod +x /usr/locat/sbin/rab ...

  3. python(列表及列表的相关操作、元组和range)

    1.什么是列表 列表是一个课表的数据类型 列表有[]来表示,每一项元素用逗号隔开,列表什么都能装.是能装对象的对象. 列表可以装大量数据. 2.列表的索引和切片 列表和字符串一样,也有索引和切片.只不 ...

  4. 用BCB 画 Code128 B模式条码

    //--------------------------------------------------------------------------- #include <vcl.h> ...

  5. redis五中数据类型

    MySql+Memcached架构的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的不断增加 ...

  6. rest_framework/api.html

    解决办法 在setting.py文件中添加 'rest_framework' 注册这个应用 INSTALLED_APPS = [ 'django.contrib.admin', 'django.con ...

  7. nginx location中root指令和alias指令的区别

    nginx location中root指令和alias指令 功能:将url映射为文件路径,以返回静态文件内容 差别:root会将完整的url映射进文件路径中 alias只会将location后的url ...

  8. 十七.rsync+SSH同步

    1. rsync同步操作 • 命令用法 – rsync [选项...] 源目录 目标目录   • 同步与复制的差异 – 复制:完全拷贝源到目标 – 同步:增量拷贝,只传输变化过的数据   • rsyn ...

  9. 三十九.NoSQL概述 部署Redis服务 、 部署LNMP+Redis

    1. 搭建Redis服务器 在主机 192.168.4.50 上安装并启用 redis 服务 设置变量test,值为123 查看变量test的值   1.1 搭建redis服务器 1.1.1 安装re ...

  10. 百度UEditor编辑器从word粘贴公式

    官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...