Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import tensorflow as tf
  2. from Data_Handler import dataSubset
  3.  
  4.  
  5. def measure_accuracy(actual, expected):
  6. num_correct = 0
  7. for i in range(len(actual)):
  8. actual_value = actual[i]
  9. expected_value = expected[i]
  10. if actual_value[0] >= actual_value[1] and expected_value[0] >= expected_value[1]:
  11. num_correct += 1
  12. elif actual_value[0] <= actual_value[1] and expected_value[0] <= expected_value[1]:
  13. num_correct += 1
  14. return (num_correct / len(actual)) * 100
  15.  
  16.  
  17. x_train, y_train = dataSubset('AAPL', '20180601', '20181229')
  18. x_test, y_test = dataSubset('AAPL', '20181229', '20190521')
  19.  
  20. x_input = tf.placeholder(dtype=tf.float32, shape=[None, 5], name='x_input')
  21. y_input = tf.placeholder(dtype=tf.float32, shape=[None, 2], name='y_input')
  22.  
  23. W = tf.Variable(initial_value=tf.ones(shape=[5, 2]))
  24. b = tf.Variable(initial_value=tf.ones(shape=[2]))
  25.  
  26. y_output = tf.add(tf.matmul(x_input, W), b, name='y_output')
  27. loss = tf.reduce_sum(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_input, logits=y_output)))
  28.  
  29. optimizer = tf.train.AdagradOptimizer(0.01)
  30. optimizer = optimizer.minimize(loss)
  31.  
  32. saver = tf.train.Saver()
  33. session = tf.Session()
  34. session.run(tf.global_variables_initializer())
  35. tf.train.write_graph(session.graph_def, '.', 'stock_prediction.pbtxt', False)
  36.  
  37. for _ in range(20000):
  38. session.run(optimizer, feed_dict={x_input: x_train, y_input: y_train})
  39.  
  40. saver.save(session, save_path='stock_prediction.ckpt')
  41. print(measure_accuracy(session.run(y_output, feed_dict={x_input: x_test}), y_test))
  42. print(str(measure_accuracy(session.run(y_output, feed_dict={x_input: x_train}), y_train) -
  43. measure_accuracy(session.run(y_output, feed_dict={x_input: x_test}), y_test)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement