Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. import os
  2. import re
  3.  
  4. import sklearn
  5. import numpy as np
  6. import tensorflow as tf
  7.  
  8. def main():
  9. path = './normalized/'
  10. data = []
  11. for i in range(51):
  12. data.append([])
  13. labels = []
  14. for f in os.listdir(path):
  15. m = re.match(r'(\w)_\d\.txt', f)
  16. if (m == None):
  17. continue
  18. char = m.group(1)
  19. with open(os.path.join(path + f), 'r') as lines:
  20. i = 0
  21. for line in lines:
  22. data[i].append(int(line.rstrip()))
  23. i += 1
  24. labels.append(0 if char == 'a' else 1 if char == 'b' else 2)
  25.  
  26. # construct the ml stuff
  27.  
  28. features = {}
  29. feature_columns = []
  30. for i in range(len(data)):
  31. key = str(i)
  32. features[key] = np.array(data[i])
  33. feature_columns.append(tf.feature_column.numeric_column(key=key))
  34.  
  35. # Build 2 hidden layer DNN with 10, 10 units respectively.
  36. classifier = tf.estimator.DNNClassifier(
  37. feature_columns=feature_columns,
  38. # Two hidden layers of 10 nodes each.
  39. hidden_units=[10, 10],
  40. # The model must choose between 3 classes.
  41. n_classes=3)
  42. #classifier = tf.estimator.LinearClassifier(
  43. # feature_columns=feature_columns,
  44. # # The model must choose between 3 classes.
  45. # n_classes=3)
  46.  
  47. batch_size = 100
  48. train_steps = 1000
  49.  
  50. # Train the Model.
  51. classifier.train(
  52. input_fn=lambda:train_input_fn(features, np.array(labels),
  53. batch_size),
  54. steps=train_steps)
  55.  
  56. # Evaluate the model.
  57. eval_result = classifier.evaluate(
  58. input_fn=lambda:eval_input_fn(features, np.array(labels),
  59. batch_size))
  60.  
  61. print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
  62.  
  63. test_data = []
  64. test_labels = ['2', '0', '1']
  65. for i in range(51):
  66. test_data.append([])
  67. with open(os.path.join(path + 'c.txt'), 'r') as lines:
  68. i = 0
  69. for line in lines:
  70. test_data[i].append(int(line.rstrip()))
  71. i += 1
  72. with open(os.path.join(path + 'a.txt'), 'r') as lines:
  73. i = 0
  74. for line in lines:
  75. test_data[i].append(int(line.rstrip()))
  76. i += 1
  77. with open(os.path.join(path + 'b.txt'), 'r') as lines:
  78. i = 0
  79. for line in lines:
  80. test_data[i].append(int(line.rstrip()))
  81. i += 1
  82.  
  83. test_features = {}
  84. for i in range(len(test_data)):
  85. test_features[str(i)] = np.array(test_data[i])
  86. predictions = classifier.predict(
  87. input_fn=lambda:eval_input_fn(test_features,
  88. labels=None,
  89. batch_size=batch_size))
  90.  
  91. template = ('\nPrediction is "{}" ({:.1f}%), expected "{}"')
  92.  
  93. for pred_dict, expec in zip(predictions, test_labels):
  94. class_id = pred_dict['class_ids'][0]
  95. probability = pred_dict['probabilities'][class_id]
  96.  
  97. print(template.format(class_id,
  98. 100 * probability, expec))
  99.  
  100.  
  101. def train_input_fn(features, labels, batch_size):
  102. """An input function for training"""
  103. # Convert the inputs to a Dataset.
  104. dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
  105.  
  106. # Shuffle, repeat, and batch the examples.
  107. #dataset = dataset.shuffle(1000).repeat().batch(batch_size)
  108. dataset = dataset.repeat().batch(batch_size)
  109.  
  110. # Return the dataset.
  111. return dataset
  112.  
  113.  
  114. def eval_input_fn(features, labels, batch_size):
  115. """An input function for evaluation or prediction"""
  116. features=dict(features)
  117. if labels is None:
  118. # No labels, use only features.
  119. inputs = features
  120. else:
  121. inputs = (features, labels)
  122.  
  123. # Convert the inputs to a Dataset.
  124. dataset = tf.data.Dataset.from_tensor_slices(inputs)
  125.  
  126. # Batch the examples
  127. assert batch_size is not None, "batch_size must not be None"
  128. dataset = dataset.batch(batch_size)
  129.  
  130. # Return the dataset.
  131. return dataset
  132.  
  133. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement