Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use sample weights in keras model
  • Loading branch information
somefreestring committed Feb 12, 2020
commit cfb4f1001276509a1fdeecc07e82cedd58579731
10 changes: 4 additions & 6 deletions pandas_ml_utils/model/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,12 @@ def fit(self,
sample_weight_train: np.ndarray, sample_weight_test: np.ndarray) -> float:
fitter_args = suitable_kwargs(self.keras_model.fit, **self.kwargs)

#if self.sample_weight_column:
# TODO now we can access the raw data frame from features and labels extractor
# so if we have a column defined for class weight we could provide the weighs for each sample to the fitter!
# FIXME doen not work yet: fitter_args["sample_weight"] = self.features_and_labels[self.sample_weight_column].values

if "verbose" in self.kwargs and self.kwargs["verbose"] > 0:
print(f'pass args to fit: {fitter_args}')

fit_history = self._exec_within_session(self.keras_model.fit,
x, y,
sample_weight=sample_weight_train.reshape((len(x), )),
epochs=self.epochs,
validation_data=(x_val, y_val),
callbacks=[cb() for cb in self.callbacks],
Expand Down Expand Up @@ -437,8 +433,10 @@ def fit(self,
index = range(pos, pos + len(labels))
target_y = y[:,index]
target_y_val = y_val[:,index]
target_w = sample_weight_train[:,index]
target_w_val = sample_weight_test[:,index]
_log.info(f"fit model for target {target}")
losses.append(self.models[target].fit(x, target_y, x_val, target_y_val, sample_weight_train, sample_weight_test))
losses.append(self.models[target].fit(x, target_y, x_val, target_y_val, target_w, target_w_val))
pos += len(labels)

losses = np.array(losses)
Expand Down
5 changes: 3 additions & 2 deletions test/unit_tests/model/test__model.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ def keras_model_provider(optimizer='adam'):
np.testing.assert_array_almost_equal(model1.get_weights(), model2.get_weights())

"""and after we fit one model"""
loss = model2.fit(np.array([0.1, 0.01]), np.array([0.1, 0.01]), np.array([0.1, 0.01]), np.array([0.1, 0.01]),
[0,1], [2,3])
loss = model2.fit(np.array([0.1, 0.01]), np.array([0.1, 0.01]),
np.array([0.1, 0.01]), np.array([0.1, 0.01]),
np.array([1,1]), np.array([1,1]))

"""then"""
self.assertIsNotNone(loss)
Expand Down