title: on colab - class_weight is causing a ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
body: <p>i'm running a CNN with keras sequential on google colab.</p>
  
  <p>i'm getting the following error:
  ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</p>
  
  <p>when i remove the class_weight argument from the model.fit function, the error is gone and the network is trained succesfully. however, i really want to account for unbalanced data</p>
  
  <p>i checked the shape of my class_weights vector and it's good (and nd.array, just like you would get when generating class_Weights from sklearn compute class weights function )</p>
  
  <p>not sure what details are relevant but i wil gladly provide more details regarding version and all that mess. </p>
  
  <p>p.s</p>
  
  <p>a fact that might be important - 
  my data is the FER2013 data and i'm using FERplus labels. 
  meaning, my samples are not associated with one unique class, rather each sample has it's own probability distribution for each class. 
  bottom line, my labels are vectors of size class_names with all elements adding up to one. </p>
  
  <p>just to be super clear, an example:
  img1
  label = [0,0,0,0,0.2,0,0.3,0,0,0.5]</p>
  
  <p>anyhow, i computed class_weights as an nd.array of size 10 with elements ranging between 0 and 1, supposed to balance down the more represented classes. </p>
  
  <p>i was not sure if that is relevant to the error, but i'm bringing it up just in case.</p>
  
  <p>my code:</p>
  
  <pre><code>def create_model_plus():
    return tf.keras.models.Sequential([
      tf.keras.layers.Conv2D(filters=32,kernel_size=5,strides=1,input_shape=(48, 48, 1),padding='same',use_bias=True,kernel_initializer='normal',bias_initializer=tf.keras.initializers.Constant(0.1),activation=tf.nn.relu),
      tf.keras.layers.BatchNormalization(),
      tf.keras.layers.MaxPooling2D((2, 2), strides=2),
      tf.keras.layers.Conv2D(filters=64,kernel_size=5,strides=1,padding='same',use_bias=True,kernel_initializer='normal',bias_initializer=tf.keras.initializers.Constant(0.1),activation=tf.nn.relu),
      tf.keras.layers.BatchNormalization(),
      tf.keras.layers.MaxPooling2D((2, 2), strides=1),
      tf.keras.layers.Conv2D(filters=128,kernel_size=5,strides=1,padding='same',use_bias=True,kernel_initializer='normal',bias_initializer=tf.keras.initializers.Constant(0.1),activation=tf.nn.relu),
      tf.keras.layers.BatchNormalization(),    
      tf.keras.layers.MaxPooling2D((2, 2), strides=1),
      tf.keras.layers.Flatten(),
      tf.keras.layers.Dense(1008, activation='relu'),
      tf.keras.layers.Dropout(0.2),
      tf.keras.layers.Dense(512, activation='relu'),
      tf.keras.layers.Dense(10, activation='softmax')
    ])
  
  
  history_df=[]
  history_object=tf.keras.callbacks.History()
  #save_best_object=tf.keras.callbacks.ModelCheckpoint('/Users/nimrodros', monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
  
  early_stop_object=tf.keras.callbacks.EarlyStopping(monitor='val_loss',min_delta=0.001, patience=4)
  gony_adam=tf.keras.optimizers.Adam(
      lr=0.001
  )
  reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.3,patience=3, min_lr=0.0001, verbose=1)
  
  #log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
  #tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
  
  datagen = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=8, width_shift_range=0.2,
      height_shift_range=0.2,
      horizontal_flip=True,
      validation_split=0.3
      )
  datagen.fit(images.reshape(28709,48,48,1))
  model = create_model_plus()
  model.compile(optimizer=gony_adam,
          loss='categorical_crossentropy',
          metrics=['accuracy'])
  history = model.fit(x=datagen.flow(images.reshape(28709,48,48,1), FER_train_labels, batch_size=32,subset='training'),validation_data=datagen.flow(images.reshape(28709,48,48,1), FER_train_labels, batch_size=32,subset='validation'),steps_per_epoch=600,validation_steps=250,epochs=60,callbacks=[history_object,early_stop_object,reduce_lr],class_weight=cl_weigh)
  history_df=pd.DataFrame(history.history)
  </code></pre>
  
  <p>hope someone knows what to do!
  thanks!!!</p>