1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#!/usr/bin/python3
from sklearn import datasets
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split
import numpy as np
import tensorflow as tf
dataset = datasets.load_boston()
data, target = dataset.data, dataset.target
data_scaler = preprocessing.MinMaxScaler()
target_scaler = preprocessing.MinMaxScaler()
data = data_scaler.fit_transform(data)
target = target_scaler.fit_transform(target)
np.random.seed(0)
x_train, x_test, y_train, y_test = train_test_split(
data, target, train_size=0.85
)
x = tf.placeholder(tf.float32, [None, 13], name="input")
W = tf.Variable(tf.zeros([13, 1]), name="weight")
b = tf.Variable(tf.zeros([1]), name="bias")
y = tf.add(tf.matmul(x, W), b)
y_ = tf.placeholder(tf.float32, name="target")
rmsle = tf.reduce_mean(tf.sqrt(tf.log(y_) - tf.log(y)))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(rmsle)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
last = 0
batchsize = 50
next_last = last + batchsize
while next_last < len(x_train):
print("running next batch")
next_last = last + batchsize
if next_last > len(x_train):
next_last = len(x_train)
xt = x_train[last:next_last]
yt = y_train[last:next_last]
sess.run(train_step, feed_dict={x: xt, y_: yt})
last = next_last
print("finished training")
diff = tf.sub(y, y_)
print(sess.run(diff, feed_dict={x: x_test, y_: y_test}))
|