联邦学习时的张量类型错误

Tensor type error when federated learning

我尝试对我的数据使用 tensorflow 联邦学习工具。我有两个从 csv 文件中获取的数据集(数据集和数据集 2),其中前 15 列是特征,最后一列是标签。我将 pandas 数据帧转换为张量流数据集。但是,在迭代器处,出现了一个奇怪的类型错误。我是 tensrflow 的新手并发送代码:任何帮助将不胜感激。提前致谢。

from __future__ import absolute_import, division, print_function
from sklearn.preprocessing import MinMaxScaler
from keras.models import Model

import collections
import numpy as np
import tensorflow as tf
import tensorflow_federated as tff
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
import pandas as pd


X_train= pd.read_csv('./daily_frames_HR.csv')



values = X_train.values

values = values.astype('float32')
# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
# frame as supervised learning

train = values[:, :]
# split into input and outputs
X, y = train[:, :-2], train[:, -1]


def create_compiled_keras_model():
  model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(
      12, activation=tf.nn.softmax, kernel_initializer='zeros', input_dim=15)])

  return model

def model_fn():
  keras_model = create_compiled_keras_model()

  keras_model.compile(loss='binary_crossentropy', optimizer='sgd', metrics= 
   ['SparseCategoricalAccuracy'])
  X_train = pd.read_csv('./daily_frames_HR.csv')

  values = X_train.values

  values = values.astype('float32')
  # normalize features
  scaler = MinMaxScaler(feature_range=(0, 1))
  scaled = scaler.fit_transform(values)
  # frame as supervised learning

  train = values[:, :]
  # split into input and outputs
  X, y = train[:, :-2], train[:, -1]

  sample_batch = collections.OrderedDict([('x', X), ('y', y)])
  return tff.learning.from_compiled_keras_model(keras_model, sample_batch)


  iterative_process = tff.learning.build_federated_averaging_process(model_fn)

  state = iterative_process.initialize()

  X2_train= pd.read_csv('./lab_frames_HR.csv')
  values2 = X2_train.values

  values2 = values2.astype('float32')
  # normalize features
  scaler = MinMaxScaler(feature_range=(0, 1))
  scaled = scaler.fit_transform(values2)
  # frame as supervised learning

  train2 = values2[:, :]
  # split into input and outputs
  X2, y2 = train2[:, :-2], train2[:, -1]

  X2=pd.DataFrame(X2)
  y2=pd.DataFrame(y2)

  X=pd.DataFrame(X)
  y=pd.DataFrame(y)


dataset = tf.data.Dataset.from_tensor_slices((X2.values, y2.values))

dataset2= tf.data.Dataset.from_tensor_slices((X.values, y.values))


list = [dataset, dataset2]

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

报错信息如下:

回溯(最后一次调用): 文件“/home/affectech/Desktop/Fed_son/Fed_son.py”,第 117 行,位于 状态,指标 = iterative_process.next(状态,列表) 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/utils/function_utils.py”,第 666 行,在 call 中 arg = pack_args(self._type_signature.parameter, args, kwargs, 上下文) 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/utils/function_utils.py”,第 424 行,在 pack_args 中 语境) 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/utils/function_utils.py”,第 346 行,在 pack_args_into_anonymous_tuple 中 result_elements.append((姓名, context.ingest(arg_value, elem_type))) 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py”,第 629 行,正在摄取 return to_representation_for_type(arg, type_spec, _handle_callable) 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py”,第 241 行,在 to_representation_for_type 中 对于价值 v 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py”,第 241 行,位于 对于价值 v 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py”,第 200 行,在 to_representation_for_type 中 对于价值 v 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py”,第 200 行,位于 对于价值 v 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py”,第 192 行,在 to_representation_for_type callable_handler) 文件“/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py”,第 165 行,在 to_representation_for_type 中 'the type spec {}.'.format(推断_type_spec, type_spec)) TypeError:值表示的张量类型 float32[15] 与类型规范 float32[?,15].

不匹配

进程已完成,退出代码为 1

我看到的一个问题是

X, y = train[:, :-2], train[:, -1]

您在这里丢失了最后一个功能栏,它应该是

X, y = train[:, :-1], train[:, -1]

这个脚本让人迷惑的地方在于函数体中trainXy的重新定义。这个程序永远不会得到你 post 的错误,因为 X2 在它被使用的时候还没有定义。我建议不要重复使用变量名作为一般规则,这将使程序更容易调试。

然后当你收到关于某物形状的抱怨时,在错误发生之前打印它(或它的一部分),这将有助于诊断问题所在。

看起来对 iterative_process.next(state, list) 的调用期望数据集列表 (list) 是 batched 数据集的列表。如果您不希望每个批次有多个示例,批次大小甚至可以是 1

batch_size = 1
list = [dataset.batch(batch_size), dataset2.batch(batch_size)]

使用 print(iterative_process.next.type_signature)tf.data.experimental.get_structure(dataset) 可以打印不同对象的类型。