我正在使用Keras训练深度神经网络。我使用 train_on_batch 函数来训练我的模型。我的模型有两个输出。我打算做的是通过每个样本的每个特定值来修改每个样本的损耗。因此,由于Keras文档here
我需要为 sample_weight 参数分配两个不同的权重。 这是我的代码,每个批次都有四个训练示例:
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实现中的错误。我在网上几乎找不到任何与此工作相关的工作或问题。有什么想法吗?
答案 0 :(得分:0)
我有同样的问题,我不知道这是否是库中的错误,或者我们可能无法正确传递数组。我设法使它能够将列表强制转换为文件training_utils.py中的numpy数组,还传递了没有名称但按样本排序的数组。
答案 1 :(得分:0)
我用另一种方式解决了这个问题。
如果输出是Y1和Y2,并且它们的层名称分别是y1_layername
和y2_layername
,并假设您想将权重向量仅应用于y2(例如y2是长度为4的向量),您可以通过以下方式编写代码:
wights=[12,10,31,1];
mod_loss = mymodel.train_on_batch([X_train], [Y1, Y2],sample_weight={"y2_layername":wights})
我对其进行了测试,并且正常工作