在线性回归中得到很高的值

2024-05-19 02:27:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图制作一个简单的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)

Tags: inimageimportforoutputtfnptrain
2条回答

首先,我认为与其运行列车,然后运行MSE 您可以在一个列表中运行这两个操作,并显著降低计算成本。你知道吗

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

其次,我建议你经常写下你的成本函数,这样你就可以看到在培训阶段发生了什么。要么手工打印出来,要么用tensorboard记录成本并绘制出来(你可以在官方tf页面上找到例子)。 你也可以监测你的体重,看看他们没有爆炸。你知道吗

您可以尝试以下几点: 降低学习率,增加权重的正则化。 检查训练集(像素)是否真的包含 你希望他们这样做。你知道吗

输入层权重和输出层权重的名称wb相同,因此在梯度下降过程中似乎出现了问题。事实上,我很惊讶tensorflow没有发出错误或警告(或者我遗漏了什么?)你知道吗

相关问题 更多 >

    热门问题