Skip to content

Instantly share code, notes, and snippets.

@sfujiwara
Created February 27, 2017 06:46
Show Gist options
  • Save sfujiwara/ebbd15bfb3c2637924e345e9722f1c07 to your computer and use it in GitHub Desktop.
Save sfujiwara/ebbd15bfb3c2637924e345e9722f1c07 to your computer and use it in GitHub Desktop.
predictors = df_train.iloc[:,FEATURE_COLS].values
targets = df_train[TARGET_COL].values
shutil.rmtree('taxi_model', ignore_errors=True) # start fresh each time
modelprefix = 'taxi_model'
with tf.Session() as sess:
npredictors = len(FEATURE_COLS)
noutputs = 1
feature_data = tf.placeholder("float", [None, npredictors])
target_data = tf.placeholder("float", [None, noutputs])
hidden = tf.contrib.layers.stack(feature_data,
tf.contrib.layers.fully_connected,
[64, 8],
activation_fn=tf.nn.relu,
# biases_initializer=tf.ones,
scope='fc')
model = tf.contrib.layers.fully_connected(hidden, 1, activation_fn=None)
cost = tf.nn.l2_loss(model - target_data)
# tf.scalar_summary("cost", cost)
tf.summary.scalar("cost", cost)
training_step = tf.contrib.layers.optimize_loss(cost,
tf.contrib.framework.get_global_step(),
optimizer='Adam', learning_rate=0.1)
# summary_writer = tf.train.SummaryWriter('taxi_model',graph=sess.graph)
summary_writer = tf.summary.FileWriter('taxi_model',graph=sess.graph)
tf.initialize_all_variables().run()
# merged_summary_op = tf.merge_all_summaries()
merged_summary_op = tf.summary.merge_all()
for iter in xrange(0, 1000):
_, trainerr, summary = sess.run([training_step, cost, merged_summary_op], feed_dict = {
feature_data : predictors,
target_data : targets.reshape(len(predictors), noutputs)
})
summary_writer.add_summary(summary)
if (iter%100 == 1):
trmse = np.sqrt(trainerr/len(predictors))
print 'iter={0} train_error={1}'.format(iter, trmse)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment