首页 文章

如何加速自动编码器用于在python的theano包中编写的文本数据?

提问于
浏览
0

我试图调整autoencoder脚本here来处理文本数据 . 此代码使用MNIST数据集作为训练数据 . 该数据采用numpy 2d数组的形式 .

我的数据是一个大约100,000个实例的csr稀疏矩阵,具有大约50,000个特征 . 矩阵是使用sklearn的tfidfvectorizer来拟合和转换文本数据的结果 . 当我使用稀疏矩阵时,我修改代码以使用theano.sparse包来表示我的输入 . 我的训练集是符号变量:

train_set_x = theano.sparse.shared(train_set)

但是,theano.sparse矩阵无法执行原始脚本中使用的所有操作(有一个稀疏操作列表here) . 代码使用输入上的张量方法的点和和 . 我已经将点更改为sparse.dot但是我无法找到替换总和的内容,因此我将训练批次转换为密集矩阵并使用此成本函数中显示的原始张量方法:

def get_cost(self):
     tilde_x = self.get_corrupted_input(self.x, self.corruption)
     y = self.get_hidden_values(tilde_x)
     z = self.get_reconstructed_input(y)
     #make dense, must be a better way to do this
     L = - T.sum(SP.dense_from_sparse(self.x) * T.log(z) + (1 - SP.dense_from_sparse(self.x)) * T.log(1 - z), axis=1)
     cost = T.mean(L)
     return cost

def get_hidden_values(self, input):
    # use theano.sparse.dot instead of T.dot
    return T.nnet.sigmoid(theano.sparse.dot(input, self.W) + self.b)

get_corrupted_input和get_reconstructed_input方法保持原样在上面的链接中 . 我的问题是有更快的方法吗?

将矩阵转换为密集使得训练非常缓慢 . 目前,一个训练时期需要20.67米,批量大小为20个训练实例 .

任何帮助或提示,您将非常感谢!

1 回答

  • 0

    在最新的theano.sparse主分支中,列出了一个sp_sum方法 .

    (see here)

    如果你没有使用最新版本,我会安装它,看看它是否会起作用,如果这样做会加快速度:

    pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
    

    (如果确实如此,注意它在这里会很好,但是并不总是很清楚稀疏功能比使用密集计算要快得多,特别是在gpu上 . )

相关问题