-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDNN_regressor.py
114 lines (81 loc) · 3.6 KB
/
DNN_regressor.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from __future__ import division
from __future__ import print_function
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
import tensorflow as tf
import itertools
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pylab import rcParams
import matplotlib
tf.logging.set_verbosity(tf.logging.INFO)
sess = tf.InteractiveSession()
train = pd.read_pickle('all_design.pkl')#Cotaining all training data
print('Shape of the train data with all features:', train.shape)
print("")
print('Shape of the train data with numerical features:', train.shape)
print("")
print("List of features contained our dataset:",list(train.columns))
#%%
col_train = list(train.columns)
col_train_bis = list(train.columns)
col_train_bis.remove('R')
col_train_bis.remove('T')
col_train_bis.remove('A')
mat_train = np.matrix(train.drop(['R','T','A'],axis =1))
COLUMNS = col_train
FEATURES = col_train_bis
LABEL = ['R','T','A']
# Columns for tensorflow
feature_cols = [tf.contrib.layers.real_valued_column(k) for k in FEATURES]
train=(train-train.mean())/train.std()#Normalize the data
#Subset into train and test data
#indices = pd.DataFrame(np.random.randn(train))
msk = np.random.choice(range(len(train)),int(0.2*len(train)))
test = train.iloc[msk]
train.drop(msk)
regressor = tf.estimator.DNNRegressor(feature_columns=feature_cols,
activation_fn = tf.nn.relu, hidden_units=[500, 400, 200, 100, 50, 25],optimizer = tf.train.AdamOptimizer(learning_rate= 0.001),
label_dimension=3, model_dir='/modelcheckpoint')
#hidden_units=[400, 200, 100, 50, 25, 50]
def input_fn(pred = False, batch_size = 256):
if pred == False:
indices = np.random.choice(range(len(train)), batch_size)#select random indices for minibatch
feature_cols = {k: tf.constant(train.iloc[indices][k].values) for k in FEATURES}
labels = tf.constant(train.iloc[indices][LABEL].values)
return feature_cols, labels
#Input function for testing
def input_test():
test_set_size = 100
indices = np.random.choice(range(len(test)), test_set_size)
feature_cols = {k: tf.constant(test.iloc[indices][k].values) for k in FEATURES}
return feature_cols
#%%Mini Batch training
epochs = 10
#batch_size =128
init_op = tf.global_variables_initializer()
logs_path = r'D:\Yale_Course\Deep Learning Theory Application\Deep-Learning-Group-Project-Monophonic-design-of-layered-structure\tensor_board_test'
#saver = tf.train.Saver()
with tf.Session() as sess:
#writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())
for e in range(epochs):
regress_res = regressor.train(input_fn = input_fn, steps=1000)
ev = regressor.evaluate(input_fn = input_test, steps=1)#Evaluate the model every epoch
#save_path = saver.save(sess, logs_path)
#%%Prediction Mode
input_fn_pred = input_fn(pred = True)
with tf.Session() as sess:
y = regressor.predict(input_fn = lambda: input_fn(pred = True))
predictions = list(itertools.islice(y, 6))
print("Predictions: {}".format(str(predictions)))
sess.close()
#regressor.fit(input_fn=lambda: input_fn(training_set), steps=2000)
#ev = regressor.evaluate(input_fn=lambda: input_fn(testing_set), steps=1)
#
## 0.002X in average
#loss_score1 = ev["loss"]
#print("Final Loss on the testing set: {0:f}".format(loss_score1))
#y = regressor.predict(input_fn=lambda: input_fn(testing_set))
#predictions = list(itertools.islice(y, testing_set.shape[0]))
#predictions = prepro_y.inverse_transform(np.array(predictions).reshape(110,1))