Created
August 21, 2018 15:28
-
-
Save suhailgupta03/d231c53cc2a4065e279db6ebb646eb45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
import pandas as pd | |
import os | |
from sklearn.preprocessing import MinMaxScaler | |
import os | |
import csv | |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' | |
output_csv = [] | |
# Load training data set from CSV file | |
training_data_df = pd.read_csv('twitter-train.csv') | |
training_data_df = training_data_df.dropna() | |
# Pull out columns for X (data to train with) and Y (value to predict) | |
X_training = training_data_df.drop( | |
['esId','createdDate', 'post', 'peakVelocity', 'estimatedReach', 'viral', 'old_viral'], axis=1).values | |
Y_training = training_data_df[['impact']].values | |
# Load testing data set from CSV file | |
test_data_df = pd.read_csv('twitter-test.csv') | |
test_data_df = test_data_df.dropna() | |
X_testing = test_data_df.drop( | |
['createdDate', 'post', 'peakVelocity', 'estimatedReach', 'viral', 'old_viral'], axis=1).values | |
Y_testing = test_data_df[['impact']].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 training inputs and outputs | |
X_scaled_training = X_scaler.fit_transform(X_training) | |
Y_scaled_training = Y_scaler.fit_transform(Y_training) | |
X_scaled_testing = X_scaler.transform(X_testing) | |
Y_scaled_testing = Y_scaler.transform(Y_testing) | |
# print(X_scaled_testing.shape) | |
# print(Y_scaled_testing.shape) | |
# Define model parameters | |
RUN_NAME = "run 2 with 50 nodes" | |
learning_rate = 0.001 | |
training_epochs = 3000 | |
display_step = 5 | |
# Define how many inputs and outputs are in our neural network | |
number_of_inputs = 13 | |
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 1: Define the layers of thne 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(tf.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(tf.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(tf.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.matmul(layer_3_output, weights) + biases | |
# Section 2: | |
with tf.variable_scope('cost'): | |
Y = tf.placeholder(tf.float32, shape=(None, 1)) | |
cost = tf.reduce_mean(tf.squared_difference(prediction, Y)) | |
# Section 3: | |
with tf.variable_scope('train'): | |
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost) | |
with tf.variable_scope('logging'): | |
tf.summary.scalar('current_cost', cost) | |
tf.summary.histogram('predicted_value', prediction) | |
summary = tf.summary.merge_all() | |
saver = tf.train.Saver() | |
# Initialize session so that we can run TF operations | |
with tf.Session() as session: | |
# Run the global variable initializer to initialize all variables and layers | |
session.run(tf.global_variables_initializer()) | |
training_writer = tf.summary.FileWriter( | |
"./logs/{}/training".format(RUN_NAME), session.graph) | |
testing_writer = tf.summary.FileWriter( | |
"./logs/{}/testing".format(RUN_NAME), session.graph) | |
# Run the optimizer over and over to train the network | |
# One epoch is one full run through the training data set | |
for epoch in range(training_epochs): | |
# Feed in the training data and do one step of neural training | |
session.run(optimizer, feed_dict={ | |
X: X_scaled_training, Y: Y_scaled_training}) | |
if epoch % 5 == 0: | |
training_cost, training_summary = session.run( | |
[cost, summary], feed_dict={X: X_scaled_training, Y: Y_scaled_training}) | |
testing_cost, testing_summary = session.run( | |
[cost, summary], feed_dict={X: X_scaled_testing, Y: Y_scaled_testing}) | |
training_writer.add_summary(training_summary, epoch) | |
testing_writer.add_summary(testing_summary, epoch) | |
print("Epoch: {} Training Cost: {} Testing Cost: {}".format( | |
epoch, training_cost, testing_cost)) | |
# Training is now complete! | |
print("Training is complete") | |
final_training_cost = session.run( | |
cost, feed_dict={X: X_scaled_training, Y: Y_scaled_training}) | |
final_testing_cost = session.run( | |
cost, feed_dict={X: X_scaled_testing, Y: Y_scaled_testing}) | |
print("Final training cost : {}".format(final_training_cost)) | |
print("Final testing cost : {}".format(final_testing_cost)) | |
save_path = saver.save(session, "logs/trained_model.ckpt") | |
# print('Model saved') | |
# Now that the neural network is trained, let's use it to make predictions for our test data. | |
# Pass in the X testing data and run the "prediciton" operation | |
Y_predicted_scaled = session.run( | |
prediction, feed_dict={X: X_scaled_testing}) | |
# Unscale the data back to it's original units (dollars) | |
Y_predicted = Y_scaler.inverse_transform(Y_predicted_scaled) | |
for idx, value in enumerate(Y_predicted): | |
output_csv.append({ | |
'original': test_data_df['impact'].values[idx], | |
'predicted': Y_predicted[idx][0], | |
'post': test_data_df['post'].values[idx] | |
}) | |
real_impact = test_data_df['impact'].values[0] | |
predicted_impact = Y_predicted[0][0] | |
print("The actual impact was {}".format(real_impact)) | |
print("Our neural network predicted impact of {}".format(predicted_impact)) | |
print("Creating CSV ..") | |
with open('comparison.csv', 'w') as output: | |
writer = csv.DictWriter(output, output_csv[0].keys()) | |
writer.writeheader() | |
writer.writerows(output_csv) | |
print("\ncreated comparison.csv") | |
model_builder = tf.saved_model.builder.SavedModelBuilder("exported_model") | |
inputs = { | |
'input': tf.saved_model.utils.build_tensor_info(X) | |
} | |
outputs = { | |
'earnings': tf.saved_model.utils.build_tensor_info(prediction) | |
} | |
signature_def = tf.saved_model.signature_def_utils.build_signature_def( | |
inputs=inputs, | |
outputs=outputs, | |
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME | |
) | |
model_builder.add_meta_graph_and_variables( | |
session, | |
tags=[tf.saved_model.tag_constants.SERVING], | |
signature_def_map={ | |
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def | |
} | |
) | |
model_builder.save() | |
# | |
# tensorflow_model_server --port=4000 --model_name=mymodel --model_base_path=/home/suhail/tensorflow-stubs/exported_model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment