# train the RNNdeftrain(rnn, n_steps, print_every):# initialize the hidden state
hidden =Nonefor batch_i, step inenumerate(range(n_steps)):# defining the training data
time_steps = np.linspace(step * np.pi,(step+1)*np.pi, seq_length +1)
data = np.sin(time_steps)
data.resize((seq_length +1,1))# input_size=1
x = data[:-1]
y = data[1:]# convert data into Tensors
x_tensor = torch.Tensor(x).unsqueeze(0)# unsqueeze gives a 1, batch_size dimension
y_tensor = torch.Tensor(y)# outputs from the rnn
prediction, hidden = rnn(x_tensor, hidden)## Representing Memory ### make a new variable for hidden and detach the hidden state from its history# this way, we don't backpropagate through the entire history
hidden = hidden.data
# calculate the loss
loss = criterion(prediction, y_tensor)# zero gradients
optimizer.zero_grad()# perform backprop and update weights
loss.backward()
optimizer.step()# display loss and predictionsif batch_i%print_every ==0:print('Loss: ', loss.item())
plt.plot(time_steps[1:], x,'r.')# input
plt.plot(time_steps[1:], prediction.data.numpy().flatten(),'b.')# predictions
plt.show()return rnn
pytorch每一个batch训练之前需要把hidden = hidden.data,否者反向传播的梯度会遍历以前的timesteptensorflow也有把new_state更新,但是没有明显detach的操作,预计是tensorflow自己机制默认backpropagation一个timestep的梯度: for e in range(epochs): # Train...