【问题标题】:What is the use of the ID field in the source code?源码中的id字段有什么用?
【发布时间】:2021-09-24 23:57:38
【问题描述】:

查看以下源代码:

import pandas as pd
from tensorflow.keras import layers, models

colors_df = pd.DataFrame(data=[[5,'yellow'],[1,'red'],[2,'blue'],[3,'green'],[4,'blue'],[7,'purple']], columns=['id', 'color'])

categorical_input = layers.Input(shape=(1,), dtype=tf.string)
one_hot_layer = OneHotEncodingLayer()
one_hot_layer.adapt(colors_df['color'].values)
encoded = one_hot_layer(categorical_input)

numeric_input = layers.Input(shape=(1,), dtype=tf.float32)

concat = layers.concatenate([numeric_input, encoded])

model = models.Model(inputs=[numeric_input, categorical_input], outputs=[concat])
predicted = model.predict([colors_df['id'], colors_df['color']])
print(predicted)
# [[5. 0. 1. 0. 0. 0.]
#  [1. 0. 0. 1. 0. 0.]
#  [2. 1. 0. 0. 0. 0.]
#  [3. 0. 0. 0. 0. 1.]
#  [4. 1. 0. 0. 0. 0.]
#  [7. 0. 0. 0. 1. 0.]]

在上面的文章中,他们写道:

这个简单的网络只接受一个分类输入,One Hot Encode 对其进行编码,然后将 One Hot Encoded 特征与数字输入特征连接起来。请注意,我在 DataFrame 中添加了一个数字 id 列,以说明如何从数字输入中拆分分类输入。

我没看懂。

为什么提供id 列以及这些 5 位单热代码?

它在整个应用程序中的用途是什么?

【问题讨论】:

    标签: python tensorflow keras neural-network one-hot-encoding


    【解决方案1】:

    这篇博文,简单地添加了Id,以保持输入字符串和一个热编码输出之间的连接,以便观众能够跟踪哪个输入字符串,转换为哪个热行。

    它只是将 ID 添加为输入,而在输出时没有进行任何处理以向您显示,例如id为5的yellow,转换为[0. 1. 0. 0. 0.]

    它对模型没有其他影响,即性能,但仅用于演示目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 2023-04-06
      • 2011-11-06
      • 2011-05-09
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多