Python ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()?
我正在构建一个编码器-解码器模型,但当我试图传递权重时,我得到了上述错误。以下是我的代码摘要:Python ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()?,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我正在构建一个编码器-解码器模型,但当我试图传递权重时,我得到了上述错误。以下是我的代码摘要: 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_stoppin
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)
这是一个错误
~\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().
我认为应该修改
data\u adapter.py
文件,class\u weights
是一个numpy数组,为了检查这个数组是否为空,我们必须使用np.any(array)
,但是keras
包中的python文件似乎没有做这项工作。如果class\u weights是一个dim数组(train\u size,)您必须在sample_weight参数中传递它,而不是在接受字典对象的class_weight中传递它。这是否回答了您的问题?似乎Keras需要一个如线程中所建议的权重字典。