Keras-多输入多输出【多任务】

2023-11-16

  1. 模型结果设计
    在这里插入图片描述
  2. 代码
from keras import Input, Model
from keras.layers import Dense, Concatenate
import numpy as np
from keras.utils import plot_model
from numpy import random as rd

samples_n = 3000
samples_dim_01 = 2
samples_dim_02 = 2
# 样本数据
x1 = rd.rand(samples_n, samples_dim_01)
x2 = rd.rand(samples_n, samples_dim_02)
y_1 = []
y_2 = []
y_3 = []
for x11, x22 in zip(x1, x2):
    y_1.append(np.sum(x11) + np.sum(x22))
    y_2.append(np.max([np.max(x11), np.max(x22)]))
    y_3.append(np.min([np.min(x11), np.min(x22)]))
y_1 = np.array(y_1)
y_1 = np.expand_dims(y_1, axis=1)
y_2 = np.array(y_2)
y_2 = np.expand_dims(y_2, axis=1)
y_3 = np.array(y_3)
y_3 = np.expand_dims(y_3, axis=1)

# 输入层
inputs_01 = Input((samples_dim_01,), name='input_1')
inputs_02 = Input((samples_dim_02,), name='input_2')
# 全连接层
dense_01 = Dense(units=3, name="dense_01", activation='softmax')(inputs_01)
dense_011 = Dense(units=3, name="dense_011", activation='softmax')(dense_01)
dense_02 = Dense(units=6, name="dense_02", activation='softmax')(inputs_02)
# 加入合并层
merge = Concatenate()([dense_011, dense_02])
# 分成两类输出 --- 输出01
output_01 = Dense(units=6, activation="relu", name='output01')(merge)
output_011 = Dense(units=1, activation=None, name='output011')(output_01)
# 分成两类输出 --- 输出02
output_02 = Dense(units=1, activation=None, name='output02')(merge)
# 分成两类输出 --- 输出03
output_03 = Dense(units=1, activation=None, name='output03')(merge)
# 构造一个新模型
model = Model(inputs=[inputs_01, inputs_02], outputs=[output_011,
                                                      output_02,
                                                      output_03
                                                      ])
# 显示模型情况
plot_model(model, show_shapes=True)
print(model.summary())
# # 编译
# model.compile(optimizer="adam", loss='mean_squared_error', loss_weights=[1,
#                                                                          0.8,
#                                                                          0.8
#                                                                          ])
# # 训练
# model.fit([x1, x2], [y_1,
#                      y_2,
#                      y_3
#                      ], epochs=50, batch_size=32, validation_split=0.1)

# 以下的方法可灵活设置
model.compile(optimizer='adam',
              loss={'output011': 'mean_squared_error',
                    'output02': 'mean_squared_error',
                    'output03': 'mean_squared_error'},
              loss_weights={'output011': 1,
                            'output02': 0.8,
                            'output03': 0.8})
model.fit({'input_1': x1,
           'input_2': x2},
          {'output011': y_1,
           'output02': y_2,
           'output03': y_3},
          epochs=50, batch_size=32, validation_split=0.1)

# 预测
test_x1 = rd.rand(1, 2)
test_x2 = rd.rand(1, 2)
test_y = model.predict(x=[test_x1, test_x2])
# 测试
print("测试结果:")
print("test_x1:", test_x1, "test_x2:", test_x2, "y:", test_y, np.sum(test_x1) + np.sum(test_x2))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Keras-多输入多输出【多任务】 的相关文章

