tensorflow/decision-forests

Can't use both `sample_weight` and `class_weight` at the same time

aburke1605 opened this issue · 1 comments

I'm trying to implement a GBDT classifier in my work where I have two data types named "signal" and "background".

I have sample_weights for each datapoint which I find externally beforehand, and I also have a much larger signal sample than background and so I must use class_weights.

While I can use either sample_weight or class_weight just fine, I get a ValueError when trying to use both simultaneously:

ValueError: Tried to convert 'shape' to a tensor and failed. Error: Cannot convert a partially known TensorShape (None,) to a Tensor.

Minimum reproducing example:

import tensorflow_decision_forests as tfdf # tfdf.__version__ = '1.8.1'
import numpy as np

# generate some data of first type...
N_signal = 30
X_signal = np.random.random(2*N_signal).reshape((N_signal,2))
y_signal = np.ones(N_signal, dtype=int)
w_signal = np.random.random(N_signal)

# ...and of second type
N_background = 20
X_background = np.random.random(2*N_background).reshape((N_background,2))
y_background = np.zeros(N_background, dtype=int)
w_background = np.random.random(N_background)

# calculate class weights since unequal amounts of each:
N_total = N_signal + N_background
weight_for_signal = (1 / N_signal) * (N_total / 2)
weight_for_backgound = (1 / N_background) * (N_total / 2)
cw = {0: weight_for_backgound, 1: weight_for_signal}

# combine into one dataset
X = np.concatenate([X_signal, X_background])
y = np.concatenate([y_signal, y_background])
w = np.concatenate([w_signal, w_background])

# make default model
model = tfdf.keras.GradientBoostedTreesModel()
# fit with sample_weight only:
model.fit(X, y, sample_weight=w, verbose=0) # fine
print("done")
# fit with class_weight only:
model.fit(X, y, class_weight=cw, verbose=0) # fine
print("done")
# fit with both:
model.fit(X, y, sample_weight=w, class_weight=cw, verbose=0) # <---- ValueError
print("done")

I've tried to remedy with np.reshape and other things but can't fix it. Is this expected behaviour?

rstz commented

Hi,
class_weight is not properly supported by TF-DF, so this is indeed expected - sorry about that.