终于把tensorflow输入层和输出层搞懂了!fit函数与输入层,输出层,tf.keras.Model输入和输出的关系

结论

fit函数与输入层,输出层,tf.keras.Model输入和输出的关系

  • fit函数使用dataset格式,输入为字典格式,假设tf.keras.Model中输入和输出为字典格式(2.2或2.3),dataset的key必须和2.2或2.3中字典的key一致,否则报错
  • fit函数使用dataset格式,输入为仍然是字典格式,假设tf.keras.Model中输入和输出为list格式(2.1),dataset的key必须和2.1涉及到的**输入层和输出层(1.1和1.2)**的层名一致,否则报错

1. 定义模型输入和输出

1.1 定义模型输入层

continuous_input = {key: tf.keras.layers.Input(shape=(), name=key) for key in continuous_feature}
discrete_input = {key: tf.keras.layers.Input(shape=(), name=key) for key in discrete_feature}  

1.2 定义模型输出层

output_1 = tf.keras.layers.Dense(1, activation='sigmoid', name='is_click')(x)
output_2 = tf.keras.layers.Dense(1, activation='sigmoid', name='is_play')(x)
output_3 = tf.keras.layers.Dense(1, activation='sigmoid', name='is_pay')(x)

2. tf.keras.Model输入和输出

2.1 输入和输出为list格式

model_func = tf.keras.Model(inputs=list(continuous_input.values()) + list(discrete_input.values()),
                            outputs=[output_1, output_2,  output_3])

2.2 输出为dict格式

model_func = tf.keras.Model(inputs=list(continuous_input.values()) + list(discrete_input.values()),
                             outputs={'is_click': output_1, 'is_play': output_2, 'is_pay': output_3})

2.3 输入为dict格式

# 构造输入字典,也可以其他方式构造,此处只是为了说明,continuous_input为字典
continuous_input.update(discrete_input)
model_func = tf.keras.Model(inputs=continuous_input,
                            outputs=[output_1, output_2,  output_3])

3. fit函数中输入和输出-dataset(tfrecord格式)

3.1 dataset定义

def _parse_function(example_proto, feature_description):
    # Parse the input `tf.Example` proto using the dictionary above.
    data = tf.io.parse_single_example(example_proto, feature_description)
    is_click = data.pop('is_click')
    is_play = data.pop('is_play')
    is_pay = data.pop('is_pay')
    return data, {'is_click': is_click, 'is_play': is_play, 'is_pay': is_pay}

3.2 dataset示例-batch_size=1024

dataset为字典格式,请注意!

({'age': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.3448276 , 0.27586207, 0.31034482, ..., 0.37931034, 0.44827586,
       0.1724138 ], dtype=float32)>, 'first_class_id': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([ 4,  1,  1, ...,  4, 15,  1], dtype=int64)>, 'gender': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([2, 1, 2, ..., 2, 2, 1], dtype=int64)>, 'married': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([2, 2, 2, ..., 1, 1, 1], dtype=int64)>, 'province': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([22, 23, 25, ..., 14, 18, 20], dtype=int64)>, 'second_class_id': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([23, 53, 24, ..., 29, 11, 47], dtype=int64)>, 'tag_id': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([ 5, 58,  6, ..., 17, 76, 49], dtype=int64)>, 'target_item_id': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([ 5, 58,  6, ..., 17, 76, 49], dtype=int64)>, 'type': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([1, 4, 1, ..., 2, 1, 1], dtype=int64)>, 'user_click_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.5       , 0.5833333 , 1.        , ..., 0.41666666, 0.33333334,
       0.6666667 ], dtype=float32)>, 'user_click_video_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.5       , 0.5833333 , 0.8333333 , ..., 0.41666666, 0.33333334,
       0.6666667 ], dtype=float32)>, 'user_exp_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.47826087, 0.5797101 , 0.6231884 , ..., 0.3768116 , 0.6086956 ,
       0.26086956], dtype=float32)>, 'user_exp_video_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.61290324, 0.61290324, 0.58064514, ..., 0.32258064, 0.61290324,
       0.4516129 ], dtype=float32)>, 'user_id': <tf.Tensor: shape=(1024,), dtype=string, numpy=
array([b'ffb07508-9acc-4253-a1a0-e3e7fc6fad58',
       b'1ac654df-2b93-47b8-80ba-ca15642b5919',
       b'69daac99-ad14-4fc8-80f7-8c80cbc221b3', ...,
       b'97366ccc-b10d-47cb-9ad6-956c535ccf87',
       b'a7f43278-9e9b-4500-98b7-6d536d680ac1',
       b'297e2eec-a491-4aab-bc96-24f806751eb1'], dtype=object)>, 'user_name': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([234, 239, 285, ..., 753, 222, 563], dtype=int64)>, 'user_pay_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>, 'user_pay_video_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>, 'user_play_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>, 'user_play_video_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>, 'video_click_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.4722222, 0.5      , 0.6666667, ..., 0.8333333, 0.6666667,
       0.4722222], dtype=float32)>, 'video_click_user_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.4       , 0.51428574, 0.6857143 , ..., 0.7714286 , 0.71428573,
       0.4857143 ], dtype=float32)>, 'video_duration': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.8046324 , 0.19939578, 0.52970797, ..., 0.40584087, 0.5800604 ,
       0.15005036], dtype=float32)>, 'video_exp_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.7175141 , 0.4858757 , 0.7627119 , ..., 0.69491524, 0.4519774 ,
       0.6384181 ], dtype=float32)>, 'video_exp_user_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.85057473, 0.4827586 , 0.83908045, ..., 0.7011494 , 0.3908046 ,
       0.54022986], dtype=float32)>, 'video_id': <tf.Tensor: shape=(1024,), dtype=string, numpy=
array([b'HVgLcemGqaFAYgyEemtb', b'YNfPZPQwWggZRBkSsjMG',
       b'AvTonQbyvahPSCjsLvqN', ..., b'tvcZUdJBXAzJxsOZkXIc',
       b'HxnekvQEXBAgptCkNpXQ', b'RGumXWzhSqSoikFAZcWH'], dtype=object)>, 'video_name': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([15, 60,  1, ..., 25, 70, 47], dtype=int64)>, 'video_pay_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>, 'video_pay_user_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>, 'video_play_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.6666667 , 0.5555556 , 0.7777778 , ..., 0.6666667 , 0.44444445,
       0.33333334], dtype=float32)>, 'video_play_user_cnt': <tf.Tensor: shape=(1024,), dtype=float32, numpy=
array([0.6666667 , 0.5555556 , 0.7777778 , ..., 0.6666667 , 0.44444445,
       0.33333334], dtype=float32)>, 'work': <tf.Tensor: shape=(1024,), dtype=int64, numpy=array([1, 2, 2, ..., 2, 3, 3], dtype=int64)>}, {'is_click': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>, 'is_play': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>, 'is_pay': <tf.Tensor: shape=(1024,), dtype=float32, numpy=array([0., 0., 0., ..., 0., 0., 0.], dtype=float32)>})

4. fit函数与输入层,输出层,tf.keras.Model输入和输出的关系

  • fit函数使用dataset格式,输入为字典格式,假设tf.keras.Model中输入和输出为字典格式(2.2或2.3),dataset的key必须和2.2或2.3中字典的key一致,否则报错
  • fit函数使用dataset格式,输入为仍然是字典格式,假设tf.keras.Model中输入和输出为list格式(2.1),dataset的key必须和2.1涉及到的**输入层和输出层(1.1和1.2)**的层名一致,否则报错
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值