简体   繁体   English

如何修复TensorFlow线性回归在MSE中没有变化?

[英]How to fix TensorFlow Linear Regression no change in MSE?

I'm working on a simple linear regression model to predict the next step in a series. 我正在研究一个简单的线性回归模型,以预测系列中的下一步。 I'm giving it x/y coordinate data and I want the regressor to predict where the next point on the plot will lie. 我正在为其提供x / y坐标数据,并且我希望回归器预测绘图中下一个点的位置。

I'm using dense layers with AdamOptmizer and have my loss function set to: 我在AdamOptmizer中使用密集层,并将损失函数设置为:

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

I'm trying to create linear regression models from scratch (I don't want to utilize the TF estimator package here). 我正在尝试从头开始创建线性回归模型(我不想在这里利用TF估计器包)。

I've seen ways to do it by manually specifying weights and biases, but nothing goes into deep regression. 我已经看到了通过手动指定权重和偏差来做到这一点的方法,但是没有深入的回归。

    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()

When I run training I'm getting the same loss value over and over again. 当我进行训练时,我一次又一次地得到相同的损失值。

It's not being reduced, like it should, why? 它没有像应该那样减少,为什么呢?

The issue is that I'm applying an activation to the output layer. 问题是我正在将激活应用于输出层。 This is causing that output to go to whatever it activates to. 这导致该输出转到其激活到的任何位置。

By specifying in the last layer that activation=None the deep regression works as intended. 通过在最后一层中指定Activation = None,深度回归将按预期工作。

Here is the updated architecture: 这是更新的体系结构:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM