I have 24 classes in my dataset and I am trying to set class weights in the following way:

ty = np.argmax(trainy, axis=1)
class_weights = class_weight.compute_class_weight('balanced', np.unique(ty), ty)
class_weights = dict(enumerate(class_weights))
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size,  class_weight = class_weights)

However, I get this error in Colab and do not know where 12 comes from:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
 58     ctx.ensure_initialized()
 59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
 61   except core._NotOkStatusException as e:
 62     if name is not None:

InvalidArgumentError:  indices[0] = 15 is not in [0, 12)
 [[{{node GatherV2}}]]
 [[IteratorGetNext]] [Op:__inference_train_function_69536]

Function call stack:train_function
  • $\begingroup$ This seems to be fixed in recent tf-nightly (as of Jun 2020) $\endgroup$ – Alex Pashkov Jun 26 '20 at 13:51

Browse other questions tagged or ask your own question.