随手记录一些比较模糊的点
1.dropout
作用是随机去除一些数据,在rnn中分为input和output的dropout ,output的dropout比较特殊,
其中input部分:
if is_training and keep_prob < 1:
inputs = tf.nn.dropout(inputs, keep_prob)
其中is_training 是判断是否是训练阶段,因为只有训练阶段需要dropout,跑test验证数据的时候要全量跑,keep_prob的意思是去除的百分比 0-1之间,0全去除,1不去除
output部分
if is_training and keep_prob < 1:
lstm_cell = tf.nn.rnn_cell.DropoutWrapper(
lstm_cell, output_keep_prob=keep_prob)
与input不同的是调用了rnn_cell的方法,不是tf.nn的通用方法,具体区别可以百度下。
2.
inputRnn = tf.reshape(inputRnn, [-1, time_step, 10]) # 将tensor转成3维,作为lstm cell的输入
cell = tf.nn.rnn_cell.BasicLSTMCell(10)
dropout_lstm = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=0.5)
lstm_cell = tf.nn.rnn_cell.MultiRNNCell([dropout_lstm]*4, state_is_tuple=True)
initState = lstm_cell.zero_state(1, dtype=tf.float32)
output_rnn,_ = tf.nn.dynamic_rnn(lstm_cell, inputRnn, initial_state=initState,
dtype=tf.float32) # output_rnn是记录lstm每个输出节点的结果,final_states是最后一个cell的结果
# output = tf.reshape(output_rnn, [-1, 10]) # 作为输出层的输入
output = tf.reshape(output_rnn, [-1, 10]) # 作为输出层的输入
终于闹明白这一段里面各个参数的含义了
inoutRnn的维度,必须和下面初始化initState的维度一样
initState = lstm_cell.zero_state(1, dtype=tf.float32)
特别注意,要用MultiRNNCell的zero_state方法,不要用basic的了
正常时间序列数据,这里的time_step应该取5-20,代表取这一段时间内的数据作为LSTM训练批次
如果输入数据不是时序数据,这个time_step 设为1,不过这种数据效果不是很理想
lstm_cell = tf.nn.rnn_cell.MultiRNNCell([dropout_lstm]*4, state_is_tuple=True)
这里*4,表示网络深度为4层
题外话:
tf.variable_scope() and tf.name_scope() 用法
https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-12-scope/
tensowflow 保存模型,加载模型
https://blog.csdn.net/ying86615791/article/details/72731372
官网rnn示例详细解释
https://blog.csdn.net/diligent_321/article/details/53385508