No Description
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.

tp2.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import matplotlib.pyplot as plt
  4. from sklearn.neural_network import MLPClassifier
  5. from sklearn.model_selection import train_test_split
  6. from sklearn.model_selection import KFold
  7. from sklearn.metrics import precision_score
  8. import random
  9. import time
  10. """
  11. from sklearn.datasets import fetch_openml
  12. mnist = fetch_openml('mnist_784')
  13. """
  14. """
  15. images = mnist.data.values.reshape((-1, 28, 28))
  16. plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest")
  17. plt.show()
  18. print(mnist.target[0])
  19. """
  20. indices = [i for i in range(len(mnist.data))]
  21. random.shuffle(indices)
  22. indices = indices[:15000]
  23. data = [mnist.data.values[i] for i in indices]
  24. target = [mnist.target[i] for i in indices]
  25. bestClf = None
  26. bestScore = 0
  27. scores = []
  28. train_sizes = [0.05*i for i in range(1,20)]
  29. xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)
  30. """
  31. network = MLPClassifier(hidden_layer_sizes = (50))
  32. network.fit(xtrain, ytrain)
  33. ytest_pred = clf.predict(xtest)
  34. print("Average = macro:")
  35. print(precision_score(ytest, ytest_pred, average = "macro"))
  36. print("Average = None:")
  37. print(precision_score(ytest, ytest_pred, average = None))
  38. print(target[4])
  39. print(network.predict(data[4].reshape(1,-1)))
  40. plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
  41. plt.show()
  42. """
  43. """
  44. n_layers = [i for i in range(1,51)]
  45. for n in n_layers:
  46. print(n)
  47. network = MLPClassifier(hidden_layer_sizes = (50,)*n)
  48. network.fit(xtrain, ytrain)
  49. scores += [network.score(xtest, ytest)]
  50. plt.plot(n_layers, scores)
  51. """
  52. """
  53. for _ in range(5):
  54. n_layers = random.randint(1,3)
  55. layer_size = tuple([random.randint(10,300) for _ in range(n_layers)])
  56. print(layer_size)
  57. network = MLPClassifier(hidden_layer_sizes = layer_size, solver = "adam", activation = "relu")
  58. network.fit(xtrain, ytrain)
  59. print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
  60. """
  61. """
  62. solvers = ["lbfgs", "sgd", "adam"]
  63. for s in solvers:
  64. print(s + ":")
  65. start_time = time.time()
  66. network = MLPClassifier(hidden_layer_sizes = (50,)*5, solver = s)
  67. network.fit(xtrain, ytrain)
  68. print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
  69. """
  70. """
  71. functions = ["identity", "logistic", "tanh", "relu"]
  72. for f in functions:
  73. print(f + ":")
  74. start_time = time.time()
  75. network = MLPClassifier(hidden_layer_sizes = (50,)*5, activation = f)
  76. network.fit(xtrain, ytrain)
  77. print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
  78. """
  79. times = []
  80. scores = []
  81. for i in range(-7,7):
  82. alpha = 10**i
  83. print(f"alpha = {alpha}:")
  84. start_time = time.time()
  85. network = MLPClassifier(hidden_layer_sizes = (50,)*5, alpha = alpha)
  86. network.fit(xtrain, ytrain)
  87. trainingTime = time.time() - start_time
  88. score = network.score(xtest, ytest)
  89. times +=[trainingTime]
  90. scores += [score]
  91. plt.plot([i for i in range(-7,7)], times)
  92. plt.show()
  93. plt.plot([i for i in range(-7,7)], scores)