title: Keras sample_weight array error
body: <p>I'm working on a brain lesion segmentation problem and I'm trying to implement a Unet with code inspired by: <a href="https://github.com/jocicmarko/ultrasound-nerve-segmentation" rel="nofollow noreferrer">https://github.com/jocicmarko/ultrasound-nerve-segmentation</a></p>
  
  <p>One of the issues I'm trying to overcome is class balance (lots more non-lesion voxels rather than lesion voxels). I tried using class_balance but that didn't work so now I'm trying to use sample_weight and that's also giving me all sorts of errors.</p>
  
  <p>First thing I tried was to set <code>sample_weight_mode</code> to <code>temporal</code> and feed in a weight matrix of the same shape as my target data:</p>
  
  <pre><code>target_data.shape -&gt; (n_samples,512 rows/pixels, 512 cols/pixels, 1 channel)
  Weight_map.shape -&gt; (n_samples,512 rows/pixels, 512 cols/pixels, 1 channel)
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>_ValueError: Found a sample_weight array with shape (100, 512, 512, 1). In order to use timestep-wise sample weighting, you should pass a 2D sample_weight array.*</p>
  </blockquote>
  
  <p>Second thing I tried was to flatten the sample array so it would be of shape:</p>
  
  <pre><code>Weight_map.shape -&gt; (n_samples,512x512x1).
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>ValueError: Found a sample_weight array with shape (100, 262144) for an input with shape (100, 512, 512, 1). sample_weight cannot be broadcast.*</p>
  </blockquote>
  
  <p>Next I tried following the advice of uschmidt83 (<a href="https://github.com/fchollet/keras/issues/3653" rel="nofollow noreferrer">here</a>) and flattening the output of my model along with the corresponding target data.</p>
  
  <pre><code>last_layer = keras.layers.Flatten()(second_last_layer)
  
  target_data.shape -&gt; (n_samples,512x512x1).
  Weight_map.shape -&gt; (n_samples,512x512x1).
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>ValueError: Found a sample_weight array for an input with shape (100, 262144). Timestep-wise sample weighting (use of sample_weight_mode="temporal") is restricted to outputs that are at least 3D, i.e. that have a time dimension.*</p>
  </blockquote>
  
  <p>Oddly enough, even if I set <code>sample_weight=None</code> I still get the same error as right above.</p>
  
  <p>Any advice on how to fix this sample_weight error? Here is the basic code to reproduce the error:
  <a href="https://gist.github.com/andreimouraviev/2642384705034da92d6954dd9993fb4d" rel="nofollow noreferrer">https://gist.github.com/andreimouraviev/2642384705034da92d6954dd9993fb4d</a></p>
  
  <p>Also, if you have advice about how to deal the class imbalance problem, please let me know.</p>