深度学习总结:tensorflow和pytorch关于RNN的对比,tf.nn.dynamic_rnn,nn.LSTM
程序员文章站
2024-03-24 23:44:34
...
tensorflow和pytorch关于RNN的对比:
tf.nn.dynamic_rnn很难理解,他的意思只是用数据走一遍你搭建的RNN网络。
可以明显看出pytorch封装更高,更容易理解,动态图的优势。
## tensorflow
# RNN
# num_units=64代表h_t,c_t的维度
rnn_cell = tf.contrib.rnn.BasicLSTMCell(num_units=64)
# 这个累加的lstm_multi ,相当于pytorch里面的num_layers=3
stacked_rnn=[]
for i in range(3):
stacked_rnn.append(rnn_cell )
lstm_multi = tf.contrib.rnn.MultiRNNCell(stacked_rnn)
# tf.nn.dynamic_rnn这个就是在吓唬你,就是把batch放入lstm_multi里面跑,跑完后的各个time-step的输出
# 和最后一步的h_t,c_t,相当于r_out, (h_n, h_c) = self.rnn(x, None),只不过tensorflow需要提前建好图
outputs, (h_c, h_n) = tf.nn.dynamic_rnn(
lstm_multi , # cell you have chosen
image, # input
initial_state=None, # the initial hidden state
dtype=tf.float32, # must given if set initial_state = None
time_major=False, # False: (batch, time step, input); True: (time step, batch, input)
)
## pytorch
# hidden_size
self.rnn = nn.LSTM( # if use nn.RNN(), it hardly learns
input_size=INPUT_SIZE,
hidden_size=64, # rnn hidden unit
num_layers=3, # number of rnn layer
batch_first=True, # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
)
r_out, (h_n, h_c) = self.rnn(x, None) # None represents zero initial hidden state
上一篇: 读方法缓存区