TensorFlow 2.16+中keras.layers.DenseFeatures的替代方案是什么?
问题:TensorFlow 2.16.1及更高版本中
keras.layers.DenseFeatures的替代方案 在TensorFlow 2.16.1及更高版本中,keras.layers.DenseFeatures类已被移除。由于使用Python 3.11.7无法安装TensorFlow 2.15或更早版本,需要找到该类的替代方案来为DNN准备输入层。
原代码示例:
INPUT_COLS = [ "pickup_longitude", "pickup_latitude", "dropoff_longitude", "dropoff_latitude", "passenger_count", ] feature_columns = {colname: tf.feature_column.numeric_column(colname) for colname in INPUT_COLS} # Build a keras DNN model using Sequential API model = Sequential( [ keras.layers.DenseFeatures(feature_columns=feature_columns.values()), keras.layers.Dense(units=32, activation="relu", name="h1"), keras.layers.Dense(units=8, activation="relu", name="h2"), keras.layers.Dense(units=1, activation="linear", name="output"), ] )
替代方案
针对你的全数值型特征场景,以下是几种实用的替代方案:
方案1:直接使用Keras Input层(最简便)
既然所有输入都是数值型特征,无需通过特征列做额外处理,可以直接定义Input层接收每个特征,拼接后送入全连接层:
import tensorflow as tf from tensorflow.keras import Sequential, layers INPUT_COLS = [ "pickup_longitude", "pickup_latitude", "dropoff_longitude", "dropoff_latitude", "passenger_count", ] # 定义每个特征的Input层 inputs = {col: layers.Input(shape=(1,), name=col) for col in INPUT_COLS} # 拼接所有输入特征 x = layers.concatenate(list(inputs.values())) # 构建后续网络 x = layers.Dense(units=32, activation="relu", name="h1")(x) x = layers.Dense(units=8, activation="relu", name="h2")(x) output = layers.Dense(units=1, activation="linear", name="output")(x) # 构建模型 model = tf.keras.Model(inputs=inputs, outputs=output)
如果你的数据是以张量形式直接输入(而非字典形式),可简化为单个Input层:
model = Sequential( [ layers.Input(shape=(len(INPUT_COLS),)), # 输入形状为特征数量 layers.Dense(units=32, activation="relu", name="h1"), layers.Dense(units=8, activation="relu", name="h2"), layers.Dense(units=1, activation="linear", name="output"), ] )
方案2:使用FeatureSpace API(官方推荐的特征处理新方式)
TensorFlow推出的FeatureSpace是特征列的替代方案,更简洁且整合了预处理逻辑,适合复杂特征场景:
import tensorflow as tf from tensorflow.keras import Sequential, layers INPUT_COLS = [ "pickup_longitude", "pickup_latitude", "dropoff_longitude", "dropoff_latitude", "passenger_count", ] # 定义特征空间,所有特征均为数值型 feature_space = layers.experimental.preprocessing.FeatureSpace( features={col: "numeric" for col in INPUT_COLS}, output_mode="concat" # 拼接所有特征输出 ) # 适配输入数据(只需执行一次,用于确定特征形状) sample_input = {col: tf.random.uniform((1,)) for col in INPUT_COLS} feature_space.adapt(sample_input) # 构建模型 model = Sequential( [ feature_space, layers.Dense(units=32, activation="relu", name="h1"), layers.Dense(units=8, activation="relu", name="h2"), layers.Dense(units=1, activation="linear", name="output"), ] )
方案3:保留特征列定义,用Lambda层封装input_layer
如果你想保留原有的feature_columns定义,可以用tf.feature_column.input_layer结合Lambda层替代DenseFeatures:
import tensorflow as tf from tensorflow.keras import Sequential, layers INPUT_COLS = [ "pickup_longitude", "pickup_latitude", "dropoff_longitude", "dropoff_latitude", "passenger_count", ] feature_columns = {colname: tf.feature_column.numeric_column(colname) for colname in INPUT_COLS} feature_columns_list = list(feature_columns.values()) # 用Lambda层封装input_layer逻辑 def dense_features_wrapper(inputs): return tf.feature_column.input_layer(inputs, feature_columns_list) model = Sequential( [ layers.Lambda(dense_features_wrapper, name="dense_features"), layers.Dense(units=32, activation="relu", name="h1"), layers.Dense(units=8, activation="relu", name="h2"), layers.Dense(units=1, activation="linear", name="output"), ] ) # 注意:模型输入需为字典格式,需先适配输入形状 sample_input = {col: tf.random.uniform((1,)) for col in INPUT_COLS} model(sample_input)
内容的提问来源于stack exchange,提问作者shahramy
相关产品推荐
相关产品推荐