随机推荐

  • R语言缺失值填补

    本文主要介绍如何利用R语言进行数值型缺失值的填补 主要使用zoo包中的na aggregate na approx na locf 函数进行缺失值的均值填补 线性插值填补以及邻近值填补 install packages zoo librar
  • 开源是物联网的驱动力量

    本文转载至 http www infoq com cn articles open source as a driver of internet of things utm campaign infoq content utm source
  • Shell脚本攻略:通配符、正则表达式

    目录 一 理论 1 通配符 2 正则表达式 二 实验 1 通配符 2 正则表达式 一 理论 1 通配符 1 概念 通配符只用于匹配文件名 目录名等 不能用于匹配文件内容 而且是已存在的文件或者目录 各个版本的shell都有通配符 这些通配符
  • 《Android 开发艺术探索》笔记2--IPC机制

    Android 开发艺术探索 笔记2 IPC机制 思维导图 Android IPC简介 Android中的多进程的模式 IPC基础概念 Serializable接口 Parcelable接口 Android的几种跨进程的方式 使用Bundl
  • having where 你真的了解了吗?

    where group by group by 字句 和 where条件语句结合在一起使用 当结合在一起时 where在前 group by 在后 即先对select xx from xx的记录集合用where进行筛选 然后再使用group
  • QT 三种关联信号和槽的办法

    1 手动关联 connect ui gt showChildButton QPushButton clicked this MyWidget showChildDialog 2 自动关联 右键单击按钮弹出菜单中选择 转到槽 void MyD
  • Basic Level 1052 卖个萌 (20分)

    题目 萌萌哒表情符号通常由 手 眼 口 三个主要部分组成 简单起见 我们假设一个表情符号是按下列格式输出的 左手 左眼 口 右眼 右手 现给出可选用的符号集合 请你按用户的要求输出表情 输入格式 输入首先在前三行顺序对应给出手 眼 口的可选
  • vue添加水印踩坑

    介绍 前景 app页面添加水印展示 技术实现 watermark dom 完整代码 vue watermark 实现效果 功能描述 添加 删除 更新水印 引入 方式一 推荐 方便拓展 在index html引入相关文件 方式二 npm包引入
  • java byte[] 学习总结

    最近在学习netty 突然发现自己对字符数组是那么的陌生 吓死宝宝了 然后各种学习 然后测试 终于会用一些了 下线的都是本人的学习笔记 byte表是字符 一个字节 8位 可以组成2 8 256中不同数字 byte存值范围 128 127 1
  • pytorch基本使用_02

    import numpy as np import torch 从numpy引入tensor a np array 2 3 3 print torch from numpy a tensor 2 0000 3 3000 dtype torc
  • java线上CPU100%如何排查

    定位耗费CPU的进程 top c 就可以显示进程列表 然后输入P 按照cpu使用率排序 你会看到类似下面的东西 2 定位耗费CPU的线程 top Hp 1500 就是输入那个进程id就好了 然后输入P 按照cpu使用率排序 你会看到类型下面
  • 单片机c语言数码管显示0到9,单片机如何让8个数码管同时流水显示0到9,大家帮我看看!...

    按你的要求修改如下 include reg52 h 此文件中定义了单片机的一些特殊功能寄存器 typedef unsigned int u16 对数据类型进行声明定义 typedef unsigned char u8 sbit LSA P2
  • Java 网络编程UDP协议之发送数据和接收数据的详解

    博主前些天发现了一个巨牛的人工智能学习网站 通俗易懂 风趣幽默 忍不住也分享一下给大家 点击跳转到网站 UDP协议 用户数据报协议 User Datagram Protocol UDP是无连接通信协议 即在数据传输时 数据的发送端和接收端不
  • 《信号与系统》4.10.2工频干扰的滤除

    平台 版本 Multisim14 1 参考书籍 信号与系统 4 10 2工频干扰的滤除 工程上 滤除工频干扰比较常用的电路是无源双T陷波滤波器 图示双T的无源陷波滤波器电路 陷波器是某一小频率范围内的带阻滤波器 陷波器的一个常见的应用是滤除
  • Seaborn入门详细教程

    作者 luanhz 来源 小数志 Seaborn入门详细教程 导读 今天我们来介绍 seaborn 这是一个基于matplotlib进行高级封装的可视化库 相比之下 绘制图表更为集成化 绘图风格具有更高的定制性 教程目录 01 初始seab
  • 一文带你了解序列化与反序列化基本原理与操作

    文章目录 一 什么是序列化与反序列化 二 为什么我们需要序列化与反序列化 三 步骤说明 四 注意说明 五 代码说明 六 序列化与反序列化原理 一 什么是序列化与反序列化 序列化是指将对象转换为字节序列的过程 以便于存储或传输 在序列化过程中
  • torch的交叉熵损失函数(cross_entropy)计算(含python代码)

    1 调用 首先 torch的交叉熵损失函数调用方式为 torch nn functional cross entropy input target weight None size average None ignore index 100
  • idea 配置log4j

    1 导入log4j jar包 放在lib目录下 右键jar包 createlibrary 好像是 还有一种方法 2 配置log4j properties文件 设置输出信息 DEBUG级别和ERROR级别 log4j rootLogger d
  • 【图像检测】基于计算机视觉和滤波实现热红外图像温度检测系统含Matlab源码

    1 简介 读入给定的热红外图像 完成彩色图像灰度化 对步骤1中的灰色图像加噪 并采用不同的方法对不同的加噪图像去噪 使得去噪效果更佳 基于灰度图像中标准温度对照带 提取图像中各像素的温度值 2 部分代码 function varargout
  • Keras-多输入多输出【多任务】

    模型结果设计 代码 from keras import Input Model from keras layers import Dense Concatenate import numpy as np from keras utils i