0

I'm building a deep learning model in python with keras with multiple inputs, but only one categorical output. The data is suffering class imbalance so I need to use integer coded output categories instead of one-hot encoded output variables to be able to use the 'class_weight' when compiling the model. However, when I calculate the predictions I get an attributeError.

My final layer is Dense(12, activation = 'softmax'). And I used this code to calculate the loss on the test set:

catcross = keras.losses.SparseCategoricalCrossentropy()
catcross(y_true, y_pred).numpy()

Where y_true is a numpy array containing integer values ranging from [0-11] with 0 not present but one of the possible outcomes. example:

array([ 3,  5,  5,  5, 10,  3,  7, 10,  3,  6], dtype=int64)

And y_pred a numpy array containing the probabilities of each class, example:

array([[1.02893096e-16, 6.50112908e-10, 1.90304434e-07, 4.12046537e-02,
        9.54286734e-05, 9.56854999e-01, 3.55672855e-05, 1.27044728e-03,
        1.40260101e-06, 8.66925802e-06, 5.18984220e-04, 9.69776465e-06],
       [9.47600015e-21, 1.01175394e-12, 6.40883790e-09, 5.98808611e-03,
        1.64965022e-05, 9.93768573e-01, 1.39956160e-06, 1.83855431e-04,
        7.57585568e-08, 3.65254152e-07, 4.05745668e-05, 6.33996308e-07]],
      dtype=float32)

The error that is raised when I try this is:

...
4528   if not from_logits:
   4529     if (isinstance(output, (ops.EagerTensor, variables_module.Variable)) or
-> 4530         output.op.type != 'Softmax'):
   4531       epsilon_ = _constant_to_tensor(epsilon(), output.dtype.base_dtype)
   4532       output = clip_ops.clip_by_value(output, epsilon_, 1 - epsilon_)
...
AttributeError: 'numpy.ndarray' object has no attribute 'op'

How can I fix this? I have no idea what that error means..

Dnorious
  • 55
  • 7
  • Are you using Functional or Sequential API? post `model.summary`, shape of x_train, y_train, show model.architecture. – Vishnuvardhan Janapati May 22 '20 at 15:01
  • I use the Sequential API because it has multiple inputs. An LSTM, GRU and MLP are concatenated as input to a MLP network that only has dense layers. X_train consist of X_train1,X_train2 and X_train3 and has both pictures and sequential data but that should have no influence here and y_train is exactly the same as y_true only that y_true is for the test set, and y_train for the trianing set. – Dnorious May 22 '20 at 15:21
  • The model architecture shouldn't have no influence here as it worked without error when I was using one-hot encoding. Only to compensate the class imbalance I'm trying to do the same with integer outputs instead. – Dnorious May 22 '20 at 15:22
  • The final layer however, and the type of output will probably contain some errors. I'm not even sure I can use integer values in my case but I hope to do so to be able to use class_weight and avoid class imbalance. – Dnorious May 22 '20 at 15:23
  • Sequential is for use-cases that have single-input and single-output. If you have multiple-inputs, then Functional would be right model. Not sure what is the root-cause of your error. – Vishnuvardhan Janapati May 22 '20 at 15:26
  • I'm sorry my bad I use functional API – Dnorious May 22 '20 at 15:28

0 Answers0