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