3

I am building an encoder decoder model, but when I am trying to pass the weights I am getting above mentioned error. Here is my code in brief:

classes = [item for sublist in train_summary.tolist() for item in sublist]
class_weights = class_weight.compute_class_weight('balanced', np.unique(classes), classes)
print(type(class_weights))
e_stopping = EarlyStopping(monitor='val_loss', patience=4, verbose=1, mode='min', restore_best_weights=True)
history = seq2seq_model.fit(x=[train_article, train_summary], y=np.expand_dims(train_target, -1),
                            batch_size=batch_size, epochs=epochs, validation_split=0.1,
                            callbacks=[e_stopping], class_weight=class_weights)

this is the error,

~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model)
   1114     strategy = ds_context.get_strategy()
   1115     dataset = self._adapter.get_dataset()
-> 1116     if class_weight:
   1117       dataset = dataset.map(_make_class_weight_map_fn(class_weight))
   1118     self._inferred_steps = self._infer_steps(steps_per_epoch, dataset)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

I think that data_adapter.py file should be modified, class_weights is a numpy array and to check if this array is empty or not we have to use np.any(array), but it seems that the python file in keras package is not doing this work.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
ronaldo_jr
  • 169
  • 9
  • if class_weights is an array of dim (train_size,) you have to pass it in sample_weight param and not in class_weight which accepts a dictionary object – Marco Cerliani Jul 15 '20 at 11:25
  • Does this answer your question? [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Trenton McKinney Aug 11 '20 at 18:05
  • It seems that Keras requires a dictionary of weights as suggested in the thread [here](https://stackoverflow.com/questions/61261907/on-colab-class-weight-is-causing-a-valueerror-the-truth-value-of-an-array-wit). – basicknowledge Jun 11 '21 at 13:09

0 Answers0