【问题标题】:indices[201] = [0,8] is out of order. Many sparse ops require sorted indices.Use `tf.sparse.reorder` to create a correctly ordered copy索引[201] = [0,8] 出现故障。许多稀疏操作需要排序索引。使用 `tf.sparse.reorder` 创建正确排序的副本
【发布时间】:2020-09-09 15:35:22
【问题描述】:

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

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) 

这是我的数据集

提前谢谢你。

【问题讨论】:

  • 如果你的稀疏数组被编码为行1[4, 2, 1, 5, 9]数据[1, 2, 3, 4, 5]列索引是乱序的。 Use 'tf.sparse.reorder' to create a correctly ordered copy.
  • 我应该在哪里使用这个功能?因为这个函数的输入是一个稀疏张量,而不是一个稀疏矩阵。你想要更多的代码来解决这个问题吗?还是谢谢你
  • 您应该在拟合模型之前使用它。如果您愿意,可以在 scipy 稀疏矩阵上使用 .sort_indices() 对索引进行排序,然后再将其转换为张量。

标签: python tensorflow keras neural-network sparse-matrix


【解决方案1】:

为了社区的利益,在此处(回答部分)提及解决方案(即使它出现在评论部分)。

SparseTensor 状态的文档

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].

因此,在代码行之前的矩阵X_train_enc, X_test_enc, Y_train_enc, Y_test_enc 上使用tf.sparse.reorderscipy.sort_indices

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

将解决问题。

更多信息请参考Sparse Tensortf.sparse.reorder的文档。

希望这会有所帮助。快乐学习!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 2021-08-10
    • 2018-01-24
    • 1970-01-01
    相关资源
    最近更新 更多