rnn

随手记录一些比较模糊的点

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值