我是一个Tensorflow的新手用户,正在编写一个解释线性回归的程序。我有一个输入,即房屋购买的年份(我的程序是一个房价识别器),权重和偏置与Excel的趋势线线性方程相同。不幸的是,最终的成本达到了70。
import tensorflow as tfimport osdir_path = os.path.dirname(os.path.realpath(__file__))filename = dir_path+ "\ActualHousePriceData7.csv"learn_rate=0.001w=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)+ bsquared_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}))
我输出成本的方式是否有问题,还是有其他原因?任何帮助都将非常感谢!
回答:
由于您使用的是squared_loss
,建议使用损失的平均值而不是总和。
cost = tf.reduce_mean(squared_deltas)