--------------------------------------------------------------------------- ValueError…
- User
ValueError Traceback (most recent call last) Cell In [20], line 10 8 import time 9 start = time.time() ---> 10 state = trainer.initialize() 11 #state.model.assign_weights_to(model_fn) 12 train_hist = []
File ~\AppData\Roaming\Python\Python38\site-packages\tensorflow_federated\python\core\impl\utils\function_utils.py:521, in ConcreteFunction.call(self, *args, **kwargs) 519 context = self._context_stack.current 520 arg = pack_args(self._type_signature.parameter, args, kwargs, context) --> 521 return context.invoke(self, arg)
File ~\AppData\Roaming\Python\Python38\site-packages\tensorflow_federated\python\core\impl\tensorflow_context\tensorflow_computation_context.py:50, in TensorFlowComputationContext.invoke(self, comp, arg) 47 py_typecheck.check_type(comp, computation_base.Computation) 48 computation_proto = computation_impl.ComputationImpl.get_proto(comp) 49 init_op, result = ( ---> 50 tensorflow_deserialization.deserialize_and_call_tf_computation( 51 computation_proto, arg, self._graph)) 52 if init_op: 53 self._init_ops.append(init_op)
File ~\AppData\Roaming\Python\Python38\site-packages\tensorflow_federated\python\core\impl\tensorflow_deserialization.py:66, in deserialize_and_call_tf_computation(computation_proto, arg, graph) 64 computation_oneof = computation_proto.WhichOneof('computation') 65 if computation_oneof != 'tensorflow': ---> 66 raise ValueError( 67 'Expected a TensorFlow computation, got {}.'.format(computation_oneof)) 68 py_typecheck.check_type(graph, tf.Graph) 69 with graph.as_default():
ValueError: Expected a TensorFlow computation, got lambda.
- Assistant
It seems the TensorFlow Federated model is expecting a TensorFlow computation but it got a lambda function instead. It's difficult to pinpoint the exact issue without the complete codebase, but here are a couple of possibilities:
-
Make sure you are defining your model using the
tff.learning.from_keras_modelortff.learning.Modelcorrectly and have the necessary input signatures defined for your input tensors. -
Double-check if your model has any custom lambda layers or functions that might be causing this issue, and try to replace them with built-in TensorFlow layers or operations.
-
If you have a custom training loop, make sure that you are correctly using
tff.tf_computationto create TensorFlow computations within the federated environment.
Without more context about the code causing this exception, it's difficult to give more specific guidance. If you can provide more details, I'd be happy to help further!
-
- User
- Assistant
It seems like you are taking the model's weights and updating them with an optimizer defined in the
cso_flfunction. However, it's not clear where the federated learning is taking place within this code since TensorFlow Federated is not being used. If you're looking to do federated learning with the current code, you can follow this outline:- Split the dataset into smaller subsets that represent data from different clients. You appear to be doing this in the commented '#client_data' line.
- User
ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.
- Assistant
The error you're encountering is related to scikit-learn's
train_test_splitfunction. You may have accidentally provided an empty or small dataset, so when the function tries to split the dataset into train and test sets, it encounters an empty train set, which is not allowed.To help you better, it would be useful to see the part of your code where you're calling
train_test_split. However, I can give you some general suggestions:-
Make sure that the dataset you're trying to split is not empty or very small in size. You can check the number of samples in your dataset before performing the split.
-
Check your
test_sizeparameter. If you're getting an error withtest_size=0.2, you may try reducing it to a smaller value like0.1or even0.05, depending on the size of your dataset. -
If you're using a small dataset, you can increase the number of samples using data augmentation techniques if it's suitable for your problem and dataset.
-
- User
def make_tf_dataset(dataframe, batch_size=None): #data = data.fillna(-999) #data.fillna(data.mean(), inplace=True) dataframe = dataframe.dropna() X = dataframe.drop(['TransactionID','TransactionDT','TransactionAmt','ProductCD','card4','card6','P_emaildomain','R_emaildomain','M4', 'isFraud'], axis=1) y = dataframe['isFraud']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=40,shuffle=True, stratify=y) scaler = StandardScaler() X_train = scaler.fit_transform(X_train) sm = SMOTE(random_state=42) X_train, y_train = sm.fit_resample(X_train, y_train) #X_train =tf.convert_to_tensor(X_train, dtype=tf.float64) #y_train = tf.convert_to_tensor(y_train, dtype=tf.int64) #X_train = X_train.astype('float32') #balanced_df=X_train.assign(Class=y_train) #y = balanced_df.pop('Class') # Dataset creation dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train)) dataset = dataset.shuffle(2048, seed=SEED) if batch_size: dataset = dataset.batch(batch_size) return datasetEPOCHS = 100 BATCH_SIZE = 1024 SEED = 1337 train_data, val_data = [], []
for client_data in [alice_df, bob_df]: train_df, val_df = train_test_split(client_data, test_size=0.1, random_state=SEED)
# TF Datasets train_data.append(make_tf_dataset(train_df, batch_size=BATCH_SIZE)) val_data.append(make_tf_dataset(val_df, batch_size=128)) - Assistant
I see that you're trying to make TensorFlow datasets from given dataframes using
make_tf_dataset. Since you're not receiving a clear error message pointing out which data split is causing the issue, I'd suggest adding more print statements to check the number of samples in the intermediate dataframes and batches.Please add print statements at various steps to help identify the issue in your code as follows:
def make_tf_dataset(dataframe, batch_size=None): dataframe = dataframe.dropna() # Print the dataframe length after dropping NA values print(f"Length after dropping NA values: {len(dataframe)}") X = dataframe.drop(['TransactionID','TransactionDT','TransactionAmt','ProductCD','card4','card6','P_emaildomain','R_emaildomain','M4', 'isFraud'], axis=1) y = dataframe['isFraud'] # Print the lengths of X and y before splitting print(f"Length of X and y before splitting: {len(X)}, {len(y)}") X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=40, shuffle=True, stratify=y) scaler = StandardScaler() X_train = scaler.fit_transform(X_train) sm = SMOTE(random_state=42) X_train, y_train = sm.fit_resample(X_train, y_train) # Print the lengths of X_train and y_train after resampling print(f"Length of X_train and y_train after resampling: {len(X_train)}, {len(y_train)}") # Dataset creation dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train)) dataset = dataset.shuffle(2048, seed=SEED) if batch_size: dataset = dataset.batch(batch_size) # Print the dataset length print(f"Length of dataset: {len(list(dataset))}") return dataset