Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import tensorflow as tf
  2. from tensorflow import keras
  3. from sklearn.model_selection import train_test_split
  4. from random import random
  5. import numpy as np
  6.  
  7. targets = []
  8. inputs = []
  9.  
  10. CLASS_A = 0
  11. CLASS_B = 1
  12.  
  13. def sort_by_probability(pair):
  14. return pair[0]
  15.  
  16. def create_model(input_count):
  17. model = keras.Sequential([
  18. keras.layers.Dense(3, input_dim=input_count, activation=tf.nn.relu),
  19. keras.layers.Dense(3, activation=tf.nn.relu, kernel_regularizer=keras.regularizers.l2(0.0000)),
  20. keras.layers.Dense(1, activation=tf.nn.sigmoid, kernel_regularizer=keras.regularizers.l2(0.00000))
  21. ])
  22. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
  23. return model
  24.  
  25. def assess_accuracy(model, targets_validation, inputs_validation):
  26. predictions = [(float(v[0]), targets_validation[i], inputs_validation[i]) for i, v in enumerate(model.predict(np.asarray(inputs_validation, dtype=np.float32)))]
  27. predictions.sort(key=sort_by_probability)
  28. predictions.reverse()
  29. correct = 0
  30. for pair in predictions[:100]:
  31. if(pair[1] == CLASS_B):
  32. correct += 1
  33. print(str(correct) + "% correct")
  34.  
  35. for v in range(0, 100000):
  36. target = CLASS_A if random() < 0.8 else CLASS_B
  37. feature = [0.45 + 0.1 * random()] if random() < 0.2 and target == CLASS_B else [random()]
  38. targets.append(target)
  39. inputs.append(feature)
  40. inputs_training, inputs_validation, targets_training, targets_validation = train_test_split(inputs, targets, test_size=0.3, random_state=42)
  41. model = create_model(1)
  42. model.fit(np.array(inputs_training), np.array(targets_training), epochs=10, verbose=0, class_weight = {0:1, 1:4})
  43. print("With one feature")
  44. assess_accuracy(model, targets_validation, inputs_validation)
  45.  
  46. print("Now with three features, each equally meaningful as the first one")
  47. inputs = []
  48. targets = []
  49. for v in range(0, 100000):
  50. target = CLASS_A if random() < 0.8 else CLASS_B
  51. featureA = 0.45 + 0.1 * random() if random() < 0.2 and target == CLASS_B else random()
  52. featureB = 0.45 + 0.1 * random() if random() < 0.2 and target == CLASS_B else random()
  53. featureC = 0.45 + 0.1 * random() if random() < 0.2 and target == CLASS_B else random()
  54. targets.append(target)
  55. inputs.append([featureA, featureB, featureC])
  56. inputs_training, inputs_validation, targets_training, targets_validation = train_test_split(inputs, targets, test_size=0.3, random_state=42)
  57. model = create_model(3)
  58. model.fit(np.array(inputs_training), np.array(targets_training), epochs=10, verbose=0, class_weight = {0:1, 1:4})
  59. assess_accuracy(model, targets_validation, inputs_validation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement