【问题标题】:Getting very high values in linear regression在线性回归中获得非常高的值
【发布时间】:2016-04-17 11:55:59
【问题描述】:

我正在尝试制作一个简单的 MLP 来预测图像像素的值 - original blog。 这是我之前在 python 中使用 Keras 的尝试 - link

我尝试在 tensorflow 中做同样的事情,但是当它们应该小于 1 时,我得到了非常大的输出值 (~10^12)。

这是我的代码:

import numpy as np
import cv2
from random import shuffle
import tensorflow as tf

'''
Image preprocessing
'''
image_file = cv2.imread("Mona Lisa.jpg")

h = image_file.shape[0]
w = image_file.shape[1]

preX = []
preY = []

for i in xrange(h):
    for j in xrange(w):
        preX.append([i,j])
        preY.append(image_file[i,j,:].astype('float32')/255.0)

print preX[:5], preY[:5]
zipped = [i for i in zip(preX,preY)]
shuffle(zipped)

X_train = np.array([i for (i,j) in zipped]).astype('float32')
Y_train = np.array([j for (i,j) in zipped]).astype('float32')

print X_train[:10], Y_train[:10]

'''
Tensorflow code
'''

def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)

x = tf.placeholder(tf.float32, shape=[None,2])
y = tf.placeholder(tf.float32, shape=[None,3])



'''
Layers
'''

w1 = weight_variable([2,300])
b1 = bias_variable([300])
L1 = tf.nn.relu(tf.matmul(X_train,w1)+b1)

w2 = weight_variable([300,3])
b2 = bias_variable([3])
y_model = tf.matmul(L1,w2)+b2


'''
Training
'''

# criterion
MSE = tf.reduce_mean(tf.square(tf.sub(y,y_model)))

# trainer
train_op = tf.train.GradientDescentOptimizer(learning_rate = 0.01).minimize(MSE)

nb_epochs = 10

init = tf.initialize_all_variables()
sess = tf.Session()

sess.run(init)
cost = 0

for i in range(nb_epochs):
    sess.run(train_op, feed_dict ={x: X_train, y: Y_train})
    cost += sess.run(MSE, feed_dict ={x: X_train, y: Y_train})

cost /= nb_epochs
print cost


'''
Prediction
'''

pred = sess.run(y_model,feed_dict = {x:X_train})*255.0
print pred[:10]

output_image = []
index = 0

h = image_file.shape[0]
w = image_file.shape[1]

for i in xrange(h):
    row = []

    for j in xrange(w):
        row.append(pred[index])
        index += 1

    row = np.array(row)
    output_image.append(row)

output_image = np.array(output_image)
output_image = output_image.astype('uint8')
cv2.imwrite('out_mona_300x3_tf.png',output_image)

【问题讨论】:

  • 数值在 10^12 到 10^22 之间波动。

标签: python tensorflow linear-regression


【解决方案1】:

首先,我认为不是先运行 train_op 再运行 MSE 您可以在列表中运行这两个操作并显着降低计算成本。

for i in range(nb_epochs):
cost += sess.run([MSE, train_op], feed_dict ={x: X_train, y: Y_train})

其次,我建议始终写出您的成本函数,以便您可以了解在训练阶段发生了什么。手动打印出来或使用 tensorboard 记录您的成本并绘制它(您可以在官方 tf 页面上找到示例)。 您还可以监控您的体重,看看它们没有爆炸。

您可以尝试一些事情: 降低学习率,为权重添加正则化。 检查您的训练集(像素)是否真的包含 你希望他们这样做。

【讨论】:

  • 是的,训练集是正确的(我在图像预处理步骤中有一个打印语句)。我可以看到每次迭代的成本从 10^9 增加到 10^21。但我似乎无法找到问题所在。我的错误函数看起来不错。
  • 那么您是否尝试降低学习率和/或降低权重随机初始化时的标准差?
  • 是的,将 std 减少到 0.01 现在可以获得有效输出。但是网络不会学习输入(仍然显示为随机噪声)。一般情况下是否有必要进行这种调整?
  • 这是 3 个隐藏层(每个 300 个)进行 200 次迭代的训练输出 - (i.imgur.com/g0C3pRk.png)。将其与 Keras 中类似网络的输出进行比较 - i.imgur.com/1hjiZT8.png
  • 这种调整在做这类事情时通常是必不可少的。我不确定你为什么会收到随机噪音。可能有很多原因。打印出每次迭代的成本,以便您了解发生了什么。没有它,你几乎是瞎子。
【解决方案2】:

您将输入层权重和输出层权重命名为 wb,因此梯度下降过程似乎出了点问题。实际上我很惊讶 tensorflow 没有发出错误或至少发出警告(或者我错过了什么?)

【讨论】:

  • 不,我尝试为变量赋予不同的名称。没有帮助。无论如何我都会编辑我的代码。
猜你喜欢
  • 2017-11-26
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2019-03-04
  • 2017-04-20
  • 2021-12-15
  • 2017-06-17
  • 2011-12-09
相关资源
最近更新 更多