gpt4 book ai didi

Tensorflow:我的 rnn 总是输出相同的值,rnn 的权重没有经过训练

转载 作者:行者123 更新时间:2023-12-03 23:39:20 25 4
gpt4 key购买 nike

我使用 tensorflow 实现了一个简单的 RNN 模型来学习时间序列数据的可能趋势并预测 future 值。然而,该模型总是在训练后产生相同的值。实际上,它得到的最佳模型是:

y = b.

RNN 结构是:

InputLayer -> BasicRNNCell -> Dense -> OutputLayer

RNN代码:

def RNN(n_timesteps, n_input, n_output, n_units):    
tf.reset_default_graph()
X = tf.placeholder(dtype=tf.float32, shape=[None, n_timesteps, n_input])
cells = [tf.contrib.rnn.BasicRNNCell(num_units=n_units)]
stacked_rnn = tf.contrib.rnn.MultiRNNCell(cells)
stacked_output, states = tf.nn.dynamic_rnn(stacked_rnn, X, dtype=tf.float32)
stacked_output = tf.layers.dense(stacked_output, n_output)
return X, stacked_output

在训练中,n_timesteps=1,n_input=1,n_output=1,n_units=2,learning_rate=0.0000001。损失是通过均方误差计算的。

输入是连续几天的数据序列。输出是输入天数后的数据。

(也许这些都不是很好的设置,但不管我怎么改,结果都差不多。所以我就设置这些,以方便稍后显示。)

我发现这是因为 BasicRNNCell 的权重和偏差没有经过训练。他们从一开始就保持不变。只有 Dense 的权重和偏差在不断变化。所以在训练中,我得到了这样的预测:

一开始:

enter image description here

loss: 1433683500.0
rnn/multi_rnn_cell/cell_0/cell0/kernel:0 [KEEP UNCHANGED]
rnn/multi_rnn_cell/cell_0/cell0/bias:0 [KEEP UNCHANGED]
dense/kernel:0 [CHANGING]
dense/bias:0 [CHANGING]

一段时间后:

enter image description here

loss: 175372340.0
rnn/multi_rnn_cell/cell_0/cell0/kernel:0 [KEEP UNCHANGED]
rnn/multi_rnn_cell/cell_0/cell0/bias:0 [KEEP UNCHANGED]
dense/kernel:0 [CHANGING]
dense/bias:0 [CHANGING]

橙色线表示真实数据,蓝色线表示我的代码的结果。通过训练,蓝线会一直上升,直到模型得到稳定的损失。

所以我怀疑是不是我实现有误,于是生成了一组y = 10x + 5的数据进行测试。这一次,我的模型学习到了正确的结果。

一开始:

enter image description here

最后:

enter image description here

我试过:

  1. 添加更多 BasicRNNCell 和 Dense 层
  2. 将 rnn 单元隐藏 num(n_units) 增加到 128
  3. 将 learning_rate 降低到 1e-10
  4. 将时间步长增加到 60

它们都不起作用。

所以,我的问题是:

  1. 是不是因为我的模型太简单了?但是我觉得我的数据的趋势学起来没有那么复杂。至少像 y = ax + b 这样的东西会产生比 y = b 更小的损失。
  2. 什么可能导致这些结果?
  3. 或者我应该如何进行调试?
  4. 而现在,我加倍也许 BasicRNNCell 没有完全实现,用户应该实现它的一些功能吗?我之前没有使用过 tensorflow。

最佳答案

看来您的网络不适合那种数据,或者从另一个角度来看,您的数据缩放不当。在 split_data 之后添加以下 4 行时,我得到了某种学习行为,类似于 a*x+b 情况

data = read_data(work_dir, input_file)
plot_data(data)
input_data, output_data, n_batches = split_data(data, n_timesteps, n_input, n_output)
# scale input and output data
input_data = input_data-input_data[0]
input_data = input_data/np.max(input_data)*1000
output_data = output_data-output_data[0]
output_data = output_data/np.max(output_data)*1000

enter image description here

关于Tensorflow:我的 rnn 总是输出相同的值,rnn 的权重没有经过训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55177781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com