-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneuralAbalone.py
More file actions
150 lines (116 loc) · 4.97 KB
/
neuralAbalone.py
File metadata and controls
150 lines (116 loc) · 4.97 KB
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
150
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""DNNRegressor with custom estimator for abalone dataset."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import tempfile
from six.moves import urllib
import numpy as np
import tensorflow as tf
from gene import Gene
class NeuralAbalone():
def __init__(self , gene):
self.LEARNING_RATE = gene.LEARNING_RATE
self.STEPS = gene.STEPS
self.UNITS = gene.UNITS
self.SET_OF_FEATURES = [0,1,2,3,4,5,6] # always all
def model_fn(self, features, labels, mode, params):
"""Model function for Estimator."""
# Connect the first hidden layer to input layer
# (features["x"]) with relu activation
myLayer = tf.layers.dense(features["x"], self.UNITS[0], activation=tf.nn.relu)
levels = len( self.UNITS )
for k in range( 1 , (levels - 1) ):
myLayer = tf.layers.dense(
myLayer, self.UNITS[k], activation=tf.nn.relu)
# Connect the output layer to last hidden layer (no activation fn)
output_layer = tf.layers.dense(myLayer, 1)
# Reshape output layer to 1-dim Tensor to return predictions
predictions = tf.reshape(output_layer, [-1])
# Provide an estimator spec for `ModeKeys.PREDICT`.
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(
mode=mode,
predictions={"ages": predictions})
# Calculate loss using mean squared error
loss = tf.losses.mean_squared_error(labels, predictions)
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=params["learning_rate"])
train_op = optimizer.minimize(
loss=loss, global_step=tf.train.get_global_step())
# Calculate root mean squared error as additional eval metric
eval_metric_ops = {
"rmse": tf.metrics.root_mean_squared_error(
tf.cast(labels, tf.float64), predictions)
}
# Provide an estimator spec for `ModeKeys.EVAL` and `ModeKeys.TRAIN` modes.
return tf.estimator.EstimatorSpec(
mode=mode,
loss=loss,
train_op=train_op,
eval_metric_ops=eval_metric_ops)
def analyzeAbalones( self, verbose ):
# Load datasets
path = "/home/andrea/Desktop/python/tensorflowTutorialGenerical/"
abalone_train = path + "training"
abalone_test = path + "test"
abalone_predict = path + "toPredictWithElder"
# Training examples
training_set = tf.contrib.learn.datasets.base.load_csv_without_header(
filename=abalone_train, target_dtype=np.int, features_dtype=np.float64)
training_set_to_use = training_set.data[:, self.SET_OF_FEATURES ]
# Test examples
test_set = tf.contrib.learn.datasets.base.load_csv_without_header(
filename=abalone_test, target_dtype=np.int, features_dtype=np.float64)
test_set_to_use = test_set.data[:, self.SET_OF_FEATURES]
# Set of 7 examples for which to predict abalone ages
prediction_set = tf.contrib.learn.datasets.base.load_csv_without_header(
filename=abalone_predict, target_dtype=np.int, features_dtype=np.float64)
prediction_set_to_use = prediction_set.data[:, self.SET_OF_FEATURES]
# Set model params
model_params1 = {"learning_rate": self.LEARNING_RATE}
# Instantiate Estimator
nn = tf.estimator.Estimator(model_fn=self.model_fn, params=model_params1)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array(training_set_to_use)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True)
# Train
nn.train(input_fn=train_input_fn, steps=self.STEPS)
# Score accuracy
test_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array(test_set_to_use)},
y=np.array(test_set.target),
num_epochs=1,
shuffle=False)
ev = nn.evaluate(input_fn=test_input_fn)
#print("Loss: %s" % ev["loss"])
#print("Root Mean Squared Error: %s" % ev["rmse"])
# Print out predictions
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": prediction_set_to_use},
num_epochs=1,
shuffle=False)
predictions = nn.predict(input_fn=predict_input_fn)
if( verbose == 1):
for i, p in enumerate(predictions):
print("Prediction %s: %s ----- real was: %s" % (i + 1, p["ages"] , prediction_set[1][i]) )
return ev["loss"]
def run(self , verbose):
loss = self.analyzeAbalones( verbose )
return loss