TF.LSTM实现
感悟:耗时最多的就是数据格式整理,其本身并不复杂
NN-LSTM-NN-SOFTMAX
数据格式:batch_size =>批大小,n_steps=>要建立多少lstm
0.原始输入数据格式:batch_size,n_steps,data_size
1.合并数据,准备NN:batch_size*n_steps,data_size
2.通过NN后: batch_size*n_steps,hidden_unit
3.准备进入LSTM:batch_size,n_steps,hidden_unit
4.lstm=tf.contrib.rnn.BasicLSTMCell(hidden_unit,forget_bias=1.0,state_is_tuple=True)
5.构建多层的lstm(层数为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,对应的输入格式为batch_size,n_steps,hidden_unit。调整格式输入数据格式,等于true时,无论如何训练结果都有问题?
8.解开output为list(batch_size,outputs,n_steps),output的数据格式为(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。_) )