Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么tensorflow中的RNN不学习?_Python_Tensorflow_Recurrent Neural Network - Fatal编程技术网

Python 为什么tensorflow中的RNN不学习?

Python 为什么tensorflow中的RNN不学习?,python,tensorflow,recurrent-neural-network,Python,Tensorflow,Recurrent Neural Network,我试图在不使用Python3.7中tensorflow(2)中的RNNAPI的情况下训练RNN,因此代码非常基本。有些事情确实出了问题,但我不确定是什么问题 作为参考,我使用的是一个数据集,因此我知道误差应该大致收敛到什么程度。我的RNN代码如下。它试图做的是使用前20个时间步来预测第21个时间步的序列值。我正在批量训练256码 虽然随着时间的推移损失会减少,但如果我遵循教程的方法,上限大约是它的10倍。时间的反向传播会有问题吗 state_size = 20 #dimensionality

我试图在不使用Python3.7中tensorflow(2)中的RNNAPI的情况下训练RNN,因此代码非常基本。有些事情确实出了问题,但我不确定是什么问题

作为参考,我使用的是一个数据集,因此我知道误差应该大致收敛到什么程度。我的RNN代码如下。它试图做的是使用前20个时间步来预测第21个时间步的序列值。我正在批量训练256码

虽然随着时间的推移损失会减少,但如果我遵循教程的方法,上限大约是它的10倍。时间的反向传播会有问题吗

state_size = 20  #dimensionality of the network
BATCH_SIZE = 256
#define recurrent weights and biases. W has 1 more dimension that the state 
#dimension as also processes the inputs
W = tf.Variable(np.random.rand(state_size+1, state_size), dtype=tf.float32)
b = tf.Variable(np.zeros((1,state_size)), dtype=tf.float32)

#weights and biases for the output
W2 = tf.Variable(np.random.rand(state_size, 1),dtype=tf.float32)
b2 = tf.Variable(np.zeros((1,1)), dtype=tf.float32)


init_state = tf.Variable(np.random.normal(size=[BATCH_SIZE,state_size]),dtype='float32')
optimizer = tf.keras.optimizers.Adam(1e-3)
losses = []
for epoch in range(20):
    with tf.GradientTape() as tape:
        loss = 0

        for batch_idx in range(200):

            current_state = init_state

            batchx = x_train_uni[batch_idx*BATCH_SIZE:(batch_idx+1)*BATCH_SIZE].swapaxes(0,1)
            batchy = y_train_uni[batch_idx*BATCH_SIZE:(batch_idx+1)*BATCH_SIZE]
            #forward pass through the timesteps
            for x in batchx:
                inst = tf.concat([current_state,x],1)  #concatenate state and inputs for that timepoint
                current_state = tf.tanh(tf.matmul(inst, W) + b)  #
            #predict using the hidden state after the full forward pass
            pred = tf.matmul(current_state,W2) + b2

            loss += tf.reduce_mean(tf.abs(batchy-pred))

    #get gradients with respect to parameters
    gradients = tape.gradient(loss, [W,b,W2,b2])
    #apply gradients
    optimizer.apply_gradients(zip(gradients, [W,b,W2,b2]))
    losses.append(loss)
    print(loss)

我想你需要一个缓冲区来消除教程中的训练。谢谢你的建议。我尝试了这个,但它并没有解决这个问题。你想做什么伪代码?我试图从上面的链接中准确地再现这个例子:你没有数据输入规范化:uni_data=uni_data.values uni_train_mean=uni_data[:train_SPLIT]。mean()uni_train_std=uni_data[:train_SPLIT]。std()uni_data=(uni_data-uni_train_平均)/统一列车标准