• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Utility

a guest Jun 15th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import math
2. from sklearn.preprocessing import MinMaxScaler
3. from sklearn.cluster import KMeans
4. from sklearn.neighbors import NearestNeighbors
5. import numpy as np
6. from tqdm import tqdm
7. import matplotlib.pyplot as plt
8. import Layer
9. import Network
10.
11.
12. # numer funkcji,pierwszy argument,drugi argument - opcjonalny
13. def getFunctionValue(choice, x1, x2=0):
14.     if (choice) == 1:
15.         return math.sqrt(x1)
16.     elif (choice == 2):
17.         return math.sin(x1)
18.     else:
19.         return math.sin(x1 * x2) + math.cos(3 * (x1 - x2))
20.
21.
22. # liczba punktow,zakres,liczba argumentow
23. def getInputs(pointsNumber, pointsRange, argumentsNumber):
24.     inputs = np.asarray(np.full((pointsNumber, argumentsNumber), pointsRange[0], dtype=float))
25.     diff = pointsRange[1] - pointsRange[0]
26.     j = diff / (pointsNumber - 1)
27.     for counter, row in enumerate(inputs):
28.         row += j * counter
29.     if (argumentsNumber == 2):
30.         inputs[:, 1] = np.flip(inputs[:, 1])
31.     return np.asmatrix(inputs)
32.
33.
34. # tablica argumentow,numer funkcji
35. def getOutputs(inputs, functionNumber):
36.     rows, columns = inputs.shape
37.     outputs = np.asmatrix(np.zeros((rows, 1), dtype=float))
38.
39.     if (columns == 1):
40.         for i in range(rows):
41.             outputs[i, 0] = getFunctionValue(functionNumber, inputs[i, 0])
42.     else:
43.         for i in range(rows):
44.             outputs[i, 0] = getFunctionValue(functionNumber, inputs[i, 0], inputs[i, 1])
45.     return outputs
46.
47.
48. # DZIELENIE NA WEJSCIE I WYJSCIA ZBIORU TESTOWEGO I UCZACEGO
49. def divideDataset(inputs, outputs):
50.     height = inputs.shape[0]
51.     x = int(round(0.3 * height))
52.     y = int((inputs.shape[0] / x) + 1)
53.
54.     inputTrain = np.delete(inputs, np.arange(0, inputs.size, y), 0)
55.     outputTrain = np.delete(outputs, np.arange(0, outputs.size, y), 0)
56.
57.     inputTest = inputs[::y]
58.     outputTest = outputs[::y]
59.
60.     return inputTrain, outputTrain, inputTest, outputTest
61.
62.
63. def normalize(dataset):
64.     rows, columns = dataset.shape
65.     normalized = np.asmatrix(np.zeros((rows, columns), dtype=float))
66.     for i in range(columns):
67.         maxCol = max(dataset[:, i])
68.         minCol = min(dataset[:, i])
69.         for j in range(rows):
70.             normalized[j, i] = (dataset[j, i] - minCol) / (maxCol - minCol)
71.     return normalized
72.
73.
74. def learn(network, inputMatrix, outputMatrix, epochs):
75.     rows = inputMatrix.shape[0]
76.     arr = list(range(0, rows))
77.     plt.ion()
78.     plt.figure()
79.     x = [None] * rows
80.     results = [None] * rows
81.     expected = [None] * rows
82.
83.     for i in tqdm(range(epochs)):
84.         for j in tqdm(range(rows)):
85.             network.simulate(inputMatrix[arr[j]].T, outputMatrix[arr[j]].T)
86.             x[arr[j]] = inputMatrix[arr[j], 0]
87.             expected[arr[j]] = outputMatrix[arr[j], 0]
88.             results[arr[j]] = network.layers[-1].output[0, 0]
89.
90.         plot2D(x, results, expected, i, 'learn')
91.         np.random.shuffle(arr)
92.
93.
94. def test(network, input, output):
95.     rows = input.shape[0]
96.     x = list()
97.     results = list()
98.     expected = list()
99.
100.     for i in range(rows):
101.         network.forwardPropagate(input[i].T)
102.         x.append(input[i, 0])
103.         results.append(network.layers[-1].output[0, 0])
104.         expected.append(output[i, 0])
105.
106.     plot2D(x, results, expected, 1, 'test')
107.     plt.pause(25)
108.
109.
110. def plot2D(x, results, expected, epochNumber, mode):
111.     plt.clf()
112.     plt.xlabel('X')
113.     plt.ylabel('Y')
114.     plt.plot(x, results, 'g', label='Approximation')
115.     plt.plot(x, expected, 'r', label='Function')
116.     plt.legend()
117.     if (mode == 'learn'):
118.         plt.title('{}\nepoch {}'.format('current', epochNumber))
119.     plt.pause(0.0001)
120.
121.
122. #
123. # def plot3D(x1, x2, results, expected, epochNumber, mode):
124.
125.
126. def getCentroids(centroidsNumber, data):
127.     kmeans = KMeans(n_clusters=centroidsNumber)
128.     kmeans.fit(data)
129.     return np.asmatrix(kmeans.cluster_centers_)
130.
131.
132. def getWidths(centroids):
133.     widths = np.asmatrix(np.zeros((centroids.shape[0], 1), dtype=float))
134.     knn = NearestNeighbors(n_neighbors=5, algorithm='auto')
135.     knn.fit(centroids)
136.     # zwraca odleglosc od sasiadow i ich indeksy
137.     distances, indexes = knn.kneighbors(centroids)
138.     for counter, row in enumerate(distances):
139.         sum = 0
140.         for i in range(1, knn.n_neighbors):
141.             sum += pow(row[i], 2)
142.         r = math.sqrt(sum / (knn.n_neighbors - 1))
143.
144.         widths[counter] = r;
145.     return widths
146.
147.
148. def createNetwork(inputMatrix, outputMatrix, hiddenLayers, layersType):
149.     layers = np.array([])
150.
151.     if (layersType[0] == 'rbf'):
152.         centroids = getCentroids(hiddenLayers[1], inputMatrix)
153.         widths = getWidths(centroids)
154.
155.         temp = np.array([Layer.rbfLayer(centroids, widths)])
156.         layers = np.append(layers, temp)
157.
158.     elif (layersType[0] == 'affine'):
159.         temp = np.array([Layer.affineLayer(hiddenLayers[1], inputMatrix.shape[1], 1, 'sig')])
160.         layers = np.append(layers, temp)
161.
162.     for i in range(1, hiddenLayers[0]):
163.         if (layersType[i] == 'rbf'):
164.             centroids = getCentroids(hiddenLayers[i + 1], inputMatrix)
165.             widths = getWidths(centroids)
166.
167.             temp = np.array([Layer.rbfLayer(centroids, widths)])
168.             layers = np.append(layers, temp)
169.
170.         elif (layersType[i] == 'affine'):
171.             temp = np.array([Layer.affineLayer(hiddenLayers[i + 1], hiddenLayers[i], 1, 'sig')])
172.             layers = np.append(layers, temp)
173.
174.     temp = np.array([Layer.affineLayer(outputMatrix.shape[1], hiddenLayers[-1], 1, 'lin')])
175.     layers = np.append(layers, temp)
176.
177.     network = Network.Network(layers)
178.     return network
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?