參考資料
源碼請點:https://github.com/llSourcell/tensorf...
語音識別無處不在,siri,google,訊飛輸入法,訊飛語記,智能家居,車,etc。
每天都在用的,很好奇它是怎么實現的,今天來看看這么便利的東東到底是什么樣子呢。
進化史
最開始的 speech recognizer 只能識別 0-9 這幾個數字,說別的單詞是識別不了滴。
后來有一個叫做 DARPA 的夢想家 team 孜孜不倦地研究。
他們用 15000 個節點代表可能的發音,然后用暴力搜索 brute force search 算法來找到節點對應的文字。
后來 IBM 用 Hidden Markov Model 來預測每個點最大概率可能表示的文字。
再后來人們嘗試用 NN 神經網絡來做這個任務,但是很長時間沒太大進展,直到 深度學習之父 Geoffrey Hinton 研究出個 Deep Learning 模型,語音識別的效果顯著提高。
Yours ~~
像 Siri,Google 一樣,現在我們來看看怎樣用 TensorFlow 創建自己的 Speech Recognizer ,來識別數字吧。
Steps:
- 導入庫
- 定義參數
- 導入數據
- 建立模型
- 訓練模型并預測
1. 導入庫
需要用到 tflearn,這是建立在 TensorFlow 上的高級的庫,可以很方便地建立網絡。
還會用到輔助的類 speech_data,用來下載數據并且做一些預處理。
from __future__ import division, print_function, absolute_import
import tflearn
import speech_data
import tensorflow as tf
2. 定義參數
learning rate 是在更新權重的時候用,太高可以很快,但是loss大,太低較準但是很慢。
learning_rate = 0.0001
training_iters = 300000 # steps
batch_size = 64
width = 20 # mfcc features
height = 80 # (max) length of utterance
classes = 10 # digits
3. 導入數據
用 speech_data.mfcc_batch_generator 獲取語音數據并處理成批次,然后創建 training 和 testing 數據。
batch = word_batch = speech_data.mfcc_batch_generator(batch_size)
X, Y = next(batch)
trainX, trainY = X, Y
testX, testY = X, Y #overfit for now
4. 建立模型
接下來,用什么模型呢?
speech recognition 是個 many to many 的問題。
eg,speech recognition
eg,image classification
eg,image caption
eg,sentiment analysis
所以我們用 Recurrent NN 。
通常的 RNN ,它的輸出結果是受整個網絡的影響的。
而 LSTM 比 RNN 好的地方是,它能記住并且控制影響的點。所以這里我們用 LSTM。
每一層到底需要多少個神經元是沒有規定的,太少了的話預測效果不好,太多了會 overfitting,這里我們取普遍的 128.
為了減輕過擬合的影響,我們用 dropout,它可以隨機地關閉一些神經元,這樣網絡就被迫選擇其他路徑,進而生成想對 generalized 模型。
接下來建立一個 fully connected 的層,它可以使前一層的所有節點都連接過來,輸出 10 類,因為數字是 0-9,激活函數用 softmax,它可以把數字變換成概率。
最后用個 regression 層來輸出唯一的類別,用 adam 優化器來使 cross entropy 損失達到最小。
# Network building
net = tflearn.input_data([None, width, height])
net = tflearn.lstm(net, 128, dropout=0.8)
net = tflearn.fully_connected(net, classes, activation='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=learning_rate, loss='categorical_crossentropy')
5. 訓練模型并預測
然后用 tflearn.DNN 函數來初始化一下模型,接下來就可以訓練并預測,最后再保存訓練好的模型。
# Training
### add this "fix" for tensorflow version errors
col = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
for x in col:
tf.add_to_collection(tf.GraphKeys.VARIABLES, x )
model = tflearn.DNN(net, tensorboard_verbose=0)
while 1: #training_iters
model.fit(trainX, trainY, n_epoch=10, validation_set=(testX, testY), show_metric=True,
batch_size=batch_size)
_y=model.predict(X)
model.save("tflearn.lstm.model")
print (_y)
print (y)
模型訓練需要一段時間,一邊碎覺一邊等著模型出爐吧。?? ?? ?? ??
我是 不會停的蝸牛 Alice
85后全職主婦
喜歡人工智能,行動派
創造力,思考力,學習力提升修煉進行中
歡迎您的喜歡,關注和評論!