Search code examples
pythontensorflowkerassparse-matrix

Using sparse array to represent labels when training Keras model


I am building a Keras model to classify data into 3000 different class, my training data consists of large number of sample so after encoding the output of the training in one hot encoding that data is very large (item_count * 3000 * size of float + input data size) Is is possible to pass sparse arrays to keras as output of training data, any suggested solution?


Solution

  • You can use a sparse representation of your ground truths by using the sparse_categorical_crossentropy loss function.

    # assuming get_model() returns your Keras model with an output_shape == [None, 3000]
    # assuming get_data() returns training data, with y_train having shape == [num_samples]
    
    x_train, y_train = get_data()
    model = get_model()
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    model.fit(x_train, y_train, epochs=10, batch_size=16)