【问题标题】:How to fix TensorFlow Linear Regression no change in MSE?如何修复 TensorFlow 线性回归在 MSE 中没有变化?
【发布时间】:2019-06-03 20:14:50
【问题描述】:

我正在研究一个简单的线性回归模型来预测系列的下一步。我给它 x/y 坐标数据,我希望回归器预测图上下一个点的位置。

我在 AdamOptmizer 中使用密集层,并将我的损失函数设置为:

tf.reduce_mean(tf.square(layer_out - y))

我正在尝试从头开始创建线性回归模型(我不想在这里使用 TF 估计器包)。

我已经看到了通过手动指定权重和偏差来做到这一点的方法,但没有任何方法进入深度回归。

    X = tf.placeholder(tf.float32, [None, self.data_class.batch_size, self.inputs])
    y = tf.placeholder(tf.float32, [None, self.data_class.batch_size, self.outputs])
    layer_input = tf.layers.dense(inputs=X, units=10, activation=tf.nn.relu)
    layer_hidden = tf.layers.dense(inputs=layer_input, units=10, activation=tf.nn.relu)
    layer_out = tf.layers.dense(inputs=layer_hidden, units=1, activation=tf.nn.relu)
    cost = tf.reduce_mean(tf.square(layer_out - y))
    optmizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
    training_op = optmizer.minimize(cost)

    init = tf.initialize_all_variables()
    iterations = 10000
    with tf.Session() as sess:
        init.run()
        for iteration in range(iterations):
            X_batch, y_batch = self.data_class.get_data_batch()
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
            if iteration % 100 == 0:
                mse = cost.eval(feed_dict={X:X_batch, y:y_batch})
                print(mse)
        array = []
        for i in range(len(self.data_class.dates), (len(self.data_class.dates)+self.data_class.batch_size)):
            array.append(i)
        x_pred = np.array(array).reshape(1, self.data_class.batch_size, 1)
        y_pred = sess.run(layer_out, feed_dict={X: x_pred})
        print(y_pred)
        predicted = np.array(y_pred).reshape(self.data_class.batch_size)
        predicted = np.insert(predicted, 0, self.data_class.prices[0], axis=0)
        plt.plot(self.data_class.dates, self.data_class.prices)
        array = [self.data_class.dates[0]]
        for i in range(len(self.data_class.dates), (len(self.data_class.dates)+self.data_class.batch_size)):
            array.append(i)

        plt.plot(array, predicted)
        plt.show()

当我进行训练时,我一遍又一遍地得到相同的损失值。

它没有像它应该的那样被减少,为什么?

【问题讨论】:

  • 数据是什么?评估结果如何?到目前为止,您取得了什么成就?不清楚是什么问题。
  • 数据只是X/Y坐标数据。评估只是一次又一次地返回与 MSE 相同的损失和相同的数字。基本上我给它一个 X 值并试图让它预测相应的 Y 值。
  • 会不会是模型的架构?
  • 您确定在模型中提供了正确的数据格式吗?占位符形状为 (none, batch_size, input)。
  • 是的,我的批处理方法如下:``` def get_data_batch(self): start = (self.current_batch * self.batch_size) end = (self.current_batch+1) * self.batch_size return np.array(self.x[start: end]).reshape(1, self.batch_size, 1), np.array(self.y[start: end]).reshape(1, self.batch_size, 1)` `` 我会确保根据需要重塑数据。

标签: python tensorflow deep-learning training-data


【解决方案1】:

问题是我正在对输出层应用激活。这导致该输出转到它激活的任何位置。

通过在最后一层指定activation=None,深度回归按预期工作。

这是更新后的架构:

layer_input = tf.layers.dense(inputs=X, units=150, activation=tf.nn.relu)
    layer_hidden = tf.layers.dense(inputs=layer_input, units=100, activation=tf.nn.relu)
    layer_out = tf.layers.dense(inputs=layer_hidden, units=1, activation=None)

【讨论】:

    猜你喜欢
    • 2019-06-29
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2018-02-21
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多