Instantly share code, notes, and snippets.

Embed
What would you like to do?
tensorFlow sample, for IoT data
import tensorflow as tf
if __name__ == "__main__":
# Model parameters
W = tf.Variable([0.0], dtype=tf.float32)
b = tf.Variable([0.0], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
linear_model = W*x + b
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [0.01, 0.02 , 0.03, 0.04, 0.05 ]
y_train = [0.13, 0.12 , 0.06, 0.11, 0.13 ]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x: x_train, y: y_train})
if i % 100 == 0:
print( i, sess.run(W), sess.run(b) )
# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment