53 lines
998 B
Python
53 lines
998 B
Python
import numpy as np
|
|
import pandas as pd
|
|
|
|
import utility
|
|
|
|
df = utility.load_data()
|
|
x_data = df.values[:, 1:]
|
|
y_data = df.values[:, 0]
|
|
m, n = x_data.shape
|
|
|
|
|
|
import tensorflow as tf
|
|
x = tf.Variable(x_data)
|
|
y = tf.Variable(y_data)
|
|
w = tf.Variable(tf.zeros((n, 1)))
|
|
|
|
a = 0.001
|
|
|
|
iter = 0
|
|
sum_ = 0
|
|
|
|
print_gap = 1.0e-3
|
|
break_gap = 1.0e-10
|
|
|
|
with tf.Session() as sess:
|
|
sess.run(tf.global_variables_initializer())
|
|
|
|
while True:
|
|
h = tf.matmul(x, w)
|
|
diff = h-tf.reshape(y, h.shape)
|
|
cost = a*tf.reduce_mean(diff**2, axis=0)/2
|
|
gradient = a*tf.matmul(diff, x, True)
|
|
w -= tf.reshape(gradient, w.shape)
|
|
|
|
values = sess.run((h, cost, gradient, tf.reshape(w, [-1])))
|
|
|
|
max__ = max(abs(values[2]))[0]
|
|
sum__ = sum(abs(values[2]))[0]
|
|
sum_ += sum__
|
|
if sum_ >= print_gap:
|
|
print('{}: {} ({})'.format(iter, values[1], values[3]))
|
|
sum_ -= print_gap
|
|
|
|
if max__ < break_gap:
|
|
break
|
|
|
|
iter += 1
|
|
|
|
print('{}: {} ({})'.format(iter, values[1], values[3]))
|
|
print('{}'.format(np.c_[values[0], y_data]))
|
|
|
|
|