python - 如何使用 Tensorflow Federated 中的多个功能构建模型?
问题描述
尝试为多个特征输入(即特征 ag)和一个标签 h 创建 OrderedDict 时,我遇到以下代码和问题。
def preprocess(dataset):
def batch_format_fn(element):
return collections.OrderedDict(
x=collections.OrderedDict(
a=tf.TensorSpec(shape=[None,], dtype=tf.int32),
b=tf.TensorSpec(shape=[None,], dtype=tf.int32),
c=tf.TensorSpec(shape=[None,], dtype=tf.int32),
d=tf.TensorSpec(shape=[None,], dtype=tf.int32),
e=tf.TensorSpec(shape=[None,], dtype=tf.int32),
f=tf.TensorSpec(shape=[None,], dtype=tf.int32),
g=tf.TensorSpec(shape=[None,], dtype=tf.int32)),
y=tf.TensorSpec(shape=[None,], dtype=tf.int32))
return dataset.map(batch_format_fn).prefetch(PREFETCH_BUFFER)
preprocessed_sample_dataset = preprocess(example_dataset)
def create_keras_model():
model = Sequential([
feature_layer,
Dense(64, activation='relu'),
Dense(64, activation='relu'),
Dense(3, activation='softmax') #classification 3 outputs
])
return model
def model_fn():
keras_model = create_keras_model()
return tff.learning.from_keras_model(
keras_model,
input_spec=preprocessed_sample_dataset.element_spec,
loss=losses.SparseCategoricalCrossentropy(),
metrics=[metrics.SparseCategoricalAccuracy()])
执行时显示如下错误input_spec=preprocessed_sample_dataset.element_spec
:
TypeError: Unsupported return value from function passed to Dataset.map(): OrderedDict([('x', OrderedDict([('a', TensorSpec(shape=(None,), dtype=tf.int32, name=None)), ('b', TensorSpec(shape=(None,), dtype=tf.int32, name=None)), ('c', TensorSpec(shape=(None,), dtype=tf.int32, name=None)), ('d', TensorSpec(shape=(None,), dtype=tf.int32, name=None)), ('e', TensorSpec(shape=(None,), dtype=tf.int32, name=None)), ('f', TensorSpec(shape=(None,), dtype=tf.int32, name=None)), ('g', TensorSpec(shape=(None,), dtype=tf.int32, name=None))])), ('y', TensorSpec(shape=(None,), dtype=tf.int32, name=None))]).
我已阅读此替代解决方案,但尚不清楚如何在我的情况下实施它。因此,如何为 TFF 中的多个特征正确分配有序字典?
当前的example_dataset.element_spec如下:
OrderedDict([
('a', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('b', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('c', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('d', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('e', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('f', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('g', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('y', TensorSpec(shape=(None,), dtype=tf.int32, name=None))])
我希望 element_spec 变成这样:
OrderedDict([('x', OrderedDict([
('a', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('b', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('c', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('d', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('e', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('f', TensorSpec(shape=(None,), dtype=tf.int32, name=None)),
('g', TensorSpec(shape=(None,), dtype=tf.int32, name=None))])),
('y', TensorSpec(shape=(None,), dtype=tf.int32, name=None))])
如何使用 batch_format_fn 将 element_spec 作为后者?
解决方案
batch_format_fn
当前返回张量类型的结构;tf.data.Dataset.map
期望接收张量结构作为函数的返回值。
我们应该更新batch_format_fn
以重新格式化它的element
参数并返回它。让我们尝试一下:
def batch_format_fn(element):
feature_dict = collections.OrderedDict(
a=element['a'],
b=element['b'],
c=element['c'],
d=element['d'],
e=element['e'],
f=element['f'],
g=element['g'],
)
return collections.OrderedDict(x=feature_dict, y=element['y'])
并保持其他一切不变。
推荐阅读
- python - 如何将存储在偶数索引处的字符串加倍和存储在 3 的倍数的索引处的三重字符串
- r - r 包的 travis-ci 构建
- python - Python的冒号是赋值吗?这一步Python做了什么?
- html - 在导航链接旁边放置文本
- javascript - 有什么方法可以避免相机通过在 ThreeJs 中加载到我们场景中的 GLtf 对象
- javascript - 无法使用跳过等待激活新的服务人员
- python - Kivy 上未解决的导入
- javascript - 在javascript中使用异步时,then函数内部的函数参数的目的是什么?
- r - 在 ggplot 中向 barplot 添加带线的点(类似于 excel 的带标记线图)
- python - 你如何在 Python 中用对数编写这个程序?