Which version of tensorflow have the function to use class_weight

Dear everyone:

I want to know which version of tensorflow have the function to use class_weight? that recent versions of tensorflow have restricted the use of class_weight

Thanks
best wishes

1 Like

What do you mean with ā€œrestrictedā€?

1 Like

@jiachen_luo , I think it is not restricted and you can still use it in latest version. Please take a look at this

1 Like

Thanks. I tried to follow the tutorial, but it still reported an error as follows: ValueError: class_weight not supported for 3+ dimensional targets.

the codingļ¼š class_weight={0: 4, 1: 15, 2: 15, 3: 3, 4: 1, 5: 6, 6: 3}ļ¼Œthe error is ValueError: class_weight not supported for 3+ dimensional targets.

def train_model(self):

	checkpoint = ModelCheckpoint(self.PATH, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')

	if self.modality == "audio":
		model = self.get_audio_model()
		model.compile(optimizer='adadelta', loss='categorical_crossentropy', sample_weight_mode='temporal')
	elif self.modality == "text":
		model = self.get_text_model()
		model.compile(optimizer='adadelta', loss='categorical_crossentropy', sample_weight_mode='temporal')
	elif self.modality == "bimodal":
		model = self.get_bimodal_model()
		model.compile(optimizer='adam', loss='categorical_crossentropy', sample_weight_mode='temporal')

	early_stopping = EarlyStopping(monitor='val_loss', patience=10)
	model.fit(self.train_x, self.train_y,
	                epochs=self.epochs,
	                batch_size=self.batch_size,
	                sample_weight=self.train_mask,
	                shuffle=True, 
	                class_weight=class_weight,
	                callbacks=[early_stopping, checkpoint],
	                validation_data=(self.val_x, self.val_y, self.val_mask))

	self.test_model()

Could you give me more detailed guidance to solve this problem?

Thanks.

Best wishes.

1 Like

Yes, this is known. For that you need to follow sample_weights. The following tutorial demos one use-case:

Immense thanks to @markdaoust for providing this section.

2 Likes

We already had a thread at

https://tensorflow-prod.ospodiscourse.com/t/class-weight-not-supported-for-3-dimensional-targets/1817?u=bhack

1 Like