感悟:耗時最多的就是數(shù)據(jù)格式整理,其本身并不復(fù)雜
NN-LSTM-NN-SOFTMAX
數(shù)據(jù)格式:batch_size =>批大小涮拗,n_steps=>要建立多少lstm
0.原始輸入數(shù)據(jù)格式:batch_size,n_steps,data_size
1.合并數(shù)據(jù)耕驰,準(zhǔn)備NN:batch_size*n_steps,data_size
2.通過NN后: batch_size*n_steps,hidden_unit
3.準(zhǔn)備進入LSTM:batch_size,n_steps,hidden_unit
4.lstm=tf.contrib.rnn.BasicLSTMCell(hidden_unit,forget_bias=1.0,state_is_tuple=True)
5.構(gòu)建多層的lstm(層數(shù)為lstm_layers):cell=tf.contrib.rnn.MultiRNNCell([lstm]*lstm_layers)
6.init_state=lstm.zero_state(batch_size,tf.float32)
7.output,state=tf.nn.dynamic_rnn(lstm,lstm_in,time_major=False,dtype=tf.float32,initial_state=init_state)
這里需要注意time_major=False,對應(yīng)的輸入格式為batch_size,n_steps,hidden_unit。調(diào)整格式輸入數(shù)據(jù)格式鹃骂,等于true時,無論如何訓(xùn)練結(jié)果都有問題恬口?
8.解開output為list(batch_size,outputs,n_steps),output的數(shù)據(jù)格式為(outputs, batch_size,n_steps ):
tf.unstack(value=, axis=)將value安裝axis的維度展開和tf.split(axis=, num_or_size_splits, value=)+reshape()相似(利用reshape將1抹掉比伏,降低一個維度)
output=tf.unstack(tf.transpose(output,[1,0,2]))
9.取最后一個output:y=tf.matmul(output[-1],weights['out'])+bias['out’]
10.計算交叉熵:loss=tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=y,labels=y敞曹。_) )