Ale*_*ex 5 python keras tensorflow tensorflow2.0 tensorflow-federated
我正在尝试联合一个具有多个输入的 keras 模型。其中一些输入是分类的,其中一些是数字的,因此我有一些 DenseFeature 层来嵌入值。
问题是,使用 tff.learning.from_keras_model()
预期作为 input_spec 的字典只有 2 个元素 (x,y),但我有多个输入,然后我必须在模型中区分这些输入,以便使用 feature_columns 函数和 DenseFeature 层正确执行嵌入。
如果模型只接受“x”作为输入而没有正确的列名称,我该如何处理单个特征列?
这是代码和错误:
def create_keras_model():
l = tf.keras.layers
# handling numerical columns
for header in numerical_column_names:
feature_columns.append(feature_column.numeric_column(header))
# handling the categorical feature
pickup = feature_column.categorical_column_with_vocabulary_list(
'pickup_location_id', [i for i in range(number_of_locations)])
#pickup_one_hot = feature_column.indicator_column(pickup)
#feature_columns.append(pickup_one_hot)
pickup_embedding = feature_column.embedding_column(pickup, dimension=64)
#feature_columns.append(pickup_embedding)
feature_inputs = {
'pickup_week_day_sin': tf.keras.Input((1,), name='pickup_week_day_sin'),
'pickup_week_day_cos': tf.keras.Input((1,), name='pickup_week_day_cos'),
'pickup_hour_sin': tf.keras.Input((1,), name='pickup_hour_sin'),
'pickup_hour_cos': tf.keras.Input((1,), name='pickup_hour_cos'),
'pickup_month_sin': tf.keras.Input((1,), name='pickup_month_sin'),
'pickup_month_cos': tf.keras.Input((1,), name='pickup_month_cos'),
}
numerical_features = l.DenseFeatures(feature_columns)(feature_inputs)#{'x': a}
location_input = {
'pickup_location_id': tf.keras.Input((1,), dtype=tf.dtypes.int32, name='pickup_location_id'),
}
categorical_features = l.DenseFeatures(pickup_embedding)(location_input)#{'x': a}
#i = l.Input(shape=(64+6,))
#embedded_lookup_feature = tf.feature_column.numeric_column('x', shape=(784))
conca = l.Concatenate()([categorical_features, numerical_features])
dense = l.Dense(128, activation='relu')(conca)
dense_1 = l.Dense(128, activation='relu')(dense)
dense_2 = layers.Dense(number_of_locations, kernel_initializer='zeros')(dense_1)
output = l.Softmax()(dense_2)
inputs = list(feature_inputs.values()) + list(location_input.values())
return tf.keras.Model(inputs=inputs, outputs=output)
Run Code Online (Sandbox Code Playgroud)
input_spec = preprocessed_example_dataset.element_spec
def model_fn():
# We _must_ create a new model here, and _not_ capture it from an external
# scope. TFF will call this within different graph contexts.
keras_model = create_keras_model()
return tff.learning.from_keras_model(
keras_model,
input_spec=input_spec,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()]
)
Run Code Online (Sandbox Code Playgroud)
调用时出错:
ValueError: The top-level structure in `dummy_batch` or `input_spec` must contain exactly two elements, as it must contain type information for both inputs to and predictions from the model.
Run Code Online (Sandbox Code Playgroud)
preprocessed_example_dataset.element_spec:
OrderedDict([('pickup_location_id',
TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('pickup_hour_sin',
TensorSpec(shape=(None,), dtype=tf.float32, name=None)),
('pickup_hour_cos',
TensorSpec(shape=(None,), dtype=tf.float32, name=None)),
('pickup_week_day_sin',
TensorSpec(shape=(None,), dtype=tf.float32, name=None)),
('pickup_week_day_cos',
TensorSpec(shape=(None,), dtype=tf.float32, name=None)),
('pickup_month_sin',
TensorSpec(shape=(None,), dtype=tf.float32, name=None)),
('pickup_month_cos',
TensorSpec(shape=(None,), dtype=tf.float32, name=None)),
('y', TensorSpec(shape=(None,), dtype=tf.int32, name=None))])
Run Code Online (Sandbox Code Playgroud)
Ale*_*ex 3
我在 GitHub 上的联邦学习存储库中找到了答案:
实现此目的的方法是使用我们想要作为输入的列的名称作为键,使orderedDict 的“x”值成为orderedDict 本身。
这里给出了一个具体的例子: https: //github.com/tensorflow/federated/blob/3b5a551c46e7eab61e40c943390868fca6422e21/tensorflow_federated/python/learning/keras_utils_test.py#L283
它定义输入规范的地方:
input_spec = collections.OrderedDict(
x=collections.OrderedDict(
a=tf.TensorSpec(shape=[None, 1], dtype=tf.float32),
b=tf.TensorSpec(shape=[1, 1], dtype=tf.float32)),
y=tf.TensorSpec(shape=[None, 1], dtype=tf.float32))
model = model_examples.build_multiple_inputs_keras_model()
Run Code Online (Sandbox Code Playgroud)
在定义为的模型中使用:
def build_multiple_inputs_keras_model():
"""Builds a test model with two inputs."""
l = tf.keras.layers
a = l.Input((1,), name='a')
b = l.Input((1,), name='b')
# Each input has a single, independent dense layer, which are combined into
# a final dense layer.
output = l.Dense(1)(
l.concatenate([
l.Dense(1)(a),
l.Dense(1)(b),
]))
return tf.keras.Model(inputs={'a': a, 'b': b}, outputs=[output])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1386 次 |
最近记录: |