我正在使用Keras来训练深度神经网络。我使用train_on_batch函数来训练我的模型。我的模型有两个输出。我的意图是通过每个样本的特定值来修改每个样本的损失。由于Keras文档here所述,我需要将sample_weight参数分配为两个不同的权重。以下是我的代码,其中每个batch包含四个训练样例:
我遇到了这个错误:
wights=[12,10,31,1];
mod_loss = mymodel.train_on_batch([X_train], [Y1, Y2],sample_weight=[wights,[1.0,1.0,1.0,1.0]])
我使用 sample_weight 来仅对第一个输出进行加权而不是第二个输出。当我运行代码时,出现了以下错误:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1211, in train_on_batch
class_weight=class_weight)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 801, in _standardize_user_data
feed_sample_weight_modes)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 799, in <listcomp>
for (ref, sw, cw, mode) in
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_utils.py", line 470, in standardize_weights
if sample_weight is not None and len(sample_weight.shape) != 1:
AttributeError: 'list' object has no attribute 'shape'
这给了我一个想法,如果我将分配给 sample_weight 的值更改为numpy数组,则问题将得到解决。因此,我将代码更改为以下内容:
wights=[12,10,31,1];
mod_loss = mymodel.train_on_batch([X_train], [Y1, Y2],sample_weight=numpy.array([wights,[1.0,1.0,1.0,1.0]]))
我遇到了这个错误:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1211, in train_on_batch
class_weight=class_weight)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 794, in _standardize_user_data
sample_weight, feed_output_names)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_utils.py", line 200, in standardize_sample_weights
'sample_weight')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_utils.py", line 188, in standardize_sample_or_class_weights
str(x_weight))
TypeError: The model has multiple outputs, so `sample_weight` should be either a list or a dict. Provided `sample_weight` type not understood: [[12.0 10.0 31.0 1.0]
[ 1. 1. 1. 1. ]]
我有点困惑,不确定这是否是Keras实现中的一个bug。我几乎找不到任何与此相关的工作或问题。你有什么想法吗?