欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

稀疏矩阵

程序员文章站 2022-07-04 10:27:04
...

在不定长文本识别中用到了稀疏矩阵

将列表数据转化为稀疏矩阵的代码如下:

import numpy as np
def _sparse_tuple_from(sequences, dtype=np.int32):
    """
    将矩阵转为稀疏矩阵存储方式
    :param sequences:
    :param dtype:
    :return:
    """

    indices = []
    values = []
    for n, seq in enumerate(sequences):
        indices.extend(zip([n] * len(seq), [i for i in range(len(seq))]))
        values.extend(seq)

    indices = np.asarray(indices, dtype=np.int64)
    values = np.asarray(values, dtype=dtype)
    shape = np.asarray([len(sequences), np.asarray(indices).max(0)[1] + 1], dtype=np.int64)

    return indices, values, shape

batch_label=[]
batch_label.append([56,45,2347])
batch_label.append([1,6,7,13,98])
batch_label.append([2,6,4,32,12,78])
batch_label.append([15,3])

batch_label=_sparse_tuple_from(batch_label)

print(batch_label)

返回一个三元元组,分别是indices, values, shape

结果如下:

稀疏矩阵

tf.sparse_tensor_to_dense将稀疏张量转换为稠密张量

import tensorflow as tf


label = tf.sparse_placeholder(tf.int32, name='label')

dense_decoded = tf.sparse_tensor_to_dense(label, default_value=-1)

with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	print(sess.run(dense_decoded,feed_dict={label:batch_label}))

结果如下:

稀疏矩阵