Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from sklearn.preprocessing import MinMaxScaler
- from sklearn.cluster import KMeans
- from sklearn.neighbors import NearestNeighbors
- import numpy as np
- from tqdm import tqdm
- import matplotlib.pyplot as plt
- import Layer
- import Network
- # numer funkcji,pierwszy argument,drugi argument - opcjonalny
- def getFunctionValue(choice, x1, x2=0):
- if (choice) == 1:
- return math.sqrt(x1)
- elif (choice == 2):
- return math.sin(x1)
- else:
- return math.sin(x1 * x2) + math.cos(3 * (x1 - x2))
- # liczba punktow,zakres,liczba argumentow
- def getInputs(pointsNumber, pointsRange, argumentsNumber):
- inputs = np.asarray(np.full((pointsNumber, argumentsNumber), pointsRange[0], dtype=float))
- diff = pointsRange[1] - pointsRange[0]
- j = diff / (pointsNumber - 1)
- for counter, row in enumerate(inputs):
- row += j * counter
- if (argumentsNumber == 2):
- inputs[:, 1] = np.flip(inputs[:, 1])
- return np.asmatrix(inputs)
- # tablica argumentow,numer funkcji
- def getOutputs(inputs, functionNumber):
- rows, columns = inputs.shape
- outputs = np.asmatrix(np.zeros((rows, 1), dtype=float))
- if (columns == 1):
- for i in range(rows):
- outputs[i, 0] = getFunctionValue(functionNumber, inputs[i, 0])
- else:
- for i in range(rows):
- outputs[i, 0] = getFunctionValue(functionNumber, inputs[i, 0], inputs[i, 1])
- return outputs
- # DZIELENIE NA WEJSCIE I WYJSCIA ZBIORU TESTOWEGO I UCZACEGO
- def divideDataset(inputs, outputs):
- height = inputs.shape[0]
- x = int(round(0.3 * height))
- y = int((inputs.shape[0] / x) + 1)
- inputTrain = np.delete(inputs, np.arange(0, inputs.size, y), 0)
- outputTrain = np.delete(outputs, np.arange(0, outputs.size, y), 0)
- inputTest = inputs[::y]
- outputTest = outputs[::y]
- return inputTrain, outputTrain, inputTest, outputTest
- def normalize(dataset):
- rows, columns = dataset.shape
- normalized = np.asmatrix(np.zeros((rows, columns), dtype=float))
- for i in range(columns):
- maxCol = max(dataset[:, i])
- minCol = min(dataset[:, i])
- for j in range(rows):
- normalized[j, i] = (dataset[j, i] - minCol) / (maxCol - minCol)
- return normalized
- def learn(network, inputMatrix, outputMatrix, epochs):
- rows = inputMatrix.shape[0]
- arr = list(range(0, rows))
- plt.ion()
- plt.figure()
- x = [None] * rows
- results = [None] * rows
- expected = [None] * rows
- for i in tqdm(range(epochs)):
- for j in tqdm(range(rows)):
- network.simulate(inputMatrix[arr[j]].T, outputMatrix[arr[j]].T)
- x[arr[j]] = inputMatrix[arr[j], 0]
- expected[arr[j]] = outputMatrix[arr[j], 0]
- results[arr[j]] = network.layers[-1].output[0, 0]
- plot2D(x, results, expected, i, 'learn')
- np.random.shuffle(arr)
- def test(network, input, output):
- rows = input.shape[0]
- x = list()
- results = list()
- expected = list()
- for i in range(rows):
- network.forwardPropagate(input[i].T)
- x.append(input[i, 0])
- results.append(network.layers[-1].output[0, 0])
- expected.append(output[i, 0])
- plot2D(x, results, expected, 1, 'test')
- plt.pause(25)
- def plot2D(x, results, expected, epochNumber, mode):
- plt.clf()
- plt.xlabel('X')
- plt.ylabel('Y')
- plt.plot(x, results, 'g', label='Approximation')
- plt.plot(x, expected, 'r', label='Function')
- plt.legend()
- if (mode == 'learn'):
- plt.title('{}\nepoch {}'.format('current', epochNumber))
- plt.pause(0.0001)
- #
- # def plot3D(x1, x2, results, expected, epochNumber, mode):
- def getCentroids(centroidsNumber, data):
- kmeans = KMeans(n_clusters=centroidsNumber)
- kmeans.fit(data)
- return np.asmatrix(kmeans.cluster_centers_)
- def getWidths(centroids):
- widths = np.asmatrix(np.zeros((centroids.shape[0], 1), dtype=float))
- knn = NearestNeighbors(n_neighbors=5, algorithm='auto')
- knn.fit(centroids)
- # zwraca odleglosc od sasiadow i ich indeksy
- distances, indexes = knn.kneighbors(centroids)
- for counter, row in enumerate(distances):
- sum = 0
- for i in range(1, knn.n_neighbors):
- sum += pow(row[i], 2)
- r = math.sqrt(sum / (knn.n_neighbors - 1))
- widths[counter] = r;
- return widths
- def createNetwork(inputMatrix, outputMatrix, hiddenLayers, layersType):
- layers = np.array([])
- if (layersType[0] == 'rbf'):
- centroids = getCentroids(hiddenLayers[1], inputMatrix)
- widths = getWidths(centroids)
- temp = np.array([Layer.rbfLayer(centroids, widths)])
- layers = np.append(layers, temp)
- elif (layersType[0] == 'affine'):
- temp = np.array([Layer.affineLayer(hiddenLayers[1], inputMatrix.shape[1], 1, 'sig')])
- layers = np.append(layers, temp)
- for i in range(1, hiddenLayers[0]):
- if (layersType[i] == 'rbf'):
- centroids = getCentroids(hiddenLayers[i + 1], inputMatrix)
- widths = getWidths(centroids)
- temp = np.array([Layer.rbfLayer(centroids, widths)])
- layers = np.append(layers, temp)
- elif (layersType[i] == 'affine'):
- temp = np.array([Layer.affineLayer(hiddenLayers[i + 1], hiddenLayers[i], 1, 'sig')])
- layers = np.append(layers, temp)
- temp = np.array([Layer.affineLayer(outputMatrix.shape[1], hiddenLayers[-1], 1, 'lin')])
- layers = np.append(layers, temp)
- network = Network.Network(layers)
- return network
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement