Guest User

Untitled

a guest
Jan 17th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. '''Imports'''
  2. import csv
  3. import numpy as np
  4. import tensorflow as tf
  5. import tensorflow.keras as keras
  6. np.set_printoptions(suppress=True)
  7.  
  8. ''' Here we unpack the .csv files. I have chosen to put their contents
  9. into lists. Do let me know if there exists a more efficient method. '''
  10.  
  11. distribution_train = []
  12. probs_train = []
  13. distribution_test = []
  14. probs_test = []
  15.  
  16. with open('training_sample.csv') as csv_file:
  17. csv_reader = csv.reader(csv_file, delimiter=',')
  18.  
  19. for row in csv_reader:
  20. distribution_train.append(row[0])
  21. probs_train.append(row[1])
  22.  
  23. with open('testing_sample.csv') as csv_file_1:
  24. csv_reader_1 = csv.reader(csv_file_1, delimiter= ',')
  25.  
  26. for row in csv_reader_1:
  27. distribution_test.append(row[0])
  28. probs_test.append(row[1])
  29.  
  30.  
  31. '''Get rid of the titles in the training_sample.csv file.'''
  32. distribution_train.pop(0)
  33. probs_train.pop(0)
  34.  
  35. '''For some reason everything in my csv file is stored as strings. Or
  36. maybe it's just because of the way I have unpacked it. The below function
  37. is to convert it into floats so that TF can work with it.
  38. It's crude, but it locates all the numbers and appends them to a list,
  39. which then gets appended to a giant list called f.'''
  40.  
  41. def num_converter_flatten(csv_list):
  42. f = []
  43. for j in range(len(csv_list)):
  44. append_this = []
  45. for i in csv_list[j]:
  46. if i == '1' or i == '2' or i == '3' or i == '4' or i == '5' or i == '6' or i == '7' or i == '8' or i =='9' or i =='0':
  47. append_this.append(float(i))
  48. f.append((append_this))
  49.  
  50. return f
  51.  
  52. '''Basically, this line is to convert the distribution_train and
  53. probs_train which are currently strings
  54. into numbers. And we normalize the training data.'''
  55. x_train = num_converter_flatten(distribution_train)
  56. y_train = num_converter_flatten(probs_train)
  57. x_train = tf.keras.utils.normalize(x_train, axis=1)
  58.  
  59. '''This line we reshape x_train and y_train into tensors. The convertion
  60. to float 32 is also necessary as I realised that A and B are different
  61. floats for some reason.'''
  62.  
  63. A = tf.reshape(x_train, [-1,1*26])
  64. B = tf.reshape(y_train, [-1,1*80])
  65. A = tf.dtypes.cast(A, dtype = tf.float32)
  66. B = tf.dtypes.cast(B, dtype = tf.float32)
  67.  
  68. '''Doing the same thing to x_test and y_test'''
  69.  
  70. x_test = num_converter_flatten(distribution_test)
  71. y_test = num_converter_flatten(probs_test)
  72. C = tf.reshape(x_test, [-1,1*26])
  73. D = tf.reshape(y_test, [-1,1*80])
  74. C = tf.dtypes.cast(C, dtype = tf.float32)
  75. D = tf.dtypes.cast(D, dtype = tf.float32)
  76.  
  77. '''Model starts from here'''
  78.  
  79. model = tf.keras.models.Sequential()
  80.  
  81. '''I'm not too sure if relu is the right activation function to use here.
  82. I've tried different activation functions, but all run into the same
  83. problem described below.'''
  84.  
  85. model.add(tf.keras.layers.Dense(180, activation=keras.activations.relu, input_shape=(26,)))
  86.  
  87. model.add(tf.keras.layers.Dense(2080, activation=keras.activations.relu))
  88.  
  89. model.add(tf.keras.layers.Dense(180, activation=keras.activations.relu))
  90.  
  91. '''I'm making the final layer 80 because I want TF to output the size of
  92. the 'probs' list in the csv file'''
  93.  
  94. model.add(tf.keras.layers.Dense(80, activation=keras.activations.softplus))
  95.  
  96. '''Again I'm not sure if softplus is the best to use here. I've also
  97. tested a number of activation functions for the last layer, and it also
  98. runs to the same problem.'''
  99.  
  100. model.compile(optimizer='adam',
  101. loss='binary_crossentropy',
  102. metrics=['accuracy'])
  103.  
  104. model.fit(A,B, epochs=2, steps_per_epoch=16)
  105.  
  106. val_loss, val_acc = model.evaluate(C,D, steps = 128)
  107. print (val_loss, val_acc)
  108.  
  109.  
  110. '''Just saving the model'''
  111. model.save('epic_equation_model_try1')
  112. new_model = tf.keras.models.load_model('epic_equation_model_try1')
  113. predictions = new_model.predict(C, steps = 1)
  114.  
  115. for i in range(1):
  116. x = np.array(predictions[i]).reshape(5,16)
  117. print (x)
  118.  
  119. [[0.00000014 0.00000065 0.0000007 0.00000031 0.00000069 0.00000003
  120. 0.00000073 0.00000009 0.00000001 0.00000095 0.00000215 0.00000045
  121. 0.00000155 0.00000274 0.00000057 0.00053975]
  122. [0.00000016 0.00000011 0.00000021 0.00000006 0.00000012 0.00000022
  123. 0.00000002 0.00000005 0.00000019 0.00000002 0.00000087 0.00000465
  124. 0.00000238 0.00000009 0.00003278 0.00001788]
  125. [0.00000002 0.00000001 0.00000046 0.00000131 0.00000072 0.00000006
  126. 0.00000005 0.00000001 0.00000001 0.0000003 0.0000005 0.00000016
  127. 0.00000465 0.00000226 0.00000083 0.00002015]
  128. [0.00000005 0.0000004 0.00000001 0.00000032 0.00000008 0.00000061
  129. 0.00000107 0.00000015 0.00000013 0.00000014 0.00000012 0.00000037
  130. 0.00000334 0.00000016 0.00000057 0.00018404]
  131. [0.00000044 0.00000038 0.00000095 0.00000013 0.0000002 0.00000006
  132. 0.00000019 0.00000087 0.00000095 0.00000016 0.00000513 0.00000095
  133. 0.00000846 0.0000534 0.00000049 0.00000429]]
  134.  
  135. '''This tests for only the first prediction. If you wwant to see more
  136. predictions, change the range.'''
  137. for i in range(1):
  138. MUTI = 500000
  139. x = np.array(predictions[i]).reshape(5,16)
  140. # print (x)
  141. PX = MUTI*x
  142. PX = np.round(PX, 2)
  143. PX[PX<0.1] = 0
  144. PX[PX>0.1] = 1
  145. PX[PX==0.1] = 1
  146. print (PX)
Add Comment
Please, Sign In to add comment