-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPervaya_laba.py
276 lines (206 loc) · 9.81 KB
/
Pervaya_laba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import csv
import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import random
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
# Считывание данных с файла
def GetCSV(filename):
with open(filename) as f:
reader = csv.reader(f)
columns = next(reader)
colmap = dict(zip(columns, range(len(columns))))
return np.matrix(np.loadtxt(filename, delimiter=",", skiprows=1))
# Нормализация строчек
def normalizeRow(matrix, row):
length = matrix.shape[1]
min = np.amin(matrix[row])
max = np.amax(matrix[row])
for i in range(length):
matrix[row,i] = (matrix[row,i] - min) / (max - min)
# Нормализация тренировочных и тестовых данных
def normalizeData(trainData, testData, row):
_min = min(np.amin(trainData[row]), np.amin(testData[row]))
_max = max(np.amax(trainData[row]), np.amax(testData[row]))
for i in range(trainData.shape[1]):
trainData[row,i] = (trainData[row,i] - _min) / (_max - _min)
for i in range(testData.shape[1]):
testData[row,i] = (testData[row,i] - _min) / (_max - _min)
# Генерация случайных весов
def getRandomWeights(inLayerSize, outLayerSize):
return np.matrix(np.random.rand(outLayerSize, inLayerSize))
# Генерация случайных смещений
def getRandomOffsets(layerSize):
return (np.random.rand(layerSize,1)-0.5) * 0.1
# Посчитать функцию по каждому элементу матрицы
def matrixElementWiseFunction(matrix, function):
result = np.zeros(matrix.shape)
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
result[i, j] = function(matrix[i, j])
return result
# Сместить матрицу (добавить к каждому эл-ту число)
def offsetMatrix(matrix, offset):
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
matrix[i, j] += offset[i, 0]
# Neural network
global functionsCache
functionsCache = None
# Сгенерировать случайную нейронку
def getRandomNetwork(x, y, layer_infos):
layer_count = len(layer_infos)
last_neuron_count = x.shape[0]
weights = []
offsets = []
for layerInfo in layer_infos:
weights.append(getRandomWeights(last_neuron_count, layerInfo[0]))
offsets.append(getRandomOffsets(layerInfo[0]))
last_neuron_count = layerInfo[0]
weights.append(getRandomWeights(last_neuron_count, y.shape[0]))
offsets.append(getRandomOffsets(y.shape[0]))
return [weights, offsets]
# Для удобства работы с функциями
def getFunctionsArray(layer_infos, lastActivationFunc):
global functionsCache
if functionsCache is not None:
return functionsCache
functions = []
for layerInfo in layer_infos:
functions.append(layerInfo[1])
functions.append(lastActivationFunc)
functionsCache = functions
return functions
# Вычисление функции стоимости (среднеквадратичное отклонение)
def calculateCost(currentOutput, y, setSize):
cost = 0
#print(y.shape)
for i in range(setSize):
for j in range(y.shape[0]):
cost += (currentOutput[j,i]-y[j,i])**2
return cost / 2
# Прямой проход по нейронке
def feedForward(x, weights, offsets, layer_infos, lastActivationFunc):
functions = getFunctionsArray(layer_infos, lastActivationFunc)
layer_count = len(layer_infos)
z = []
a = []
a.append(x)
# Calculation
currentOutput = x
for i in range(layer_count + 1):
#print(i)
#print("O: " + str(offsets[i].shape))
#print("W: " + str(weights[i].shape))
#print("H: " + str(currentOutput.shape))
currentOutput = np.matmul(weights[i], currentOutput)
offsetMatrix(currentOutput, offsets[i])
z.append(currentOutput.copy())
currentOutput = matrixElementWiseFunction(currentOutput, functions[i][0])
a.append(currentOutput.copy())
#print("A: " + str(currentOutput.shape))
return [currentOutput,a ,z]
# Обратный проход по нейронке (Обучение нейронки, воозвращает новые веса и смещения)
def backPropagate(currentOutput, x, y, z, a, layer_infos, lastActivationFunc,weights, offsets, mu = 0.01):
functions = getFunctionsArray(layer_infos, lastActivationFunc)
layer_count = len(layer_infos)
deltas = []
currentDelta = np.multiply((currentOutput - y), matrixElementWiseFunction(z[layer_count], functions[layer_count][1]))
deltas.append(currentDelta)
for l in reversed(range(layer_count)):
currentDelta = np.multiply(np.matmul(weights[l + 1].transpose(), currentDelta), matrixElementWiseFunction(z[l],functions[l][1]))
deltas.append(currentDelta.copy())
newWeights = []
newOffsets = []
for i in range(layer_count + 1):
deltaL = deltas[layer_count - i]
newOffsets.append(offsets[i] - np.dot(mu, deltaL))
temp = np.matmul(deltaL, a[i].transpose())
newWeights.append(weights[i] - np.dot(mu, temp))
return [newWeights, newOffsets]
# Сама программа
def main(layer_infos, lastActivationFunc):
#Считывание данных
trainFilename = '/kaggle/input/modern-computer-technologies-laboratory/train.csv'
testFilename = '/kaggle/input/modern-computer-technologies-laboratory/test.csv'
train_csv = GetCSV(trainFilename)
test_csv = GetCSV(testFilename)
def GetInOutData(csv_data, shuffle = False):
size = int(csv_data.shape[0])
x = np.zeros((2, size))
y = np.zeros((2, size))
indexArray = list(range(size))
if(shuffle):
random.shuffle(indexArray)
for i in indexArray:
x[0,i] = csv_data[i,2]
x[1,i] = csv_data[i,1]
y[0,i] = csv_data[i,4]
y[1,i] = csv_data[i,3]
return {'x' : x, 'y' : y}
trainSetSize = int(train_csv.shape[0])
testSetSize = int(test_csv.shape[0])
trainData = GetInOutData(train_csv)
x = trainData['x']
y = trainData['y']
testData = GetInOutData(test_csv, True)
xTest = testData['x']
yTest = testData['y']
# Нормализация данных
normalizeData(x, xTest, 0)
normalizeData(x, xTest, 1)
normalizeData(y, yTest, 0)
normalizeData(y, yTest, 1)
# Генерация нчальной нейронки
randomNetwork = getRandomNetwork(x,y,layer_infos)
weights = randomNetwork[0]
offsets = randomNetwork[1]
# График тестовых данных
sns.lineplot(data = pd.DataFrame({"Set Y[0]" : yTest[0]}), palette=['r'])
sns.lineplot(data = pd.DataFrame({"Set Y[1]" : yTest[1]}), palette=['g'])
iterations = 20
period = 1
speed = 0.01
for i in range(iterations * period + 1):
if (i / period) % 1 == 0:
testOutput = feedForward(xTest, weights, offsets, layer_infos, lastActivationFunc)
print("\nTEST COST: " + str(calculateCost(testOutput[0], yTest, testSetSize)) + "\n")
#sns.lineplot(data = pd.DataFrame({"Set" + str(i) : yTest[0], "Output" : testOutput[0][0]}))
#sns.lineplot(data = pd.DataFrame({"Output" + str(i) : testOutput[0][0]}))
# Обучение (берет входные и выходные данные одной строки и делает прямой и обратный проход)
for j in range(x.shape[1]):
xDash = np.matrix(x[:,j]).transpose()
yDash = np.matrix(y[:,j]).transpose()
feedOutput = feedForward(xDash, weights, offsets, layer_infos, lastActivationFunc)
currentOutput = feedOutput[0]
a = feedOutput[1]
z = feedOutput[2]
#print(str(i) + " Train cost: " + str(calculateCost(currentOutput, yDash, 1)))
newNetwork = backPropagate(currentOutput, xDash, yDash, z, a, layer_infos, lastActivationFunc,weights, offsets, speed)
weights = newNetwork[0]
offsets = newNetwork[1]
# График результатов обучения
testOutput = feedForward(xTest, weights, offsets, layer_infos, lastActivationFunc)
print("\nTEST COST: " + str(calculateCost(testOutput[0], yTest, testSetSize)) + "\n")
#sns.lineplot(data = pd.DataFrame({"Set" + str(i) : yTest[0], "Output" : testOutput[0][0]}))
sns.lineplot(data = pd.DataFrame({"Output [0]" : testOutput[0][0]}), palette=['b'])
sns.lineplot(data = pd.DataFrame({"Output [1]" : testOutput[0][1]}), palette=['y'])
def ReLu(x):
return x if x > 0 else 0
def ReLuDerivative(x):
return 0 if x < 0 else 1
def Sigmoid(x):
return 1/(1+np.exp(-x))
def SigmoidDerivative(x):
return Sigmoid(x) * (1 - Sigmoid(x))
ReLuFunctions = [ReLu, ReLuDerivative]
SigmoidFunctions = [Sigmoid, SigmoidDerivative]
# Запуск программы, задаются размеры слоев и функции
main(layer_infos=[[10, ReLuFunctions],[15, ReLuFunctions],[10, ReLuFunctions]], lastActivationFunc = SigmoidFunctions)