首页 > 解决方案 > 如何在tensorflow中使用稀疏矩阵(csr_matrix作为anndata格式)?

问题描述

我正在尝试使用scDeepCluster框架来处理单个单元格数据。我已经从 github 下载了原始代码并修复了一些版本错误。

但是当我将数据输入模型进行预训练时,出现以下错误。

TypeError: Could not build a TypeSpec for <30000x18896 sparse matrix of type '<class 'numpy.float32'>' 
with 4734184 stored elements in Compressed Sparse Row format> with type csr_matrix

During handling of the above exception, another exception occurred:
TypeError: sparse matrix length is ambiguous; use getnnz() or shape[0]

这是代码:

def pretrain(self, x, y, batch_size=256, epochs=200, optimizer='adam', ae_file='ae_weights.h5'):
    print('...Pretraining autoencoder...')
    self.autoencoder.compile(loss=self.loss, optimizer=optimizer)
    es = EarlyStopping(monitor="loss", patience=50, verbose=1)
    self.autoencoder.fit(x=x, y=y, batch_size=batch_size, epochs=epochs, callbacks=[es])
    self.autoencoder.save_weights(ae_file)
    print('Pretrained weights are saved to ./' + str(ae_file))
    self.pretrained = True

我使用以下句子调用这些代码,其中adata是一个AnnData对象。

scDeepCluster.pretrain(x=[adata.X, adata.obs.size_factors], 
                       y=adata.raw.X, 
                       batch_size=256, 
                       epochs=600, 
                       optimizer=optimizer1, 
                       ae_file='ae_weights.h5')

我已经减少了数据量,但错误仍然存​​在。错误与模型结构有关吗?或者是否有任何参数可以使用稀疏矩阵设置来拟合模型?

我如何在张量流中使用稀疏矩阵来解决这个问题?非常感谢 :)

标签: pythontensorflowdeep-learningsparse-matrixbioinformatics

解决方案


推荐阅读