(已解决)Tensorflow 联合 | tff.learning.from_keras_model() 具有具有 DenseFeature 层和多个输入的模型

Posted

技术标签:

【中文标题】(已解决)Tensorflow 联合 | tff.learning.from_keras_model() 具有具有 DenseFeature 层和多个输入的模型【英文标题】:(SOLVED) Tensorflow Federated | tff.learning.from_keras_model() with a model with DenseFeature layer and multiple inputs 【发布时间】:2020-08-10 23:32:25 【问题描述】:

我正在尝试联合一个具有多个输入的 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)

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()]
      )

调用时出错:

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.

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))])

【问题讨论】:

【参考方案1】:

我在 GitHub 上的 Federate Learning 存储库中找到了答案:

这样做的方法是使用我们想要作为输入的列的名称作为键,使 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()

要在模型中使用定义为:

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])

【讨论】:

实现您的答案时,我收到此错误“AttributeError: Tensor.op is meaningless when eager execution is enabled.”你有同样的问题吗?我用的是TF2.1 顺便说一句,我使用的是 TF2.2。如果您有同样的问题,请告诉我。如果是这样,如何解决?谢谢。

以上是关于(已解决)Tensorflow 联合 | tff.learning.from_keras_model() 具有具有 DenseFeature 层和多个输入的模型的主要内容,如果未能解决你的问题,请参考以下文章

使用 TensorFlow 实现联合损失的交集

TensorFlow_Fold计算图连接初探 Diamond计算图调试历程

用Rust语言编写的Weld可以让Spark和TensorFlow在速度上提升几个数量级!

20160118UILabel 设置.tff格式字体

谷歌开放TPU!与Tensorflow软硬联合,或将占独家地势

Tensorflow Serving 初探