将张量批与稀疏矩阵相乘

时间:2020-04-28 08:14:48

标签: python numpy tensorflow matrix keras

我想通过将数据与稀疏矩阵相乘来对损失函数进行线性变换。

我为此编写了一些伪代码,请帮助我进行转换:

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import pandas as pd

from scipy import sparse

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers


# I have a 0-1 sparse matrix
sparse_matix = sparse.random(1000, 20, density=.2, format='coo', data_rvs=np.ones,dtype='f' ).astype('int8') 
sparse_matix # <1000x20 sparse matrix of type '<class 'numpy.int8'>' with 4000 stored elements in COOrdinate format>

# Dummy data generator
def data_gen():
    for i in range(100):
        yield np.random.normal(size=(500, 1000)).astype('float32')

dataset = tf.data.Dataset.from_generator(data_gen, output_types=np.dtype('float32'), output_shapes=(500, 1000)).batch(5)

dataset_batch = dataset.__iter__().__next__()
dataset_batch.shape # TensorShape([5, 500, 1000])



def fn(dataset_batch): # i want this function to written completly in tensorflow, so that i can use this in my loss function
    dataset_batch = dataset_batch.numpy()
    return np.array([dataset_batch[i]*sparse_matix for i in range(dataset_batch.shape[0])]) # mutiple with sparse matrix

fn(dataset_batch).shape # (5, 500, 20)

请帮助我使用tensorflow编写fn。 Tensorflow层会很好

1 个答案:

答案 0 :(得分:0)

我找到了一种解决方案,该解决方案要求将稀疏矩阵转换为密集的张量对象,从而使用更多的ram。如果有人知道更好的解决方案,请发布。

代码:

order = np.lexsort((sparse_matix.col, sparse_matix.row))
sparse_matix_tensor = tf.SparseTensor(indices=list(zip(sparse_matix.row[order], sparse_matix.col[order])), values=sparse_matix.data[order].astype('float32'), dense_shape=sparse_matix_tensor.shape)
sparse_matix_tensor = tf.sparse.to_dense(sparse_matix_tensor)

def fn(dataset_batch):
    return tf.tensordot(dataset_batch, sparse_matix_tensor, axes=[[2], [0]])