sersem1
sersem1

Reputation: 478

Tensor type error when federated learning

I tried to use tensorflow federated learning tool for my data. I have two datasets (dataset and dataset2) obtained from csv files where first 15 column are features and the last column is the label. I converted my pandas dataframe to tensorflow dataset. However, at the iterator, there is a strange type error. I am new in tensrflow and sending the code: Any help would be appreciated. Thanks in advance.

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

Error messages are as follows:

Traceback (most recent call last): File "/home/affectech/Desktop/Fed_son/Fed_son.py", line 117, in state, metrics = iterative_process.next(state, list) File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/utils/function_utils.py", line 666, in call arg = pack_args(self._type_signature.parameter, args, kwargs, context) File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/utils/function_utils.py", line 424, in pack_args context) File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/utils/function_utils.py", line 346, in pack_args_into_anonymous_tuple result_elements.append((name, context.ingest(arg_value, elem_type))) File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py", line 629, in ingest return to_representation_for_type(arg, type_spec, _handle_callable) File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py", line 241, in to_representation_for_type for v in value File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py", line 241, in for v in value File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py", line 200, in to_representation_for_type for v in value File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py", line 200, in for v in value File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py", line 192, in to_representation_for_type callable_handler) File "/home/affectech/Desktop/Fed_son/venv/lib/python3.6/site-packages/tensorflow_federated/python/core/impl/reference_executor.py", line 165, in to_representation_for_type 'the type spec {}.'.format(inferred_type_spec, type_spec)) TypeError: The tensor type float32[15] of the value representation does not match the type spec float32[?,15].

Process finished with exit code 1

Upvotes: 0

Views: 368

Answers (2)

Zachary Garrett
Zachary Garrett

Reputation: 2941

Looks like the call to iterative_process.next(state, list) is expecting the list of datasets (list) to be a list of batched datasets. The batch size can even be 1 if you prefer not to have multiple examples per batch.

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

Printing the types of the different objects is possible with print(iterative_process.next.type_signature) and tf.data.experimental.get_structure(dataset).

Upvotes: 2

hlidka
hlidka

Reputation: 2345

One problem I see is

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

You are losing your last feature column here, it should likely be

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

What makes this script confusing is the redefinition of train, X and y in the function body. This program should never get to the error you post, because X2 is not defined by the time it is used. I'd recommend not reusing variable names as a general rule, it will make the program easier for you to debug.

Then when you get a complain about the shape of something, print it (or a part of it) before the error happens, that will help diagnose what's wrong.

Upvotes: 0

Related Questions