预期可调用,发现不可调用 tensorflow_federated.python.learning.model_utils.EnhancedTrainableModel

Expected a callable, found non-callable tensorflow_federated.python.learning.model_utils.EnhancedTrainableModel

无法使用 TFF 的 build_federated_averaging_process()。按照 TFF 联合文档中的教程进行操作。

这是我的模型代码:

X_train = <valuex>
Y_train = <valuey>


def model_fn():

    model = tf.keras.models.Sequential([
        tf.keras.layers.Conv1D(32,dtype="float64",kernel_size=3,padding='same',activation=tf.nn.relu,input_shape=(X_train.shape[1], X_train.shape[2])),
        tf.keras.layers.MaxPooling1D(pool_size=3),
        tf.keras.layers.Conv1D(64,kernel_size=3,padding='same',activation=tf.nn.relu),
        tf.keras.layers.MaxPooling1D(pool_size=3),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128,activation=tf.nn.relu),
        tf.keras.layers.Dropout(0.45),
        tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
    ])

    model.compile(
      loss=tf.keras.losses.SparseCategoricalCrossentropy(),
      optimizer=tf.keras.optimizers.SGD(learning_rate=0.05),
      metrics=[tf.keras.metrics.Accuracy()])

    model.summary()

    return tff.learning.from_compiled_keras_model(model, sample_batch)


iterative_process = tff.learning.build_federated_averaging_process(model_fn())

我收到错误:

TypeError:应为可调用,但发现不可调用 tensorflow_federated.python.learning.model_utils.EnhancedTrainableModel。

build_federated_averaging_process 的参数应该是 model_fn 函数,而不是调用它的 return 值。

尝试更改此行:

iterative_process = tff.learning.build_federated_averaging_process(model_fn())

至:

iterative_process = tff.learning.build_federated_averaging_process(model_fn)