-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathtf-linear_regression_restore.py
68 lines (54 loc) · 1.57 KB
/
tf-linear_regression_restore.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import os
import csv
import tensorflow as tf
import numpy as np
import pandas as pd
from util import read_dataset
import argparse
np.set_printoptions(suppress=True)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
np.random.seed(0)
EPOCHES = 10
BATCH_SIZE = 16
learning_rate = 0.0002
# real data
# ######################################## difference from rosettta
file_x = '../dsets/ALL/reg_test_x.csv'
file_y = '../dsets/ALL/reg_test_y.csv'
real_X, real_Y = pd.read_csv(file_x, header=None).to_numpy(
), pd.read_csv(file_y, header=None).to_numpy()
# ######################################## difference from rosettta
DIM_NUM = real_X.shape[1]
X = tf.placeholder(tf.float64, [None, DIM_NUM])
Y = tf.placeholder(tf.float64, [None, 1])
print(X)
print(Y)
# initialize W & b
W = tf.Variable(tf.zeros([DIM_NUM, 1], dtype=tf.float64))
b = tf.Variable(tf.zeros([1], dtype=tf.float64))
print(W)
print(b)
# predict
pred_Y = tf.matmul(X, W) + b
print(pred_Y)
# loss
loss = tf.square(Y - pred_Y)
loss = tf.reduce_mean(loss)
print(loss)
# optimizer
train = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
print(train)
# save
saver = tf.train.Saver(var_list=None, max_to_keep=5, name='v2')
os.makedirs("./log/ckpt0", exist_ok=True)
init = tf.global_variables_initializer()
print(init)
# restore mpc's plain model(P0) and predict
with tf.Session() as sess:
sess.run(init)
if os.path.exists("./log/ckpt0/checkpoint"):
saver.restore(sess, './log/ckpt0/model')
# predict
Y_pred = sess.run(pred_Y, feed_dict={X: real_X, Y: real_Y})
print("Y_pred:", Y_pred)