Skip to content

Instantly share code, notes, and snippets.

@risenW
Last active July 17, 2018 13:25
Show Gist options
  • Save risenW/8ae302c4ad9180ceb92ea2eb0e6ebc38 to your computer and use it in GitHub Desktop.
Save risenW/8ae302c4ad9180ceb92ea2eb0e6ebc38 to your computer and use it in GitHub Desktop.
code for stochastic training on python using Tensorflow
#Load our libraries
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
sess = tf.Session()
# We create our data, placeholders and variables
x_val = np.random.normal(1, 0.1, 100) #input values
y_val = np.repeat(10., 100) #target values
# placeholders
x_data = tf.placeholder(shape=[1], dtype=tf.float32)
y_target = tf.placeholder(shape=[1], dtype=tf.float32)
#Variables
w = tf.Variable(0.)
# Add linear function to the computational graph
y_pred = tf.multiply(x_data, w)
# Add a loss function(L2 norm)
loss = tf.square(y_pred - y_target)
# initialize our variables
init = tf.global_variables_initializer()
sess.run(init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment