Tensorflow多输入模型构建以及Dataset数据构建

本文介绍如何在Tensorflow中构建多输入模型,适用于处理如问答模型和时间序列预测等问题。文章详细讲解了如何结合不同类型的输入数据,并通过`tf.data.Dataset.zip`合并数据。同时,讨论了在使用MultiWorkerMirroredStrategy进行分布式训练时,为何需要使用Dataset格式。文章还简要提及了多机多卡训练的优势和注意事项。
摘要由CSDN通过智能技术生成

多输入模型多适用于问答模型或者对于时间序列模型来说有部分特征是针对样本个体而固定的,不随时间变换而发生改变的情况下。
对于模型的输入数据格式来说,有很多种方式,普通的全部数据导入,或者写成生成器等,可以逐批读取数据然后训练模型,但是当你使用tensorflow内置分布式训练,也就是多机多卡模卡MultiWorkerMirroredStrategy的时候,就必须使用Dataset格式。
因为Dataset会自动根据batch_size分发数据进行迭代训练。

如果对MultiWorkerMirroredStrategy以及MirroredStrateg两种训练模型感兴趣的可以去看看我的另外一篇文章。https://blog.csdn.net/qq_35869630/article/details/106313745

一、多输入模型

import tensorflow as tf
from tensorflow.keras.layers import LSTM, Dense, concatenate,Bidirectional
from tensorflow.keras import Input,Model

def build_and_compile_model():
  inputA = Input(shape=(7, 22))
  inputB = Input(shape=(3,))
  x = LSTM(128, return_sequences=False, activation="relu")(inputA)
  x = Dense(128, activation="relu")(x)
  x = Dense(64, activation="relu")(x)
  x = Model(inputs=inputA, outputs=x)
  y = Dense(32, activation="relu")(inputB)
  y = Dense(16, activation="relu")(y)
  y = Model(inputs=inputB, outputs=y)
  combined = concatenate([x.output, y.output], axis=-1)
  z = Dense(32, activation="relu")(combined)
  z = Dense(1, activation="sigmoid")(z)
  model = Model(inputs=[x.input, y.input], outputs=z)
  model.compile(loss='binary_crossentropy', optimizer='rmsprop',
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值