Advertisement
mirosh111000

Numbers(pr6)

Sep 29th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.70 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import tensorflow as tf
  4. import matplotlib.pyplot as plt
  5. from sklearn.metrics import log_loss, accuracy_score, classification_report, mean_squared_error
  6. from imblearn.over_sampling import SMOTE
  7. from mpl_toolkits.mplot3d import Axes3D
  8. from sklearn.preprocessing import StandardScaler
  9. from sklearn.decomposition import PCA
  10. import plotly.express as px
  11.  
  12. class MultiNeuralNetwork:
  13.     def __init__(self, type='reg', learning_rate=0.01, epochs=100000, hidden_layers=[10], C=1, early_stopping_rounds=np.inf):
  14.         self.type = type # 'reg', 'binary', 'multi'
  15.         self.learning_rate = learning_rate
  16.         self.epochs = epochs
  17.         self.hidden_layers = hidden_layers
  18.         self.C = C
  19.         self.early_stopping_rounds = early_stopping_rounds
  20.         self.weights = []
  21.         self.biases = []
  22.         self.loss_ = []
  23.         self.epochs_ = []
  24.  
  25.     def sigmoid(self, z):
  26.         return 1 / (1 + np.exp(-z))
  27.  
  28.     def sigmoid_derivative(self, z):
  29.         s = self.sigmoid(z)
  30.         return s * (1 - s)
  31.  
  32.     def linear(self, z, C=1):
  33.         return z * C
  34.  
  35.     def softmax(self, z):
  36.         exp_values = np.exp(z - np.max(z))
  37.         return exp_values / np.sum(exp_values, axis=0)
  38.  
  39.     def fit(self, x, y):
  40.         def initialize_parameters(input_size, output_size):
  41.             layers = [input_size] + self.hidden_layers + [output_size]
  42.             self.weights = [np.random.randn(layers[i], layers[i + 1]) * np.sqrt(1 / layers[i]) for i in range(len(layers) - 1)]
  43.             self.biases = [np.random.randn(layers[i + 1]) * 0.01 for i in range(len(layers) - 1)]
  44.  
  45.         best_err, count = np.inf, 0
  46.  
  47.         try:
  48.             input_size = x.shape[1]
  49.         except:
  50.             x = x.reshape(-1, 1)
  51.             input_size = x.shape[1]
  52.            
  53.         output_size = len(np.unique(y)) if self.type == 'multi' else 1
  54.         initialize_parameters(input_size=input_size, output_size=output_size)
  55.         for epoch in range(self.epochs):  
  56.             y_pred = []
  57.             for i, x_sample in enumerate(x):
  58.                 inputs, activations, outputs = [x_sample], [], []
  59.                 input = x_sample
  60.  
  61.                 len_ = len(self.weights)
  62.                 d_weights, d_biases = [None] * len_ , [None] * len_
  63.                
  64.                 for idx, _ in enumerate(zip(self.weights, self.biases)):
  65.                     w, b = _[0], _[1]
  66.                    
  67.                     activation = np.dot(input, w) + b
  68.                     activations.append(activation)
  69.                    
  70.                     if w.shape[1] == 1 and self.type == 'reg':
  71.                         output = self.linear(activation, C=self.C)
  72.                         outputs.append(output)
  73.                     elif w.shape[1] == 1 and self.type == 'binary':
  74.                         output = self.sigmoid(activation)
  75.                         outputs.append(output)
  76.                     elif idx == (len_-1) and self.type == 'multi':
  77.                         output = self.softmax(activation)
  78.                         outputs.append(output)
  79.                     else:
  80.                         input = self.sigmoid(activation)
  81.                         outputs.append(input)
  82.                         inputs.append(input)
  83.  
  84.                 y_pred.append(output)
  85.                
  86.                 if self.type == 'multi':
  87.                     delta = output - np.eye(output_size)[y[i]]
  88.                 else:
  89.                     delta = output - y[i]
  90.                    
  91.                 d_biases[len_-1] = delta * self.C
  92.                 d_weights[len_-1] = np.dot(inputs[len_-1].reshape(-1, 1), d_biases[len_-1].reshape(-1, 1).T)
  93.  
  94.                 for idx in reversed(range(len_ - 1)):
  95.                     d_biases[idx] = np.dot(d_biases[idx+1], self.weights[idx+1].T * self.sigmoid_derivative(activations[idx]))
  96.                     d_weights[idx] = np.dot(inputs[idx].reshape(-1, 1), d_biases[idx].reshape(-1, 1).T)
  97.  
  98.                 for idx in (range(len_)):
  99.                     self.weights[idx] -= self.learning_rate * d_weights[idx]
  100.                     self.biases[idx] -= self.learning_rate * d_biases[idx]
  101.        
  102.             if self.type == 'reg':
  103.                 err = mean_squared_error(y_true=y, y_pred=y_pred)
  104.                 print(f'Epoche={epoch}, MSE={err}')
  105.             elif self.type == 'binary' or self.type == 'multi':
  106.                 err = log_loss(y, y_pred)
  107.                 print(f'Epoche={epoch}, logloss={err}')
  108.             if err < best_err:
  109.                 count = 0
  110.                 best_err = err
  111.             else:
  112.                 count += 1
  113.             self.epochs_.append(epoch+1)
  114.             self.loss_.append(err)
  115.            
  116.             if count >= self.early_stopping_rounds:
  117.                 print(f"Навчання завершено на епохі {epoch} з помилкою {err}")
  118.                 break
  119.  
  120.     def predict(self, x):
  121.        
  122.         try:
  123.             _ = x.shape[1]
  124.         except:
  125.             x = x.reshape(-1, 1)
  126.        
  127.         y_pred = []
  128.         len_ = len(self.weights)
  129.         for x_sample in x:
  130.             input = x_sample
  131.             for idx, _ in enumerate(zip(self.weights, self.biases)):
  132.                 w, b = _[0], _[1]
  133.                 activation = np.dot(input, w) + b
  134.                
  135.                 if w.shape[1] == 1 and self.type == 'reg':
  136.                     output = self.linear(activation, C=self.C)
  137.                 elif w.shape[1] == 1 and self.type == 'binary':
  138.                     output = (self.sigmoid(activation) > 0.5).astype(int)
  139.                 elif idx == (len_-1) and self.type == 'multi':
  140.                     output = np.argmax(self.softmax(activation))
  141.                 else:
  142.                     input = self.sigmoid(activation)
  143.             y_pred.append(output)
  144.         return np.array(y_pred)
  145.  
  146.  
  147.  
  148. mnist = tf.keras.datasets.mnist
  149. (X_train, y_train), (X_test, y_test) = mnist.load_data()
  150.  
  151. plt.figure(figsize=(10,5))
  152. for i in range(5):
  153.     plt.subplot(1, 5, i + 1)
  154.     plt.imshow(X_train[i], cmap='gray_r')
  155.     plt.title(f"Label: {y_train[i]}")
  156.     plt.axis("off")
  157. plt.show()
  158.  
  159. x_train = X_train.reshape(X_train.shape[0], -1) / 255.0
  160. x_test = X_test.reshape(X_test.shape[0], -1) / 255.0
  161.  
  162. df = pd.DataFrame(x_train)
  163. df['target'] = y_train
  164.  
  165.  
  166. n = 3
  167. x_pca = PCA(n_components=n).fit_transform(StandardScaler().fit_transform(x_train))
  168.  
  169. df_pca = pd.DataFrame(x_pca, columns=[f'PCA{i+1}' for i in range(n)], index=df.index)
  170. df_pca['target'] = y_train
  171.  
  172. fig = px.scatter_3d(
  173.     df_pca,
  174.     x='PCA1',
  175.     y='PCA2',
  176.     z='PCA3',
  177.     color='target',
  178.     labels={'target': 'Клас'},
  179.     title="3D візуалізація PCA з класами"
  180. )
  181.  
  182. num = 10
  183. fig.update_layout(
  184.     scene=dict(
  185.         xaxis=dict(range=[df_pca['PCA1'].min() - num, df_pca['PCA1'].max() + num], title='PCA1'),
  186.         yaxis=dict(range=[df_pca['PCA2'].min() - num, df_pca['PCA2'].max() + num], title='PCA2'),
  187.         zaxis=dict(range=[df_pca['PCA3'].min() - num, df_pca['PCA3'].max() + num], title='PCA3')
  188.     )
  189. )
  190.  
  191. fig.update_traces(marker=dict(size=5))
  192. fig.show()
  193.  
  194.  
  195.  
  196. def plot_values_counts(df):
  197.     plt.figure(figsize=(15, 4))
  198.     df['target'].value_counts().plot(kind='barh', color='skyblue')
  199.     for index, value in enumerate(df['target'].value_counts()):
  200.         plt.text(value, index, f'{value}', va='center')
  201.     plt.grid()
  202.     plt.title('Кількість кожного з класів в навчальній вибірці')
  203.     plt.ylabel('Клас')
  204.     plt.xlabel('Кількість')
  205.     plt.show()
  206.  
  207. plot_values_counts(df)
  208.  
  209.  
  210. sampler = SMOTE(random_state=42)
  211. x_train_resampled, y_train_resampled = sampler.fit_resample(x_train, y_train)
  212. df_sampled = pd.DataFrame(x_train_resampled)
  213. df_sampled['target'] = y_train_resampled
  214.  
  215. plot_values_counts(df_sampled)
  216.  
  217.  
  218. model = MultiNeuralNetwork(type='multi', learning_rate=0.01, epochs=1000, hidden_layers=[20], early_stopping_rounds=10)
  219. model.fit(x_train_resampled, y_train_resampled)
  220.  
  221. y_pred_train = model.predict(x_train)
  222. y_pred = model.predict(x_test)
  223.  
  224. plt.figure(figsize=(10,5))
  225. for i in range(5):
  226.     plt.subplot(1, 5, i + 1)
  227.     plt.imshow(X_test[i], cmap='gray_r')
  228.     plt.title(f"Real: {y_test[i]} | Pred: {y_pred[i]}")
  229.     plt.axis("off")
  230. plt.show()
  231.  
  232. print(f"Точність на тренувальних даних: {accuracy_score(y_train, y_pred_train) * 100:.2f}%")
  233. print(f"Точність на тестових даних: {accuracy_score(y_test, y_pred) * 100:.2f}%")
  234.  
  235. print(classification_report(y_test, y_pred))
  236.  
  237.  
  238. model_wthout_res = MultiNeuralNetwork(type='multi', learning_rate=0.01, epochs=1000, hidden_layers=[20], early_stopping_rounds=10)
  239. model_wthout_res.fit(x_train, y_train)
  240.  
  241. y_pred_train = model_wthout_res.predict(x_train)
  242. y_pred = model_wthout_res.predict(x_test)
  243.  
  244. plt.figure(figsize=(10,5))
  245. for i in range(5):
  246.     plt.subplot(1, 5, i + 1)
  247.     plt.imshow(X_test[i+5], cmap='gray_r')
  248.     plt.title(f"Real: {y_test[i+5]} | Pred: {y_pred[i+5]}")
  249.     plt.axis("off")
  250. plt.show()
  251.  
  252. print(f"Точність на тренувальних даних: {accuracy_score(y_train, y_pred_train) * 100:.2f}%")
  253. print(f"Точність на тестових даних: {accuracy_score(y_test, y_pred) * 100:.2f}%")
  254.  
  255. print(classification_report(y_test, y_pred))
  256.  
  257.  
  258. plt.figure(figsize=(15, 5))
  259. plt.title('Залежність помилки від епохи навчання')
  260. plt.plot(model.epochs_, model.loss_, c='blue', label=f'З оверсемплінгом (lr={model.learning_rate})')
  261. plt.plot(model_wthout_res.epochs_, model_wthout_res.loss_, c='red', label=f'Без оверсемплінгу (lr={model_wthout_res.learning_rate})')
  262. plt.ylabel('logloss')
  263. plt.xlabel('Epoche')
  264. plt.grid()
  265. plt.legend()
  266. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement