fork download
  1. import pandas as pd
  2. import numpy as np
  3. import pickle
  4. from keras.datasets import mnist
  5. import matplotlib.pyplot as plt
  6.  
  7. def ReLU(Z):
  8. return np.maximum(Z,0)
  9.  
  10. def derivative_ReLU(Z):
  11. return Z > 0
  12.  
  13. def softmax(Z):
  14. """Compute softmax values for each sets of scores in x."""
  15. exp = np.exp(Z - np.max(Z)) #le np.max(Z) evite un overflow en diminuant le contenu de exp
  16. return exp / exp.sum(axis=0)
  17.  
  18. def init_params(size):
  19. W1 = np.random.rand(10,size) - 0.5
  20. b1 = np.random.rand(10,1) - 0.5
  21. W2 = np.random.rand(10,10) - 0.5
  22. b2 = np.random.rand(10,1) - 0.5
  23. return W1,b1,W2,b2
  24.  
  25. def forward_propagation(X,W1,b1,W2,b2):
  26. Z1 = W1.dot(X) + b1 #10, m
  27. A1 = ReLU(Z1) # 10,m
  28. Z2 = W2.dot(A1) + b2 #10,m
  29. A2 = softmax(Z2) #10,m
  30. return Z1, A1, Z2, A2
  31.  
  32. def one_hot(Y):
  33. ''' return an 0 vector with 1 only in the position correspondind to the value in Y'''
  34. one_hot_Y = np.zeros((Y.max()+1,Y.size)) #si le chiffre le plus grand dans Y est 9 ca fait 10 lignes
  35. one_hot_Y[Y,np.arange(Y.size)] = 1 # met un 1 en ligne Y[i] et en colonne i, change l'ordre mais pas le nombre
  36. return one_hot_Y
  37.  
  38. def backward_propagation(X, Y, A1, A2, W2, Z1, m):
  39. one_hot_Y = one_hot(Y)
  40. dZ2 = 2*(A2 - one_hot_Y) #10,m
  41. dW2 = 1/m * (dZ2.dot(A1.T)) # 10 , 10
  42. db2 = 1/m * np.sum(dZ2,1) # 10, 1
  43. dZ1 = W2.T.dot(dZ2)*derivative_ReLU(Z1) # 10, m
  44. dW1 = 1/m * (dZ1.dot(X.T)) #10, 784
  45. db1 = 1/m * np.sum(dZ1,1) # 10, 1
  46.  
  47. return dW1, db1, dW2, db2
  48.  
  49. def update_params(alpha, W1, b1, W2, b2, dW1, db1, dW2, db2):
  50. W1 -= alpha * dW1
  51. b1 -= alpha * np.reshape(db1, (10,1))
  52. W2 -= alpha * dW2
  53. b2 -= alpha * np.reshape(db2, (10,1))
  54.  
  55. return W1, b1, W2, b2
  56.  
  57. def get_predictions(A2):
  58. return np.argmax(A2, 0)
  59.  
  60. def get_accuracy(predictions, Y):
  61. return np.sum(predictions == Y)/Y.size
  62.  
  63. def gradient_descent(X, Y, alpha, iterations):
  64. size , m = X.shape
  65.  
  66. W1, b1, W2, b2 = init_params(size)
  67. for i in range(iterations):
  68. Z1, A1, Z2, A2 = forward_propagation(X, W1, b1, W2, b2)
  69. dW1, db1, dW2, db2 = backward_propagation(X, Y, A1, A2, W2, Z1, m)
  70.  
  71. W1, b1, W2, b2 = update_params(alpha, W1, b1, W2, b2, dW1, db1, dW2, db2)
  72.  
  73. if (i+1) % int(iterations/10) == 0:
  74. print(f"Iteration: {i+1} / {iterations}")
  75. prediction = get_predictions(A2)
  76. print(f'{get_accuracy(prediction, Y):.3%}')
  77. return W1, b1, W2, b2
  78.  
  79. def make_predictions(X, W1 ,b1, W2, b2):
  80. _, _, _, A2 = forward_propagation(X, W1, b1, W2, b2)
  81. predictions = get_predictions(A2)
  82. return predictions
  83.  
  84. def show_prediction(index,X, Y, W1, b1, W2, b2):
  85. # None => cree un nouvel axe de dimension 1, cela a pour effet de transposer X[:,index] qui un np.array de dimension 1 (ligne) et qui devient un vecteur (colonne)
  86. # ce qui correspond bien a ce qui est demande par make_predictions qui attend une matrice dont les colonnes sont les pixels de l'image, la on donne une seule colonne
  87.  
  88.  
  89. vect_X = X[:, index,None]
  90. prediction = make_predictions(vect_X, W1, b1, W2, b2)
  91. label = Y[index]
  92. print("Prediction: ", prediction)
  93. print("Label: ", label)
  94.  
  95. current_image = vect_X.reshape((WIDTH, HEIGHT)) * SCALE_FACTOR
  96.  
  97. plt.gray()
  98. plt.imshow(current_image, interpolation='nearest')
  99. plt.show()
  100.  
  101. def show_from_input(vect_X, W1, b1, W2, b2):
  102.  
  103.  
  104. prediction = make_predictions(vect_X, W1, b1, W2, b2)
  105. return prediction
  106. #label = Y[index]
  107. #print("Prediction: ", prediction)
  108.  
  109. #current_image = vect_X.reshape((WIDTH, HEIGHT)) * SCALE_FACTOR
  110.  
  111. #plt.gray()
  112. #plt.imshow(current_image, interpolation='nearest')
  113. #plt.show()
  114. def predict(a):
  115. show_from_input(a, W1, b1, W2, b2)
  116.  
  117.  
  118.  
  119. ############## MAIN ##############
  120.  
  121. (X_train, Y_train), (X_test, Y_test) = mnist.load_data()
  122. SCALE_FACTOR = 255 # TRES IMPORTANT SINON OVERFLOW SUR EXP
  123. WIDTH = X_train.shape[1]
  124. HEIGHT = X_train.shape[2]
  125. X_train = X_train.reshape(X_train.shape[0],WIDTH*HEIGHT).T / SCALE_FACTOR
  126. X_test = X_test.reshape(X_test.shape[0],WIDTH*HEIGHT).T / SCALE_FACTOR
  127.  
  128. W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 0.15, 200)
  129.  
  130. with open("model.pkl","wb") as dump_file:
  131. pickle.dump((bl, W1, b1, W2, b2),dump_file)
  132.  
  133.  
  134.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: '.' expected
