I am implementing a multi-class classification machine learning model with Keras for NLP. I’m not sure whether this context is useful, but due to class imbalances, I created a class_weight array to be implemented in the model.fit() function as follows:
class_weight = {1: 3.,
2: 2.,
3: 1.5,
4: 1.,
5: 3.,
6: 3.
}
I then implemented my model.fit() function as follows:
history = model.fit(X_train, y_train,
epochs = 100,
verbose = False,
validation_data = (X_val, y_val),
batch_size = 10,
class_weight = class_weight,
callbacks = [early_stopping]
)
Running this, however, resulted with the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[583], line 9
1 class_weight = {1: 3.,
2 2: 2.,
3 3: 1.5,
(...)
6 6: 3.
7 }
----> 9 history = model.fit(X_train, y_train,
10 epochs = 100,
11 verbose = False,
12 validation_data = (X_val, y_val),
13 batch_size = 10,
14 class_weight = class_weight,
15 callbacks = [early_stopping]
16 )
File ~AppDataLocalanaconda3Libsite-packageskerassrcutilstraceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
119 filtered_tb = _process_traceback_frames(e.__traceback__)
120 # To get the full stack trace, call:
121 # `keras.config.disable_traceback_filtering()`
--> 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
File ~AppDataLocalanaconda3Libsite-packagesnumpycorefromnumeric.py:3360, in round(a, decimals, out)
3269 @array_function_dispatch(_round_dispatcher)
3270 def round(a, decimals=0, out=None):
3271 """
3272 Evenly round to the given number of decimals.
3273
(...)
3358
3359 """
-> 3360 return _wrapfunc(a, 'round', decimals=decimals, out=out)
File ~AppDataLocalanaconda3Libsite-packagesnumpycorefromnumeric.py:68, in _wrapfunc(obj, method, *args, **kwds)
59 return bound(*args, **kwds)
60 except TypeError:
61 # A TypeError occurs if the object does have such a method in its
62 # class, but its signature is not identical to that of NumPy's. This
(...)
66 # Call _wrapit from within the except clause to ensure a potential
67 # exception has a traceback chain.
---> 68 return _wrapit(obj, method, *args, **kwds)
File ~AppDataLocalanaconda3Libsite-packagesnumpycorefromnumeric.py:45, in _wrapit(obj, method, *args, **kwds)
43 except AttributeError:
44 wrap = None
---> 45 result = getattr(asarray(obj), method)(*args, **kwds)
46 if wrap:
47 if not isinstance(result, mu.ndarray):
TypeError: loop of ufunc does not support argument 0 of type str which has no callable rint method
The error is obviously in the model.fit() function, but I’m not sure where it originates and how to fix it. To try to alleviate the issue, I converted y_train
and y_val
into int types with astype(). However, this resulted in a different error:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[587], line 9
1 class_weight = {1: 3.,
2 2: 2.,
3 3: 1.5,
(...)
6 6: 3.
7 }
----> 9 history = model.fit(X_train, y_train,
10 epochs = 100,
11 verbose = False,
12 validation_data = (X_val, y_val),
13 batch_size = 10,
14 class_weight = class_weight,
15 callbacks = [early_stopping]
16 )
File ~AppDataLocalanaconda3Libsite-packageskerassrcutilstraceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
119 filtered_tb = _process_traceback_frames(e.__traceback__)
120 # To get the full stack trace, call:
121 # `keras.config.disable_traceback_filtering()`
--> 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
File ~AppDataLocalanaconda3Libsite-packagespandascoreseries.py:1040, in Series.__getitem__(self, key)
1037 return self._values[key]
1039 elif key_is_scalar:
-> 1040 return self._get_value(key)
1042 # Convert generator to list before going through hashable part
1043 # (We will iterate through the generator there to check for slices)
1044 if is_iterator(key):
File ~AppDataLocalanaconda3Libsite-packagespandascoreseries.py:1156, in Series._get_value(self, label, takeable)
1153 return self._values[label]
1155 # Similar to Index.get_value, but we do not fall back to positional
-> 1156 loc = self.index.get_loc(label)
1158 if is_integer(loc):
1159 return self._values[loc]
File ~AppDataLocalanaconda3Libsite-packagespandascoreindexesbase.py:3798, in Index.get_loc(self, key)
3793 if isinstance(casted_key, slice) or (
3794 isinstance(casted_key, abc.Iterable)
3795 and any(isinstance(x, slice) for x in casted_key)
3796 ):
3797 raise InvalidIndexError(key)
-> 3798 raise KeyError(key) from err
3799 except TypeError:
3800 # If we have a listlike key, _check_indexing_error will raise
3801 # InvalidIndexError. Otherwise we fall through and re-raise
3802 # the TypeError.
3803 self._check_indexing_error(key)
KeyError: 0
Please advise!