-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.2.dl_tf_basic_regression.py
149 lines (114 loc) · 5.54 KB
/
1.2.dl_tf_basic_regression.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#%% Building Regression with Premade Estimators
import pandas as pd
import numpy as np
import tensorflow as tf
import itertools
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import gc; gc.enable()
os.chdir("D:\\trainings\\tensorflow")
exec(open(os.path.abspath('tf_CommonUtils.py')).read())
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
# fix random seed for reproducibility
seed = 123; np.random.seed(seed); tf.random.set_seed(seed)
#read data. Details is at https://www.cs.toronto.edu/~delve/data/boston/bostonDetail.html
data = pd.read_csv("./data/boston.csv")
data.shape
data.dtypes
data.head(2)
data.info()
print(data.describe()) # .unstack()
#print(data.describe(include = [np.number])) # for number only
# identifications of features and response
FEATURES = ["crim", "zn", "indus", "nox", "rm", "age", "dis", "tax", "ptratio"]
LABEL = "medv"
batch_size = 8
#Segragate 85% and 15%
training_set ,test_set = train_test_split(data,test_size=0.15)
del(data)
#An input function returns a tf.data.Dataset object which contains features (dictionary -
#with key (feature name) and value (feature's values)
#label - An array containing the values of the label for every row.
#Building the input_fn: regressor accepts Tensors and custom function to convert pandas
#Dataframe and return feature column and label values as Tensors:
def input_fn(features, labels = None, custom_batch_size = batch_size, caller_source = 'train'):
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices(dict(features))
if caller_source != 'test':
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
if caller_source == 'train': #test
dataset = dataset.shuffle(len(features)).repeat()
dataset = dataset.batch(custom_batch_size)
return dataset
#Feature column describs how the model should use raw input data from the features dictionary.
#All features in data set contain continuous values, hence create their FeatureColumn
#Defining FeatureColumns and Creating the Regressor
feature_cols = [tf.feature_column.numeric_column(k) for k in FEATURES]
# Clean model folder from previous models as new model will overide or append depending upon settings
model_dir="./log/boston_model/"; # remove_nonempty_folder(model_dir); os.makedirs(model_dir)
# instantiate a DNNRegressor for the neural network regression model
regressor = tf.estimator.DNNRegressor(feature_columns=feature_cols, hidden_units=[20, 20], model_dir=model_dir)
#Training the Regressor
regressor.train(input_fn=lambda: input_fn(training_set[FEATURES], training_set[LABEL],custom_batch_size = batch_size), steps=5000)
#Evaluating the Model
ev = regressor.evaluate(input_fn=lambda: input_fn(test_set[FEATURES], test_set[LABEL],custom_batch_size = batch_size, caller_source = 'eval'))
#Retrieve the loss from the ev results and print it to output:
loss_score = ev["loss"]
print("Loss: {0:f}".format(loss_score))
#Making Predictions
y = regressor.predict(input_fn=lambda: input_fn(test_set[FEATURES], None, custom_batch_size = batch_size, caller_source = 'test'))
# .predict() returns an iterator; convert to a list and print predictions
predictions = list(pred_tensor["predictions"][0] for pred_tensor in itertools.islice(y, test_set.shape[0]))
print ("Predictions: {}".format(str(predictions)))
#RMSE
rmse = np.sqrt(mean_squared_error(test_set[LABEL], predictions))
print(rmse) # batch 8: 9.75
# To view tensorboard, go to anaconda prompt -> respective model_dir
# tensorboard --logdir=.
# Cleaning
del(training_set, test_set, predictions, feature_cols, ev, loss_score, rmse, model_dir, batch_size); gc.collect()
#%% Regression by using tf.keras model layers
# Restart the Spyder
import pandas as pd
import numpy as np
import tensorflow as tf
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import gc; gc.enable()
os.chdir("D:\\trainings\\tensorflow")
#exec(open(os.path.abspath('tf_CommonUtils.py')).read())
# identifications of features and response
FEATURES = ["crim", "zn", "indus", "nox", "rm", "age", "dis", "tax", "ptratio"]
LABEL = "medv"
# fix random seed for reproducibility
seed = 123; np.random.seed(seed); tf.compat.v1.set_random_seed(seed)
# load dataset
data = pd.read_csv("./data/boston.csv")
data.shape
data.dtypes
data.head(2)
data.info()
print(data.describe()) # .unstack()
#print(data.describe(include = [np.number])) # for number only
#Segragate 85% and 15%
training_set ,test_set = train_test_split(data,test_size=0.15)
del(data)
# Build the model
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(len(FEATURES), input_shape=(len(FEATURES),), activation=tf.nn.relu)) # , kernel_initializer = tf.random_normal_initializer
model.add(tf.keras.layers.Dense(1)) # , kernel_initializer = tf.random_normal_initializer
#Compile
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mean_squared_error'])
model.summary()
# Train it
model.fit(training_set[FEATURES].values, training_set[LABEL].values, epochs=100,batch_size=8, shuffle=True)
# Evaluate on test data
model.evaluate(test_set[FEATURES].values, test_set[LABEL].values) # [81, 81]
#Making Predictions
predictions = model.predict(x=test_set[FEATURES].values, verbose=1)
predictions = np.ravel(predictions)
#RMSE
rmse = np.sqrt(mean_squared_error(test_set[LABEL].values, predictions))
print("RMSE: ", rmse) # 7.59