You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
3.0 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # This is the code what she teaches in the first hour in https://www.youtube.com/watch?v=qFJeN9V1ZsI
  4. import numpy as np
  5. from random import randint
  6. from sklearn.utils import shuffle
  7. from sklearn.preprocessing import MinMaxScaler
  8. train_labels = []
  9. train_samples = []
  10. for i in range(50):
  11. train_samples.append(randint(13, 64))
  12. train_labels.append(1)
  13. train_samples.append(randint(65, 100))
  14. train_labels.append(0)
  15. for i in range(1000):
  16. train_samples.append(randint(13, 64))
  17. train_labels.append(0)
  18. train_samples.append(randint(65, 100))
  19. train_labels.append(1)
  20. train_labels = np.array(train_labels)
  21. train_samples = np.array(train_samples)
  22. train_labels, train_samples = shuffle(train_labels, train_samples)
  23. scaler = MinMaxScaler(feature_range=(0,1))
  24. # `reshape` the data from 1D to fit in the fit() function ... instead of x we need [x]
  25. # I assume because the array could contain any number of el, but we only have one, so it looks odd
  26. scaled_train_samples = scaler.fit_transform(train_samples.reshape(-1, 1))
  27. #print(scaled_train_samples)
  28. import tensorflow as tf
  29. from tensorflow import keras
  30. from tensorflow.keras.models import Sequential
  31. from tensorflow.keras.layers import Activation, Dense
  32. from tensorflow.keras.optimizers import Adam
  33. from tensorflow.keras.metrics import categorical_crossentropy
  34. gpus = tf.config.experimental.list_physical_devices('GPU')
  35. print('GPUs = ', len(gpus))
  36. #tf.config.experimental.set_memory_growth(gpus[0], True)
  37. model = Sequential([
  38. Dense(units=16, input_shape=(1,), activation='relu'),
  39. Dense(units=32, activation='relu'),
  40. Dense(units=2, activation='softmax'),
  41. ])
  42. #model.summary()
  43. model.compile(optimizer=Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
  44. #model.fit(x=scaled_train_samples, y=train_labels, batch_size=10, epochs=30, shuffle=True, verbose=2)
  45. model.fit(x=scaled_train_samples, y=train_labels, validation_split=0.1, batch_size=10, epochs=30, shuffle=True, verbose=2)
  46. test_labels = []
  47. test_samples = []
  48. for i in range(10):
  49. test_samples.append(randint(13, 64))
  50. test_labels.append(1)
  51. test_samples.append(randint(65, 100))
  52. test_labels.append(0)
  53. for i in range(200):
  54. test_samples.append(randint(13, 64))
  55. test_labels.append(0)
  56. test_samples.append(randint(65, 100))
  57. test_labels.append(1)
  58. test_labels = np.array(test_labels)
  59. test_samples = np.array(test_samples)
  60. test_labels, test_samples = shuffle(test_labels, test_samples)
  61. scaled_test_samples = scaler.fit_transform(test_samples.reshape(-1, 1))
  62. # # Predict
  63. predictions = model.predict(x=scaled_test_samples, batch_size=10, verbose=0)
  64. print(predictions)
  65. # seems to work ONLY in jupyter
  66. from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
  67. rounded_predictions = np.argmax(predictions, axis=-1)
  68. cm = confusion_matrix(y_true=test_labels, y_pred=rounded_predictions)
  69. cm_plot_labels = ['no_side_fx', 'has_side_fx']
  70. disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=cm_plot_labels)
  71. disp.plot()