-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·92 lines (69 loc) · 3.68 KB
/
model.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
import tensorflow as tf
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# Load training data set from CSV file
training_data_df = pd.read_csv("sales_data_training.csv", dtype=float)
# Pull out columns for X (data to train with) and Y (value to predict)
X_training = training_data_df.drop('total_earnings', axis=1).values
Y_training = training_data_df[['total_earnings']].values
# Load testing data set from CSV file
test_data_df = pd.read_csv("sales_data_test.csv", dtype=float)
# Pull out columns for X (data to train with) and Y (value to predict)
X_testing = test_data_df.drop('total_earnings', axis=1).values
Y_testing = test_data_df[['total_earnings']].values
# All data needs to be scaled to a small range like 0 to 1 for the neural
# network to work well. Create scalers for the inputs and outputs.
X_scaler = MinMaxScaler(feature_range=(0, 1))
Y_scaler = MinMaxScaler(feature_range=(0, 1))
# Scale both the training inputs and outputs
X_scaled_training = X_scaler.fit_transform(X_training)
Y_scaled_training = Y_scaler.fit_transform(Y_training)
# It's very important that the training and test data are scaled with the same scaler.
X_scaled_testing = X_scaler.transform(X_testing)
Y_scaled_testing = Y_scaler.transform(Y_testing)
# Define model parameters
learning_rate = 0.001
training_epochs = 100
display_step = 5
# Define how many inputs and outputs are in our neural network
number_of_inputs = 9
number_of_outputs = 1
# Define how many neurons we want in each layer of our neural network
layer_1_nodes = 50
layer_2_nodes = 100
layer_3_nodes = 50
# Section One: Define the layers of the neural network itself
# Input Layer
with tf.variable_scope('input'):
X = tf.placeholder(tf.float32,shape=(None, number_of_inputs))
# Layer 1
with tf.variable_scope('layer_1'):
weights = tf.get_variable(name="weights1", shape=[number_of_inputs, layer_1_nodes],
initializer=tf.contrib.layers.xavier.initializer)
biases = tf.get_variable(name="biases1", shape=[layer_1_nodes], initializer=tf.zeros_initializer)
layer_1_output = tf.nn.relu.matmul(X, weights) + biases
# Layer 2
with tf.variable_scope('layer_2'):
weights = tf.get_variable(name="weights2", shape=[layer_1_nodes, layer_2_nodes],
initializer=tf.contrib.layers.xavier.initializer)
biases = tf.get_variable(name="biases2", shape=[layer_2_nodes], initializer=tf.zeros_initializer)
layer_2_output = tf.nn.relu.matmul(layer_1_output, weights) + biases
# Layer 3
with tf.variable_scope('layer_3'):
weights = tf.get_variable(name="weights3", shape=[layer_2_nodes, layer_3_nodes],
initializer=tf.contrib.layers.xavier.initializer)
biases = tf.get_variable(name="biases3", shape=[layer_3_nodes], initializer=tf.zeros_initializer)
layer_3_output = tf.nn.relu.matmul(layer_2_output, weights) + biases
# Output Layer
with tf.variable_scope('output'):
weights = tf.get_variable(name="weights4", shape=[layer_3_nodes, number_of_outputs],
initializer=tf.contrib.layers.xavier.initializer)
biases = tf.get_variable(name="biases4", shape=[number_of_outputs], initializer=tf.zeros_initializer)
prediction = tf.nn.relu.matmul(layer_3_output, weights) + biases
# Section Two: Define the cost function of the neural network that will measure prediction accuracy during training
with tf.variable_scope('cost'):
Y = tf.placeholder(tf.float32, shape=(None,1))
cost = tf.reduce_mean(tf.squared_difference(prediction,Y))
# Section Three: Define the optimizer function that will be run to optimize the neural network
with tf.variable_scope('train'):
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)