Files
regressions/linear_regression4.py
2018-08-17 17:32:18 +09:00

53 lines
1000 B
Python

import numpy as np
import pandas as pd
import utility
df = utility.Loader.load_data()
x_data = df.values[:, 1:]
y_data = df.values[:, 0]
m, n = x_data.shape
import tensorflow as tf
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
feed = {x: x_data, y: y_data.reshape((m, 1))}
w = tf.Variable(tf.zeros((n, 1)))
a = tf.Variable(0.0002)
h = tf.matmul(x, w)
cost = tf.reduce_mean(tf.square(h-y))
opt = tf.train.GradientDescentOptimizer(a)
train = opt.minimize(cost)
iter = 0
sum_ = 0
stepper = utility.Stepper()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
while True:
w1 = sess.run(w)
sess.run(train, feed_dict=feed)
stepper.add_step(sess.run(cost, feed))
if stepper.is_print_turn():
print('{}: {}'.format(iter, sess.run(cost, feed_dict=feed)))
if stepper.is_break_turn():
break
iter += 1
h_v = sess.run(h, feed_dict=feed)
print('{}: {}'.format(iter, sess.run(cost, feed_dict=feed)))
print('{}'.format(np.c_[h_v, y_data]))