indexs[201] = [0,8] 乱序。许多稀疏操作需要排序索引。使用“tf.sparse.reorder”创建正确排序的副本

2024-02-29

我正在对每个变量进行编码的神经网络,当我要拟合模型时,会出现错误。

indices[201] = [0,8] is out of order. Many sparse ops require sorted indices.
    Use `tf.sparse.reorder` to create a correctly ordered copy.

 [Op:SerializeManySparse]

我不知道如何解决它。 我可以在这里打印一些代码,如果你想要更多,我仍然可以打印它

def process_atributes(df, train, test):

    continuas = ['Trip_Duration']
    cs = MinMaxScaler()
    trainCont = cs.fit_transform(train[continuas])
    testCont = cs.transform(test[continuas])

    discretas = ['Start_Station_Name', 'End_Station_Name', 'User_Type', 'Genero', 'Hora_inicio']
    ohe = OneHotEncoder()
    ohe.fit(train[discretas])

    trainDisc = ohe.transform(train[discretas])
    testDisc = ohe.transform(test[discretas])

    trainX = sc.sparse.hstack((trainDisc, trainCont))
    testX = sc.sparse.hstack((testDisc, testCont))
    return (trainX, testX)

def prepare_targets(df, train, test):

    labeled_col = ['RangoEdad']

    le = LabelEncoder()
    le.fit(train[labeled_col].values.ravel())
    trainY = le.transform(train[labeled_col])
    testY = le.transform(test[labeled_col])
    return trainY, testY

X_train_enc, X_test_enc = process_atributes(dataFrameDepurado2, train, test)
Y_train_enc, Y_test_enc = prepare_targets(dataSetPrueba, train, test)

model = Sequential()
model.add(Dense(10, input_dim = X_train_enc.shape[1], activation = 'tanh', kernel_initializer = 'he_normal'))
model.add(Dense(4, activation = 'sigmoid'))

model.compile(loss = 'sparse_categorical_crossentropy', optimizer = SGD(lr = 0.01), metrics = ['accuracy'])

model.fit(X_train_enc, Y_train_enc, validation_data = (X_test_enc, Y_test_enc), epochs = 20, batch_size = 64, shuffle = True) 

这是我的数据集

先感谢您。


在这里提及解决方案(答案部分),即使它存在于评论部分中,因为社区的利益.

的文档稀疏张量 https://www.tensorflow.org/api_docs/python/tf/sparse/SparseTensor states

By convention, indices should be sorted in row-major order (or equivalently 
lexicographic order on the tuples indices[i]). This is not enforced when
SparseTensor objects are constructed, but most ops assume correct ordering. If 
the ordering of sparse tensor st is wrong, a fixed version can be obtained by
calling [tf.sparse.reorder(st)][2].

所以,使用tf.sparse.reorder or scipy.sort_indices在矩阵上,X_train_enc, X_test_enc, Y_train_enc, Y_test_enc,在代码行之前,

model.fit(X_train_enc, Y_train_enc, validation_data = (X_test_enc, 
Y_test_enc), epochs = 20, batch_size = 64, shuffle = True)

将解决该问题。

有关更多信息,请参阅文档稀疏张量 https://www.tensorflow.org/api_docs/python/tf/sparse/SparseTensor and tf.稀疏.重新排序 https://www.tensorflow.org/api_docs/python/tf/sparse/reorder.

希望这可以帮助。快乐学习!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

indexs[201] = [0,8] 乱序。许多稀疏操作需要排序索引。使用“tf.sparse.reorder”创建正确排序的副本 的相关文章

随机推荐