Advertisement
Guest User

Untitled

a guest
Feb 21st, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """Перцептрон.ipynb
  3.  
  4. Automatically generated by Colaboratory.
  5.  
  6. Original file is located at
  7.    https://colab.research.google.com/drive/1B6_exDJbrp6sk0ABTZtsPHiIluxq0iLL
  8. """
  9.  
  10. target = "Price"
  11.  
  12. import numpy as np
  13. import pandas as pd
  14.  
  15. class Layer:
  16.     def __init__(self, input_size, output_size):
  17.         self.weights = np.random.randn(input_size, output_size)
  18.         self.bias = np.zeros((1, output_size))
  19.         self.inputs = None
  20.         self.outputs = None
  21.         self.d_weights = None
  22.         self.d_bias = None
  23.  
  24.     def forward(self, inputs):
  25.         self.inputs = inputs
  26.         self.outputs = np.dot(inputs, self.weights) + self.bias
  27.         return self.outputs
  28.  
  29.     def backward(self, d_outputs, learning_rate):
  30.         d_inputs = np.dot(d_outputs, self.weights.T)
  31.         self.d_weights = np.dot(self.inputs.reshape(-1, 1), d_outputs)
  32.         self.d_bias = np.sum(d_outputs, axis=0, keepdims=True)
  33.  
  34.         # Update weights and bias
  35.         self.weights -= learning_rate * self.d_weights
  36.         self.bias -= learning_rate * self.d_bias
  37.  
  38.         return d_inputs
  39.  
  40. def sigmoid(x):
  41.     return 1 / (1 + np.exp(-x))
  42.  
  43. def sigmoid_derivative(x):
  44.     return sigmoid(x) * (1 - sigmoid(x))
  45.  
  46. class Activation:
  47.     def __init__(self, activation_func, activation_derivative):
  48.         self.activation_func = activation_func
  49.         self.activation_derivative = activation_derivative
  50.  
  51.     def forward(self, inputs):
  52.         self.inputs = inputs
  53.         return self.activation_func(inputs)
  54.  
  55.     def backward(self, d_outputs):
  56.         return d_outputs * self.activation_derivative(self.inputs)
  57.  
  58. class NeuralNetwork:
  59.     def __init__(self, layers):
  60.         self.layers = layers
  61.  
  62.     def forward_propagation(self, X):
  63.         for layer in self.layers:
  64.             X = layer.forward(X)
  65.         return X
  66.  
  67.     def back_propagation(self, d_output, learning_rate):
  68.         for layer in reversed(self.layers):
  69.             d_output = layer.backward(d_output, learning_rate) if isinstance(layer, Layer) else layer.backward(d_output)
  70.  
  71.     def mse(self, y_true, y_pred):
  72.         return ((y_true - y_pred) ** 2).mean()
  73.  
  74.     def fit(self, X, y, learning_rate, epochs):
  75.         for epoch in range(epochs):
  76.             total_error = 0
  77.             for i in range(len(X)):
  78.                 outputs = self.forward_propagation(X[i])
  79.                 error = self.mse(y[i], outputs)
  80.                 total_error += error
  81.                 d_output = outputs - y[i]
  82.                 self.back_propagation(d_output, learning_rate)
  83.             mean_error = total_error / len(X)
  84.             print(f'Epoch {epoch + 1}/{epochs}, Mean Squared Error: {mean_error}')
  85.  
  86.  
  87.     def guess(self, x):
  88.         output = self.forward_propagation(x)
  89.         return output
  90.  
  91. # input_size = 2
  92. # hidden_size = 2
  93. # output_size = 1
  94.  
  95. # layer1 = Layer(input_size, hidden_size)
  96. # activation1 = Activation(sigmoid, sigmoid_derivative)
  97. # layer2 = Layer(hidden_size, output_size)
  98.  
  99. # neural_network = NeuralNetwork([layer1, activation1, layer2])
  100.  
  101. # x = np.array([[0, 0], [0, 1], [1, 0], [1, 1], [0.5, 0.5]])
  102. # y = np.array([[0], [1], [1], [0], [0]])
  103.  
  104. # learning_rate = 0.1
  105. # epochs = 1000
  106.  
  107. # neural_network.fit(X, y, learning_rate, epochs)
  108.  
  109. # # Making a prediction
  110. # new_sample = np.array([[1, 0]])
  111. # prediction = neural_network.guess(new_sample)
  112. # print("Prediction for", new_sample, ":", prediction)
  113.  
  114.  
  115. import numpy as np
  116.  
  117. # Define the sine dataset
  118. x = np.linspace(0, 2*np.pi, 100)  # Input values from 0 to 2*pi
  119. y = np.sin(x).reshape(-1, 1)      # Corresponding sine values
  120.  
  121. input_size = 1
  122. hidden_size = 2
  123. output_size = 1
  124.  
  125. layer1 = Layer(input_size, hidden_size)
  126. activation1 = Activation(sigmoid, sigmoid_derivative)
  127. layer2 = Layer(hidden_size, output_size)
  128.  
  129. neural_network = NeuralNetwork([layer1, activation1, layer2])
  130.  
  131. learning_rate = 0.1
  132. epochs = 1000
  133.  
  134. neural_network.fit(x, y, learning_rate, epochs)
  135.  
  136. # Making a prediction
  137. new_sample = np.array([[np.pi/2]])  # Predict the sine value of pi/2
  138. prediction = neural_network.guess(new_sample)
  139. print("Prediction for", new_sample, ":", prediction)
  140.  
  141. from sklearn.preprocessing import StandardScaler
  142. import pandas as pd
  143. import numpy as np
  144.  
  145. # Чтение данных из файла
  146. df = pd.read_csv("data.csv")
  147.  
  148. # Преобразование категориальных признаков в бинарные
  149. encoded_data = pd.get_dummies(df, columns=['Brand'], prefix='Brand', drop_first=True)
  150.  
  151. # Разделение данных на матрицу признаков X и вектор целевой переменной y
  152. X = encoded_data[["Storage_Capacity"]].values
  153. scaler = StandardScaler()
  154. X = scaler.fit_transform(X)
  155. print(X)
  156.  
  157. y = encoded_data[target].values
  158.  
  159. # Определение размеров слоев
  160. input_size = X.shape[1]
  161. hidden_size1 = 8
  162. hidden_size2 = 16
  163. hidden_size3 = 8
  164. output_size = 1
  165.  
  166. # Создание слоев и активаций
  167. layer1 = Layer(input_size, hidden_size1)
  168. activation1 = Activation(sigmoid, sigmoid_derivative)
  169. layer2 = Layer(hidden_size1, hidden_size2)
  170. activation2 = Activation(sigmoid, sigmoid_derivative)
  171. layer3 = Layer(hidden_size2, hidden_size3)
  172. activation3 = Activation(sigmoid, sigmoid_derivative)
  173. layer4 = Layer(hidden_size3, output_size)
  174.  
  175. # Создание нейронной сети с добавленными слоями
  176. neural_network = NeuralNetwork([layer1, activation1, layer2, activation2, layer3, activation3, layer4])
  177.  
  178. # Обучение нейронной сети
  179. learning_rate = 0.1
  180. epochs = 1000
  181. neural_network.fit(X, y, learning_rate, epochs)
  182.  
  183. res = neural_network.guess(X[0])
  184. print(res)
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement