Skip to content

Instantly share code, notes, and snippets.

@risenW
Created July 17, 2018 13:26
Show Gist options
  • Save risenW/ab4b8a573788ce567f74db7d06506ff2 to your computer and use it in GitHub Desktop.
Save risenW/ab4b8a573788ce567f74db7d06506ff2 to your computer and use it in GitHub Desktop.
end code for stochastic training on python using Tensorflow
# Declare an optimizer: here i use gradient descent
my_opt = tf.train.GradientDescentOptimizer(learning_rate=0.02)
#Create the train step
train_step = my_opt.minimize(loss)
n_iterations = 100
loss_stochastic = []
for i in range(n_iterations):
rand_index = np.random.choice(100)
rand_x = [x_val[rand_index]]
rand_y = [y_val[rand_index]]
#Run the graph
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
#Print the result after 5 intervals
if(i+1) % 5 == 0:
print('Step #', str(i+1), 'W = ', str(sess.run(W_st)))
temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
loss_stochastic.append(temp_loss)
print('Loss = ', temp_loss)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment