Python 如何在Tensorflow模型中添加一个热编码层?

Python 如何在Tensorflow模型中添加一个热编码层?,python,tensorflow,keras,tensorflow2.0,Python,Tensorflow,Keras,Tensorflow2.0,我想在Tensorflow 2模型中添加一个热编码层。这就是我到目前为止所做的: import pandas as pd import tensorflow as tf # import CSV file to pandas DataFrame called df # set categorical (CAT_COLUMNS) and numerical (NUM_COLUMNS) features feature_cols = [] # Create IndicatorColumn fo

我想在Tensorflow 2模型中添加一个热编码层。这就是我到目前为止所做的:

import pandas as pd
import tensorflow as tf

# import CSV file to pandas DataFrame called df
# set categorical (CAT_COLUMNS) and numerical (NUM_COLUMNS) features

feature_cols = []

# Create IndicatorColumn for categorical features
for feature in CAT_COLUMNS:
  vocab = df[feature].unique()
  feature_cols.append(tf.feature_column.indicator_column(
      tf.feature_column.categorical_column_with_vocabulary_list(feature, vocab)))

# Create NumericColumn for numerical features
for feature in NUM_COLUMNS:
  feature_cols.append(tf.feature_column.numeric_column(feature, dtype=tf.int32))

print(feature_cols)
我应该如何在Tensorflow模型中使用
feature\u cols
,以便只对分类特征应用一个热编码

model = tf.keras.Sequential([
                             tf.keras.layers.Dense(units=1, input_shape=[len(df.columns)]),
                             tf.keras.layers.Dense(units=128, activation=tf.nn.relu),
                             tf.keras.layers.Dense(units=1, activation=tf.nn.softmax)
                            ])

我认为您可以提供分类和数字特征作为单独的输入,并使用
tf.keras.layers.Concatenate
将它们组合起来。

使用
tf.keras.layers.experimental.preprocessing


阅读示例。

你在谷歌上找到了什么?@furas:什么都没有。一个典型的建议是使用一个热编码
sklearn
。。。但是我想为Tensorflow模型提供一个原始输入,而不是用
sklearn
对其进行预处理。你能举个例子吗?CachuanZhao的答案似乎是正确的。在linked()页面上,是一个使用连接层合并所有输入功能的示例。