用于语音识别的 Tensorflow LSTM 在训练每个后续单词时速度变慢
Tensor flow LSTM for speech recognition slows down when training each subsequent word
所以我正在尝试使用 tensorflows LSTM 来识别口语。然而,在每个训练词通过 LSTM 后,下一个词需要更长的时间来处理(特别是通过反向传播计算梯度并将其应用于网络)。我目前正在使用不支持 CUDA 的 iMac,因此我不得不使用 CPU 而不是 GPU(一旦可以,我将切换到 GPU)。
我正在使用 Python-2.7
编程
我使用的词汇量非常小,8 个单词 类 每个单词有 10 个训练示例,每个单词都是孤立的(不是句子的一部分,只是一个单词本身)。
每个单词都被预处理为 Mel 频率倒谱系数,然后使用 Kmeans 对这些系数进行聚类,K = 100。因此,LSTM 的输入是一次输入一个项目的 float32 列表。
LSTM 肯定会出现减速,因为从列表中获取每个项目并将其传递给 LSTM 所花费的时间对于每个项目都大致保持不变。每次传递给 LSTM 的每个项目的大小也是相同的(更长的单词只是有更长的项目列表);然而,随着训练的继续,即使是更短的单词(列表中的项目更少)仍然需要越来越长的时间。
我正在使用 gradient decent 和 backpropogation 来训练网络,并尝试将梯度剪裁到 10 个时间步或根本不剪裁,这没有区别。
LSTM 实例化为:
cell = rnn_cell.BasicLSTMCell(size, forget_bias=config.forget_bias)
cell_layers = rnn_cell.MultiRNNCell([cell] * config.num_layers)
//pass inputs through the cell
outputs, states = RNN.rnn(cell_layers, _inputs,initial_state=self._initial_state)
在 LSTM 之后我有一个 softmax 层;使用交叉焓损失将其输出与代表正确输出的单热向量进行比较。
管道sudo代码:
_inputs = [[float32]*]
for input in _inputs: //input = _inputs[0][0] at time zero input = _inputs[0][1] at time 1 etc.
lstm_output = LSTM(input)
soft_out = softmax(last_output)
cost = CrossEnthalpyCost(soft_out, answer)
gradients = backprop(cost)
new_weights = gradientDecent(gradients, learning_rate)
最后,如果我不清楚我的问题是什么,这里是我的网络时间:
Epoch: 0 Learning rate: 1.000
{'heart': 5, 'car': 1, 'dog': 3, 'cat': 2, 'book': 0, 'three': 7, 'girl': 4, 'milk': 6}
book
time to input all clusters for one word into network: 0.0293724536896
time to pass all inputs for one word and perform gradient decent:2.956
Time difference from previous word: 2.956
Epoch Number: 0 Word Number:1, Number of Pieces:247, Word ID:0
time to input all clusters for one word into network: 0.0287952423096
time to pass all inputs for one word and perform gradient decent:3.738
Time difference from previous word: 0.782
Epoch Number: 0 Word Number:2, Number of Pieces:247, Word ID:0
time to input all clusters for one word into network: 0.029797077179
time to pass all inputs for one word and perform gradient decent:4.754
Time difference from previous word: 1.015
Epoch Number: 0 Word Number:3, Number of Pieces:250, Word ID:0
...
time to input all clusters for one word into network: 0.0417804718018
time to pass all inputs for one word and perform gradient decent:25.123
Time difference from previous word: 12.255
Epoch Number: 0 Word Number:24, Number of Pieces:258, Word ID:2
...
time to input all clusters for one word into network: 0.0413291454315
time to pass all inputs for one word and perform gradient decent:40.364
Time difference from previous word: 0.932
Epoch Number: 0 Word Number:38, Number of Pieces:255, Word ID:3
如果有人对为什么花费的时间越来越长有任何想法
这个问题是由于每次输入一个词段时使用 tf.assign 引起的。这会导致图形随着每次创建新变量而增长。在我的例子中,每个单词被分成大约 250 个部分,因此所花费的时间迅速且大量增加。
通过删除 tf.assign 方法并将有问题的变量替换为使用 feed_dict 而不是 tf.assign 设置的占位符来解决问题。
感谢@mrry 对我的帮助!
帮助解决此类错误的一个好技巧是 Graph.finalize()
...一旦您添加了所有您想要的图形操作,调用 finalize 并且任何尝试向图形添加额外操作都将抛出一个错误。在我开始并没有完全内化 "I build the graph ONCE and get pointers to tensors as I do, then I exclusively run ops/call eval on the tensors to do stuff" 咒语
时对我很有帮助
所以我正在尝试使用 tensorflows LSTM 来识别口语。然而,在每个训练词通过 LSTM 后,下一个词需要更长的时间来处理(特别是通过反向传播计算梯度并将其应用于网络)。我目前正在使用不支持 CUDA 的 iMac,因此我不得不使用 CPU 而不是 GPU(一旦可以,我将切换到 GPU)。
我正在使用 Python-2.7
编程我使用的词汇量非常小,8 个单词 类 每个单词有 10 个训练示例,每个单词都是孤立的(不是句子的一部分,只是一个单词本身)。
每个单词都被预处理为 Mel 频率倒谱系数,然后使用 Kmeans 对这些系数进行聚类,K = 100。因此,LSTM 的输入是一次输入一个项目的 float32 列表。
LSTM 肯定会出现减速,因为从列表中获取每个项目并将其传递给 LSTM 所花费的时间对于每个项目都大致保持不变。每次传递给 LSTM 的每个项目的大小也是相同的(更长的单词只是有更长的项目列表);然而,随着训练的继续,即使是更短的单词(列表中的项目更少)仍然需要越来越长的时间。
我正在使用 gradient decent 和 backpropogation 来训练网络,并尝试将梯度剪裁到 10 个时间步或根本不剪裁,这没有区别。
LSTM 实例化为:
cell = rnn_cell.BasicLSTMCell(size, forget_bias=config.forget_bias)
cell_layers = rnn_cell.MultiRNNCell([cell] * config.num_layers)
//pass inputs through the cell
outputs, states = RNN.rnn(cell_layers, _inputs,initial_state=self._initial_state)
在 LSTM 之后我有一个 softmax 层;使用交叉焓损失将其输出与代表正确输出的单热向量进行比较。
管道sudo代码:
_inputs = [[float32]*]
for input in _inputs: //input = _inputs[0][0] at time zero input = _inputs[0][1] at time 1 etc.
lstm_output = LSTM(input)
soft_out = softmax(last_output)
cost = CrossEnthalpyCost(soft_out, answer)
gradients = backprop(cost)
new_weights = gradientDecent(gradients, learning_rate)
最后,如果我不清楚我的问题是什么,这里是我的网络时间:
Epoch: 0 Learning rate: 1.000
{'heart': 5, 'car': 1, 'dog': 3, 'cat': 2, 'book': 0, 'three': 7, 'girl': 4, 'milk': 6}
book
time to input all clusters for one word into network: 0.0293724536896
time to pass all inputs for one word and perform gradient decent:2.956
Time difference from previous word: 2.956
Epoch Number: 0 Word Number:1, Number of Pieces:247, Word ID:0
time to input all clusters for one word into network: 0.0287952423096
time to pass all inputs for one word and perform gradient decent:3.738
Time difference from previous word: 0.782
Epoch Number: 0 Word Number:2, Number of Pieces:247, Word ID:0
time to input all clusters for one word into network: 0.029797077179
time to pass all inputs for one word and perform gradient decent:4.754
Time difference from previous word: 1.015
Epoch Number: 0 Word Number:3, Number of Pieces:250, Word ID:0
...
time to input all clusters for one word into network: 0.0417804718018
time to pass all inputs for one word and perform gradient decent:25.123
Time difference from previous word: 12.255
Epoch Number: 0 Word Number:24, Number of Pieces:258, Word ID:2
...
time to input all clusters for one word into network: 0.0413291454315
time to pass all inputs for one word and perform gradient decent:40.364
Time difference from previous word: 0.932
Epoch Number: 0 Word Number:38, Number of Pieces:255, Word ID:3
如果有人对为什么花费的时间越来越长有任何想法
这个问题是由于每次输入一个词段时使用 tf.assign 引起的。这会导致图形随着每次创建新变量而增长。在我的例子中,每个单词被分成大约 250 个部分,因此所花费的时间迅速且大量增加。
通过删除 tf.assign 方法并将有问题的变量替换为使用 feed_dict 而不是 tf.assign 设置的占位符来解决问题。
感谢@mrry 对我的帮助!
帮助解决此类错误的一个好技巧是 Graph.finalize()
...一旦您添加了所有您想要的图形操作,调用 finalize 并且任何尝试向图形添加额外操作都将抛出一个错误。在我开始并没有完全内化 "I build the graph ONCE and get pointers to tensors as I do, then I exclusively run ops/call eval on the tensors to do stuff" 咒语