Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tensorflow as tf
- # initialize variables/model parameters
- W = tf.Variable(tf.zeros([2,1]), name="weights")
- b = tf.Variable(0., name="bias")
- # define the training loop operations
- def inference(X):
- # compute inference model over data X and return the result
- # pass
- return tf.matmul(X, W) + b
- def loss(X, Y):
- # compute loss over training data X and expected output Y
- # pass
- Y_predicted = inference(X)
- return tf.reduce_sum(tf.squared_difference(Y, Y_predicted))
- def inputs():
- # read/generate input training data X and expected output Y
- # pass
- weight_age = [[84,46], [73,20], [65,52], [70,30], [76,57], [69,25], [63,28], [72,36], [79,57], [75,44], [27,24], [89,31], [65,52], [57,23], [59,60], [69,48], [60,34], [79,51], [75,50], [82,34], [59,46], [67,23], [85,37], [55,40]]
- blood_fat_content = [354, 190, 405, 263, 451, 302, 288, 385, 402, 365, 209, 290, 346, 254, 395, 434, 220, 374, 308, 220, 311, 181, 274, 303]
- return tf.to_float(weight_age), tf.to_float(blood_fat_content)
- def train(total_loss):
- # train/adjust model parameters according to computed total loss
- # pass
- learning_rate = 0.0000001
- return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)
- def evaluate(sess, X, Y):
- # evaluate the resulting trained model
- # pass
- print(sess.run(inference([[80.,25.]]))) #~303
- print(sess.run(inference([[63.,30.]]))) #~256
- # launch the graph in a session, setup boilerplate
- with tf.Session() as sess:
- tf.initialize_all_variables().run()
- X, Y = inputs()
- total_loss = loss(X, Y)
- train_op = train(total_loss)
- coord = tf.train.Coordinator()
- threads = tf.train.start_queue_runners(sess=sess, coord=coord)
- # actual training loop
- training_steps = 1000
- for step in range(training_steps):
- sess.run([train_op])
- # for debugging or learning purposes
- if step%10 == 0:
- print("loss: ", sess.run([total_loss]))
- evaluate(sess, X, Y)
- coord.request_stop()
- coord.join(threads)
- sess.close()
Advertisement
Add Comment
Please, Sign In to add comment