import pandas as pd
             ^
Main.java:1: error: ';' expected
import pandas as pd
                ^
Main.java:2: error: '.' expected
import numpy as np
            ^
Main.java:2: error: ';' expected
import numpy as np
               ^
Main.java:3: error: '.' expected
import pickle
             ^
Main.java:4: error: ';' expected
from keras.datasets import mnist
    ^
Main.java:4: error: '.' expected
from keras.datasets import mnist
                                ^
Main.java:5: error: ';' expected
import matplotlib.pyplot as plt
                        ^
Main.java:15: error: illegal character: '#'
    exp = np.exp(Z - np.max(Z)) #le np.max(Z) evite un overflow en diminuant le contenu de exp
                                ^
Main.java:26: error: illegal character: '#'
    Z1 = W1.dot(X) + b1 #10, m
                        ^
Main.java:27: error: illegal character: '#'
    A1 = ReLU(Z1) # 10,m
                  ^
Main.java:28: error: illegal character: '#'
    Z2 = W2.dot(A1) + b2 #10,m
                         ^
Main.java:29: error: illegal character: '#'
    A2 = softmax(Z2) #10,m
                     ^
Main.java:33: error: empty character literal
    ''' return an 0 vector with 1 only in the position correspondind to the value in Y'''
    ^
Main.java:33: error: unclosed character literal
    ''' return an 0 vector with 1 only in the position correspondind to the value in Y'''
      ^
Main.java:33: error: empty character literal
    ''' return an 0 vector with 1 only in the position correspondind to the value in Y'''
                                                                                      ^
