尽管重量和偏差正确,但损失很大--Tensorflow

时间:2017-09-03 13:16:50

标签: python tensorflow artificial-intelligence

我是tensorflow的初级用户,我正在编写一个解释线性回归的程序。我有一个输入,即购买房屋的那一年(我的程序是房价标识符),重量和偏差与excel的趋势线线性方程相同。不幸的是,费用在70日结束。

import tensorflow as tf
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
filename = dir_path+ "\ActualHousePriceData7.csv"

learn_rate=0.001


w=tf.Variable(([1.00]),tf.float32)
b=tf.Variable(([1.00]),tf.float32)
x= tf.placeholder(tf.float32)
y_=tf.placeholder(tf.float32)


init= tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)


all_x1s = []
all_ys = []

with tf.Session() as sess:
    sess.run( tf.global_variables_initializer())
    with open(filename) as inf:
        # Skip header
        next(inf)
        for line in inf:
            # Read data, using python, into our features
            housenumber, _x1, _y_= line.strip().split(",")
            all_x1s.append(float(_x1))
            all_ys.append(float(_y_))

sess = tf.Session()
sess.run(init)
y_pred = (x*w)+ b
squared_deltas = tf.square(y_ - y_pred)
cost = tf.reduce_sum(squared_deltas)
train_step = tf.train.GradientDescentOptimizer(learn_rate).minimize(cost)

print (all_x1s)
for i in range(10000):
    sess.run(train_step, feed_dict={x:all_x1s ,y_:all_ys})
    print("After %d iteration:" % i)
    print("W: %f" % sess.run(w))
    print("b: %f" % sess.run(b))
    print("Cost")
    print(sess.run(cost,feed_dict={x:all_x1s, y_:all_ys}))

我输出成本的方式是否存在问题,还是其他问题?任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

由于您使用的是squared_loss,因此建议使用损失均值而不是总和。

cost = tf.reduce_mean(squared_deltas)