2016-11-26 73 views
3

爲了進一步理解RNN和LSTM,我試圖實現一個簡單的LSTM來估計正弦波的頻率和相位。這被證明是非常難以收斂的。 MSE是相當高的(以千計) 似乎工作一點的唯一的事情是,如果我生成全部具有相同相位的正弦波(同時開始)並且訓練樣本作爲矢量傳遞而不是在RNN中一次一個採樣。同時,這裏是不會收斂的代碼。在這段代碼中,我已經刪除了每個頻率的不同相位 對此處出現的問題有任何想法RNN LSTM估計正弦波頻率和相位

我看過這個Keras : How should I prepare input data for RNN?並試圖修改我的輸入,但沒有運氣。

from keras.models import Sequential 
from keras.layers.core import Activation, Dropout ,Dense 
from keras.layers.recurrent import GRU, LSTM 
import numpy as np 
from sklearn.cross_validation import train_test_split 

np.random.seed(0) # For reproducability 
TrainingNums = 12000 #Number of Trials 
numSampleInEach = 200 #Length of each sinewave 
numPhaseExamples = 1 #for each freq, so many different phases 

X = np.zeros((TrainingNums,numSampleInEach)) 
Y = np.zeros((TrainingNums,2)) 

#create sinewaves here 
for iii in range(0, TrainingNums//numPhaseExamples): 
    freq = np.round(np.random.randn()*100) 
    for kkk in range(0,numPhaseExamples): 
    #set timeOffset below to 0, if you want the same phase every run 
     timeOffset = 0# 0 for now else np.random.randint(0,90) 
     X[iii*numPhaseExamples+kkk,:] = np.sin(2*3.142*freq*np.linspace(0+timeOffset,numSampleInEach-1+timeOffset,numSampleInEach)/10000) 
     Y[iii*numPhaseExamples+kkk,0] = freq 
     Y[iii*numPhaseExamples+kkk,1] = timeOffset 

X = np.reshape(X,(TrainingNums, numSampleInEach,1)) 
#This below works when there is no phase variation 
#X = np.reshape(X,(TrainingNums, numSampleInEach,1)) 

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33) 

#Now create the RNN 
model = Sequential() 
#batch_input_shape = [batch_size,timeStep,dataDimension] 
model.add(LSTM(128,input_shape= (numSampleInEach,1),return_sequences=True)) 

#For things to work for freq estimation only the following change helps 
#model.add(LSTM(128,input_shape=(1,numSampleInEach),return_sequences=True)) 
model.add(Dropout(0.2)) 
model.add(Activation("relu")) 

#second layer of RNN 
model.add(LSTM(128,return_sequences=False)) 
model.add(Dropout(0.2)) 
model.add(Activation("relu")) 

model.add(Dense(2,activation="linear")) 
model.compile(loss="mean_squared_error", optimizer="Nadam") 
print model.summary() 

print "Model compiled." 
model.fit(X_train, y_train, batch_size=16, nb_epoch=150, 
     validation_split=0.1) 
result = model.evaluate(X_test, y_test, verbose=0) 
print 'mse: ', result 

所以問題是:

  1. 是不是期待的RNN估計頻率和相位?
  2. 我嘗試了幾個架構(多層LSTM,單層與更多的節點等)。我也嘗試過不同的體系結構。
+1

取得了一些進展,在這裏記錄了他人。主要是在LSTM之後不應該有激活。 LSTM層之間的激活是錯誤 – krat

回答

1

卸下LSTM後的激活是正確的答案