简体   繁体   中英

How should I set up my data for a sparse_categorical_crossentropy loss function?

I really struggled myself the last two days to figure out the problem. I am new to machine learning and with keras I'm trying to build a very simple neural network which uses sparse_c._c . as a one hot encoded matrix would be enormous, in fact my labels reach very random values up to 1000; but I get an error saying that sparse_c._c . does not accept values out of the range [0, 1) which is odd because I saw from documentation and lot of articles that this loss function accepts any integer. Am I giving some wrong input data? Is there an easier way to make my neural network work?

    dataset=numpy.genfromtxt("data.csv", delimiter=",")
    labelset=numpy.genfromtxt("labels.csv", delimiter=",")
    print(dataset.shape)
    print(labelset.shape)
    vn=dataset.shape[1]-2

    X=dataset[:, 0:vn]
    Y=labelset[:, 0:1]



    train_data, test_data, train_labels, test_labels = train_test_split(X, Y, test_size=.3, random_state=5)


    train_labels = numpy.squeeze(numpy.asarray(train_labels))
    test_labels = numpy.squeeze(numpy.asarray(test_labels))

    model=Sequential()
    model.add(Dense(vn, input_dim=vn, activation='relu'))
    model.add(Dense(1, kernel_initializer='normal', activation='relu'))


    model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=['sparse_categorical_accuracy'])
    model.fit(train_data, train_labels, epochs=100, batch_size=50, validation_data=(test_data, test_labels))    

    scores=model.evaluate(test_data, test_labels, verbose=0)
    print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

    ##Dataset example: [[4, 5, 6, 7],
    ##                   5, 23, 6, 2],
    ##                   2, 456, 7, 5]]
    ##Labels example: [1, 1, 4]

The gist of the error I get: Received a label value of 4 which is outside the valid range of [0, 1). Label values: 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

Sparse in the context of loss means that you have a vector which is non zero (1 in this case) iff the class index matches the label vector index. So you would have to feed [0 0 0 1] instead of [4] (assuming you have 4 classes in total).

You can use this to convert your labels:

num_classes = 4
labels = np.subtract(labels, 1) # assuming your labels start at 1 instead of 0
labels = np.squeeze(np.eye(num_classes)[np.array(labels).reshape(-1)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM