简体   繁体   中英

Problem in sample_weight in Keras when trying train_on_batch for a model with multiple outputs

I am using Keras to train a deep neural network. I use train_on_batch function to train my model. My model has two outputs. What I intend to do, is to modify the loss for each of the samples by some specific value per each sample. So due to Keras documentation here

I need to have two different weights assigned to the sample_weight argument. Here is what my code looks like, wherein each batch, I have four training example:

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]])

I use sample_weight to weight only the first output and not the second output. when I run the code, I get this error:

  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'

It gave me the idea if I change the assigned value to sample_weight to a numpy array, the problem will be solved. So I changed the code to this one:

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]]))

And I have got this error:

  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.        ]]

I was a bit confused, I am not sure if it is a bug inside Keras implementation or not. I could barely find any work or issue related to this one on the web. Any thoughts?

I have solved the issue in another way. If the outputs are Y1 and Y2, and their layer names are y1_layername and y2_layername and imagine you want to apply a weight vector, only to y2 ( where y2 is a vector of length 4 for example), You can write your code in this way :

wights=[12,10,31,1];  
mod_loss = mymodel.train_on_batch([X_train], [Y1, Y2],sample_weight={"y2_layername":wights})

I tested it, and it worked properly

I have the same problem, I don't understand if it's a bug in the library or we might not pass the array properly. I've managed to make it work casting the list to numpy array in the file training_utils.py, passing also the arrays without names but sorted as the samples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM