Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import numpy as np
  2. from numpy.random import randint
  3. import tensorflow as tf
  4. from pprint import pprint
  5.  
  6. NUM_DATAS = 100
  7. NUM_TESTS = 10
  8. INPUT_DIMENSION = 2
  9. OUTPUT_DIMENSION = 1
  10. OPTIMIZE_RATE = 0.5
  11. TRAIN_TIMES = 1000
  12.  
  13. def createSuperviserData(nDatas):
  14. inData = []
  15. outData = []
  16. for i in range(nDatas):
  17. tmpIn = [
  18. #float(randint(18, 100)),
  19. float(randint(1, 3)),
  20. #float(randint(18, 100)),
  21. float(randint(1, 3)),
  22. ]
  23. if tmpIn[0] == tmpIn[1]:
  24. tmpOut = 0
  25. else:
  26. tmpOut = 1
  27. inData.append(tmpIn)
  28. outData.append(tmpOut)
  29. return [inData, outData]
  30.  
  31. def inference(input_placeholder):
  32. W = tf.Variable(tf.zeros([INPUT_DIMENSION, OUTPUT_DIMENSION]), name='weight')
  33. b = tf.Variable(tf.zeros([OUTPUT_DIMENSION]), name='bias')
  34. y = tf.matmul(input_placeholder, W) + b
  35. return y
  36.  
  37. def loss(logits, labels):
  38. pprint({'logits':logits, 'labels':labels})
  39. labels = tf.to_int64(labels)
  40. xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
  41. logits=logits, labels=labels)
  42. xentropy_mean = tf.reduce_mean(xentropy, name='loss')
  43. return xentropy_mean
  44.  
  45. def training(loss):
  46. optimizer = tf.train.GradientDescentOptimizer(OPTIMIZE_RATE)
  47. train_step = optimizer.minimize(loss, name='training')
  48. return train_step
  49.  
  50. def accuracy(logits, labels):
  51. current_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
  52. accuracy = tf.reduce_mean(tf.cast(current_prediction, tf.float32), name='accuracy')
  53. return accuracy
  54.  
  55. if __name__ == '__main__':
  56. with tf.name_scope('superviser'):
  57. original_input, original_output = createSuperviserData(NUM_DATAS)
  58. test_input, test_output = createSuperviserData(NUM_TESTS)
  59.  
  60. inputs = tf.placeholder(tf.float32, shape=(NUM_DATAS, INPUT_DIMENSION), name='x')
  61. labels = tf.placeholder(tf.int32, shape=(NUM_DATAS), name='y')
  62.  
  63. feed_dict = {inputs: original_input, labels: original_output}
  64. test_feed_dict = {inputs: test_input, labels: test_output}
  65. pprint({'original_input': original_input, 'original_output': original_output,
  66. 'test_input': test_input, 'test_output': test_output})
  67.  
  68. with tf.name_scope('main_op'):
  69. logits = inference(inputs)
  70. loss = loss(logits, labels)
  71. train_op = training(loss)
  72.  
  73. pprint({'inputs':inputs,'labels':labels,'logits':logits,'loss':loss,
  74. 'train_op':train_op,'accuracy':accuracy})
  75.  
  76. sess = tf.InteractiveSession()
  77. tf.global_variables_initializer().run()
  78. print()
  79. for i in range(TRAIN_TIMES):
  80. x, y = createSuperviserData(NUM_DATAS)
  81. sess.run(train_op, feed_dict={inputs:x, labels:y})
  82. if i % (TRAIN_TIMES//10) == 0:
  83. print('loss: %.6f' % sess.run(loss, feed_dict=test_feed_dict))
  84. accuracy = accuracy(logits, labels)
  85. print()
  86. print('loss: %.6f' % sess.run(loss, feed_dict=test_feed_dict))
  87. print('accuracy: %.6f' % sess.run(accuracy, feed_dict=test_feed_dict))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement