问题
用稀疏矩阵作为model的输入,提示错误:
InvalidArgumentError: indices[1] = [0,1247] is out of order. Many sparse ops require sorted indices.
Use `tf.sparse.reorder` to create a correctly ordered copy.
原因
稀疏矩阵的索引无序,如下所示:
SparseTensor(indices=tf.Tensor(
[[ 0 1274]
[ 0 1247]
[ 0 1194]
...
[2707 329]
[2707 186]
[2707 19]], shape=(49216, 2), dtype=int64), values=tf.Tensor([0.11111111 0.11111111 0.11111111 ... 0.07692308 0.07692308 0.07692308], shape=(49216,), dtype=float32), dense_shape=tf.Tensor([2708 1433], shape=(2,), dtype=int64))
解决
将稀疏矩阵的索引变有序:
features_s = tf.sparse.reorder(features_s)
print(features_s)
SparseTensor(indices=tf.Tensor(
[[ 0 19]
[ 0 81]
[ 0 146]
...
[2707 1328]
[2707 1412]
[2707 1414]], shape=(49216, 2), dtype=int64), values=tf.Tensor([0.11111111 0.11111111 0.11111111 ... 0.07692308 0.07692308 0.07692308], shape=(49216,), dtype=float32), dense_shape=tf.Tensor([2708 1433], shape=(2,), dtype=int64))