且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-03 10:51:40

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

I was able to find the answer looking at the Federate Learning repository on GitHub:

方法是使用我们想要作为输入的列的名称作为键,使orderedDict的"x"值成为orderedDict本身.

The way to do it is to make the 'x' value of the orderedDict an orderedDict itself using as keys the name of the columns we want as input.

此处提供了一个具体示例: https://github.com/tensorflow/federated/blob/3b5a551c46e7eab61e40c943390868fca6422e21/tensorflow_federated/python/learning/keras_utils_test.py#L283

A concrete example is given here: https://github.com/tensorflow/federated/blob/3b5a551c46e7eab61e40c943390868fca6422e21/tensorflow_federated/python/learning/keras_utils_test.py#L283

在其中定义输入规范的地方:

Where it define the input spec:

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


要在定义为以下模型的模型中使用:


To be used in the model defined as:

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