Main.java:33: error: illegal line end in character literal
    ''' return an 0 vector with 1 only in the position correspondind to the value in Y'''
                                                                                        ^
Main.java:34: error: illegal character: '#'
    one_hot_Y = np.zeros((Y.max()+1,Y.size)) #si le chiffre le plus grand dans Y est 9 ca fait 10 lignes
                                             ^
Main.java:35: error: illegal character: '#'
    one_hot_Y[Y,np.arange(Y.size)] = 1 # met un 1 en ligne Y[i] et en colonne i, change l'ordre mais pas le nombre
                                       ^
Main.java:35: error: unclosed character literal
    one_hot_Y[Y,np.arange(Y.size)] = 1 # met un 1 en ligne Y[i] et en colonne i, change l'ordre mais pas le nombre
                                                                                         ^
Main.java:40: error: illegal character: '#'
    dZ2 = 2*(A2 - one_hot_Y) #10,m
                             ^
Main.java:41: error: illegal character: '#'
    dW2 = 1/m * (dZ2.dot(A1.T)) # 10 , 10
                                ^
Main.java:42: error: illegal character: '#'
    db2 = 1/m * np.sum(dZ2,1) # 10, 1
                              ^
Main.java:43: error: illegal character: '#'
    dZ1 = W2.T.dot(dZ2)*derivative_ReLU(Z1) # 10, m
                                            ^
Main.java:44: error: illegal character: '#'
    dW1 = 1/m * (dZ1.dot(X.T)) #10, 784
                               ^
Main.java:45: error: illegal character: '#'
    db1 = 1/m * np.sum(dZ1,1) # 10, 1
                              ^
Main.java:76: error: unclosed character literal
            print(f'{get_accuracy(prediction, Y):.3%}')
                   ^
Main.java:76: error: unclosed character literal
            print(f'{get_accuracy(prediction, Y):.3%}')
                                                     ^
Main.java:85: error: illegal character: '#'
    # None => cree un nouvel axe de dimension 1, cela a pour effet de transposer X[:,index] qui un np.array de dimension 1 (ligne) et qui devient un vecteur (colonne)
    ^
Main.java:86: error: illegal character: '#'
    #  ce qui correspond bien a ce qui est demande par make_predictions qui attend une matrice dont les colonnes sont les pixels de l'image, la on donne une seule colonne
    ^
Main.java:86: error: unclosed character literal
    #  ce qui correspond bien a ce qui est demande par make_predictions qui attend une matrice dont les colonnes sont les pixels de l'image, la on donne une seule colonne
                                                                                                                                     ^
Main.java:98: error: unclosed character literal
    plt.imshow(current_image, interpolation='nearest')
                                            ^
Main.java:98: error: unclosed character literal
    plt.imshow(current_image, interpolation='nearest')
                                                    ^
Main.java:106: error: illegal character: '#'
    #label = Y[index]
    ^
Main.java:107: error: illegal character: '#'
    #print("Prediction: ", prediction)
    ^
Main.java:109: error: illegal character: '#'
    #current_image = vect_X.reshape((WIDTH, HEIGHT)) * SCALE_FACTOR
    ^
Main.java:111: error: illegal character: '#'
    #plt.gray()
    ^
Main.java:112: error: illegal character: '#'
    #plt.imshow(current_image, interpolation='nearest')
    ^
Main.java:112: error: unclosed character literal
    #plt.imshow(current_image, interpolation='nearest')
                                             ^
Main.java:112: error: unclosed character literal
    #plt.imshow(current_image, interpolation='nearest')
                                                     ^
Main.java:113: error: illegal character: '#'
    #plt.show()
    ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
 ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
  ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
   ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
    ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
     ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
      ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
       ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
        ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
         ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
          ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
           ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
            ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
             ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                    ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                     ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                      ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                       ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                        ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                         ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                          ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                           ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                            ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                             ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                              ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                               ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                                ^
Main.java:119: error: illegal character: '#'
############## MAIN ##############
                                 ^
Main.java:122: error: illegal character: '#'
SCALE_FACTOR = 255 # TRES IMPORTANT SINON OVERFLOW SUR EXP
                   ^
70 errors
stdout
Standard output is empty