【问题标题】:'Error While Encoding with Hub.KerasLayer' while using TFF使用 TFF 时出现“使用 Hub.KerasLayer 编码时出错”
【发布时间】:2020-07-08 03:01:59
【问题描述】:

训练使用 hub.KerasLayer 的联合模型时出现错误。下面给出了错误和堆栈跟踪的详细信息。完整代码可从 gist https://gist.github.com/aksingh2411/60796ee58c88e0c3f074c8909b17b5a1 获得。在这方面的帮助和建议将不胜感激。谢谢。

from tensorflow import keras

def create_keras_model():
 encoder = hub.load("https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1")
 return tf.keras.models.Sequential([
  hub.KerasLayer(encoder, input_shape=[],dtype=tf.string,trainable=True),
  keras.layers.Dense(32, activation='relu'),
  keras.layers.Dense(16, activation='relu'),
  keras.layers.Dense(1, activation='sigmoid'),
])

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=preprocessed_example_dataset.element_spec,
  loss=tf.keras.losses.BinaryCrossentropy(),
  metrics=[tf.keras.metrics.Accuracy()])

# Building the Federated Averaging Process
iterative_process = tff.learning.build_federated_averaging_process(
 model_fn,
 client_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=0.02),
 server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0))

str(iterative_process.initialize.type_signature)
state = iterative_process.initialize()

state, metrics = iterative_process.next(state, federated_train_data)
print('round  1, metrics={}'.format(metrics))

UnimplementedError                        Traceback (most recent call last)
<ipython-input-80-39d62fa827ea> in <module>()
----> 1 state, metrics = iterative_process.next(state, federated_train_data)
  2 print('round  1, metrics={}'.format(metrics))

119 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in 
quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
 58     ctx.ensure_initialized()
 59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
 61   except core._NotOkStatusException as e:
 62     if name is not None:

UnimplementedError:    Cast string to float is not supported
 [[{{node StatefulPartitionedCall_1/StatefulPartitionedCall/Cast_1}}]]
 [[StatefulPartitionedCall_1]]
 [[import/StatefulPartitionedCall_3/ReduceDataset]] [Op:__inference_wrapped_function_65986]

Function call stack:
wrapped_function -> wrapped_function -> wrapped_function

【问题讨论】:

  • 在没有 TensorFlow Federated 包装的情况下,模型是否可以工作?要对此进行测试,您可以尝试使用 create_keras_model() 返回的 Keras 模型,使用 compile() 添加损失函数和优化器,然后在模型上调用 .fit() 并传入数据集(例如 federated_train_data[0] 可能有效) .这可能有助于提供更简单的重现。
  • 感谢您的建议。我用 federated_train_data[0] 尝试了 model.fit。它给出了错误 AttributeError: 'Sequential' object has no attribute '_v1_compile_was_call'。
  • 同时,我发现标签作为 tf.string 而不是 tf.int32 传递。解决这个问题后,我的 tff 训练正在运行。

标签: tensorflow tensorflow2.0 tf.keras tensorflow-federated


【解决方案1】:

问题现已解决。抛出错误是因为“标签”作为 tf.string 而不是 tf.int32 传递。显式转换解决了这个问题。

【讨论】:

    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多