def __reservoirTrain__(self, x):

        #Extract the parameters
        inputScaling = x[0]
        leakingRate = x[1]

        #Create the reservoir
        res = dr.DeterministicReservoir(
            size=self.size,
            inputWeight_v=self.inputWeight_v,
            inputWeightScaling=inputScaling,
            inputData=self.trainingInputData,
            outputData=self.trainingOutputData,
            leakingRate=leakingRate,
            initialTransient=self.initialTransient,
            reservoirTopology=self.reservoirTopology)
        #Train the reservoir
        res.trainReservoir()

        #Predict for the validation data
        predictedOutputData = res.predict(self.validationInputData)

        #Calcuate the regression error
        errorFunction = rmse.MeanSquareError()
        regressionError = errorFunction.compute(self.validationOutputData,
                                                predictedOutputData)

        #Return the error
        return regressionError
    def __reservoirTrain__(self, x):

        #Extract the parameters
        spectralRadius = x[0]
        inputScaling = x[1]
        reservoirScaling = x[2]
        leakingRate = x[3]

        #Create the reservoir
        res = classicESN.Reservoir(
            size=self.size,
            spectralRadius=spectralRadius,
            inputScaling=inputScaling,
            reservoirScaling=reservoirScaling,
            leakingRate=leakingRate,
            initialTransient=self.initialTransient,
            inputData=self.trainingInputData,
            outputData=self.trainingOutputData,
            inputWeightRandom=self.inputWeightRandom,
            reservoirWeightRandom=self.reservoirWeightRandom)

        #Train the reservoir
        res.trainReservoir()

        #Predict for the validation data
        predictedOutputData = res.predict(self.validationInputData)

        #Calcuate the regression error
        errorFunction = rmse.MeanSquareError()
        regressionError = errorFunction.compute(self.validationOutputData,
                                                predictedOutputData)

        #Return the error
        return regressionError
示例#3
0
    def __ESNConnTrain__(self, x):
        #Extract the parameters
        inputConnectivity = x[0]
        reservoirConnectivity = x[1]
        reservoirTopology = topology.RandomTopology(
            size=self.size, connectivity=reservoirConnectivity)

        #Create the network
        esn = EchoStateNetwork.EchoStateNetwork(
            size=self.size,
            inputData=self.trainingInputData,
            outputData=self.trainingOutputData,
            reservoirTopology=reservoirTopology,
            inputConnectivity=inputConnectivity)

        #Train the reservoir
        esn.trainReservoir()

        #Predict for the validation data
        predictedOutputData = esn.predict(self.validationInputData)

        #Calcuate the regression error
        errorFunction = rmse.MeanSquareError()
        regressionError = errorFunction.compute(self.validationOutputData,
                                                predictedOutputData)

        #Free the memory
        gc.collect()

        #Return the error
        #print("Optimizing connectivity..")
        #print("\nRegression error:"+str(regressionError)+"\n")
        return regressionError
示例#4
0
def trainAndGetError(size, spectralRadius, inputScaling, reservoirScaling,
                     leakingRate, initialTransient, trainingInputData,
                     trainingOutputData, inputWeightMatrix,
                     reservoirWeightMatrix, validationOutputData, horizon,
                     testingActualOutputData):

    # Error function
    errorFun = metrics.MeanSquareError()

    # Train
    network = ESN.Reservoir(size=size,
                            spectralRadius=spectralRadius,
                            inputScaling=inputScaling,
                            reservoirScaling=reservoirScaling,
                            leakingRate=leakingRate,
                            initialTransient=initialTransient,
                            inputData=trainingInputData,
                            outputData=trainingOutputData,
                            inputWeightRandom=inputWeightMatrix,
                            reservoirWeightRandom=reservoirWeightMatrix)
    network.trainReservoir()

    warmupFeatureVectors, warmTargetVectors = formFeatureVectors(
        validationOutputData)
    predictedWarmup = network.predict(warmupFeatureVectors[-initialTransient:])

    initialInputSeedForTesing = validationOutputData[-1]

    predictedOutputData = predictFuture(network, initialInputSeedForTesing,
                                        horizon)[:, 0]

    # Calculate the error
    error = errorFun.compute(testingActualOutputData, predictedOutputData)
    return error
    def __reservoirTrain__(self, x):

        #Extract the parameters
        meanDegree, beta = x
        meanDegree = int(meanDegree)

        # To get rid off the randomness in assigning weights, run it 10 times and  take the average error
        times = 100
        cumulativeError = 0

        for i in range(times):
            # Input and weight connectivity Matrix
            inputWeightMatrix = topology.ClassicInputTopology(
                self.inputD, self.size).generateWeightMatrix()
            reservoirWeightMatrix = topology.SmallWorldGraphs(
                size=self.size, meanDegree=meanDegree,
                beta=beta).generateWeightMatrix()

            #Create the reservoir
            res = classicESN.Reservoir(
                size=self.size,
                spectralRadius=self.spectralRadius,
                inputScaling=self.inputScaling,
                reservoirScaling=self.reservoirScaling,
                leakingRate=self.leakingRate,
                initialTransient=self.initialTransient,
                inputData=self.trainingInputData,
                outputData=self.trainingOutputData,
                inputWeightRandom=inputWeightMatrix,
                reservoirWeightRandom=reservoirWeightMatrix)

            #Train the reservoir
            res.trainReservoir()

            # Warm up
            predictedTrainingOutputData = res.predict(
                self.trainingInputData[-self.initialTransient:])

            #Predict for the validation data
            predictedOutputData = util.predictFuture(res, self.initialSeed,
                                                     self.horizon)

            gc.collect()

            #Calcuate the regression error
            errorFunction = metrics.MeanSquareError()
            error = errorFunction.compute(self.validationOutputData,
                                          predictedOutputData)
            cumulativeError += error

        regressionError = cumulativeError / times

        #Return the error
        print("\nThe Parameters: " + str(x) + " Regression error:" +
              str(regressionError))
        return regressionError
    def evaluate(self, x):

        # Extract the parameters
        attachment = int(x[0, 0])

        # To get rid off the randomness in assigning weights, run it 10 times and take the average error
        times = 1
        cumulativeError = 0

        for i in range(times):
            # Input and weight connectivity Matrix
            inputWeightMatrix = topology.ClassicInputTopology(
                self.inputD, self.size).generateWeightMatrix()
            network = topology.ScaleFreeNetworks(size=self.size,
                                                 attachmentCount=attachment)
            reservoirWeightMatrix = network.generateWeightMatrix()

            # Create the reservoir
            res = esn.Reservoir(size=self.size,
                                spectralRadius=self.spectralRadius,
                                inputScaling=self.inputScaling,
                                reservoirScaling=self.reservoirScaling,
                                leakingRate=self.leakingRate,
                                initialTransient=self.initialTransient,
                                inputData=self.trainingInputData,
                                outputData=self.trainingOutputData,
                                inputWeightRandom=inputWeightMatrix,
                                reservoirWeightRandom=reservoirWeightMatrix)

            # Train the reservoir
            res.trainReservoir()

            # Warm up
            predictedTrainingOutputData = res.predict(
                self.trainingInputData[-self.initialTransient:])

            # Predict for the validation data
            predictedOutputData = util.predictFuture(res, self.initialSeed,
                                                     self.horizon)

            gc.collect()

            # Calculate the regression error
            errorFunction = metrics.MeanSquareError()
            error = errorFunction.compute(self.validationOutputData,
                                          predictedOutputData)
            cumulativeError += error

        regressionError = cumulativeError / times

        # Return the error
        #print("Attachment: "+str(attachment) + "Error: "+str(regressionError))
        return regressionError
示例#7
0
    def __ESNConnTrain__(self, x):
        #Extract the parameters
        inputConnectivity = x[0]
        meanDegree = int(np.floor(x[1]))
        beta = x[2]
        reservoirTopology = topology.SmallWorldGraphs(size=self.size,
                                                      meanDegree=meanDegree,
                                                      beta=beta)
        #print("\nParameters:"+str(x))

        cumRMSE = 0
        times = 10
        #Run many times - just to get rid of randomness in assigning random weights
        for i in range(times):

            #Create the network
            esn = EchoStateNetwork.EchoStateNetwork(
                size=self.size,
                inputData=self.trainingInputData,
                outputData=self.trainingOutputData,
                reservoirTopology=reservoirTopology,
                inputConnectivity=inputConnectivity)

            #Train the reservoir
            esn.trainReservoir()

            #Predict for the validation data
            predictedOutputData = esn.predict(self.validationInputData)

            #Calcuate the regression error
            errorFunction = rmse.MeanSquareError()
            regressionError = errorFunction.compute(self.validationOutputData,
                                                    predictedOutputData)
            cumRMSE += regressionError

            #Free the memory
            gc.collect()

        regressionError = cumRMSE / times

        #Return the error
        #print("Regression error"+str(regressionError)+"\n")
        return regressionError
    def __reservoirTrain__(self, x):

        #Extract the parameters
        spectralRadius = x[0]
        inputScaling = x[1]
        reservoirScaling = x[2]
        leakingRate = x[3]

        #Create the reservoir
        res = classicESN.Reservoir(
            size=self.size,
            spectralRadius=spectralRadius,
            inputScaling=inputScaling,
            reservoirScaling=reservoirScaling,
            leakingRate=leakingRate,
            initialTransient=self.initialTransient,
            inputData=self.trainingInputData,
            outputData=self.trainingOutputData,
            inputWeightRandom=self.inputWeightRandom,
            reservoirWeightRandom=self.reservoirWeightRandom)

        #Train the reservoir
        res.trainReservoir()

        # Warm up
        predictedTrainingOutputData = res.predict(
            self.trainingInputData[-self.initialTransient:])

        #Predict for the validation data
        predictedOutputData = util.predictFuture(res, self.initialSeed,
                                                 self.horizon)

        gc.collect()

        #Calcuate the regression error
        errorFunction = metrics.MeanSquareError()
        regressionError = errorFunction.compute(self.validationOutputData,
                                                predictedOutputData)

        #Return the error
        print("\nThe Parameters: " + str(x) + " Regression error:" +
              str(regressionError))
        return regressionError
示例#9
0
    def __ESNTrain__(self, x):
        #print("\nOptimizing esn parameters:"+str(x))
        #Extract the parameters
        spectralRadius = x[0]
        inputScaling = x[1]
        reservoirScaling = x[2]
        leakingRate = x[3]

        #Create the reservoir
        esn = EchoStateNetwork.EchoStateNetwork(
            size=self.size,
            inputData=self.trainingInputData,
            outputData=self.trainingOutputData,
            spectralRadius=spectralRadius,
            inputScaling=inputScaling,
            reservoirScaling=reservoirScaling,
            leakingRate=leakingRate,
            initialTransient=self.initialTransient,
            inputWeightConn=self.inputWeightConn,
            reservoirWeightConn=self.reservoirWeightConn,
            reservoirTopology=topology.SmallWorldGraphs(
                size=self.size,
                meanDegree=self.meanDegreeOptimum,
                beta=self.betaOptimum))

        #Train the reservoir
        esn.trainReservoir()

        #Predict for the validation data
        predictedOutputData = esn.predict(self.validationInputData)

        #Calcuate the regression error
        errorFunction = rmse.MeanSquareError()
        regressionError = errorFunction.compute(self.validationOutputData,
                                                predictedOutputData)

        #Free the memory
        gc.collect()

        #Return the error
        #print("Regression error"+str(regressionError)+"\n")
        return regressionError
示例#10
0
    def __ESNConnTrain__(self, x):
        #Extract the parameters
        inputConnectivity = x[0]
        probability = x[1]
        reservoirTopology = topology.ErdosRenyiTopology(
            size=self.size, probability=probability)
        #print("\nInput:"+str(inputConnectivity)+" Probability:"+str(probability))

        cumRMSE = 0
        times = 10
        #Run many times - just to get rid of randomness in assigning random weights
        for i in range(times):

            #Create the network
            esn = EchoStateNetwork.EchoStateNetwork(
                size=self.size,
                inputData=self.trainingInputData,
                outputData=self.trainingOutputData,
                reservoirTopology=reservoirTopology,
                inputConnectivity=inputConnectivity)

            #Train the reservoir
            esn.trainReservoir()

            #Predict for the validation data
            predictedOutputData = esn.predict(self.validationInputData)

            #Calcuate the regression error
            errorFunction = rmse.MeanSquareError()
            regressionError = errorFunction.compute(self.validationOutputData,
                                                    predictedOutputData)
            cumRMSE += regressionError

            #Free the memory
            gc.collect()

        regressionError = cumRMSE / times

        #Return the error
        #print("\nOptimizing connectivity..")
        #print("Regression error"+str(regressionError)+"\n")
        return regressionError
示例#11
0
    def __ESNTrain__(self, x):

        #Extract the parameters
        attachment = int(np.floor(x[0]))

        reservoirTopology = topology.ScaleFreeNetworks(
            size=self.size, attachmentCount=attachment)
        #print("\nOptimizing connectivity..")
        print("\nAttachment:" + str(attachment))

        cumRMSE = 0
        times = self.times
        for i in range(times):
            #Create the network
            esn = EchoStateNetwork.EchoStateNetwork(
                size=self.size,
                inputData=self.trainingInputData,
                outputData=self.trainingOutputData,
                reservoirTopology=reservoirTopology,
                inputConnectivity=self.inputConnectivity)

            #Train the reservoir
            esn.trainReservoir()

            #Predict for the validation data
            predictedOutputData = esn.predict(self.validationInputData)

            #Calcuate the regression error
            errorFunction = rmse.MeanSquareError()
            regressionError = errorFunction.compute(self.validationOutputData,
                                                    predictedOutputData)
            cumRMSE += regressionError

            #Free the memory
            gc.collect()

        regressionError = cumRMSE / times
        #Return the error
        print("\nRegression error:" + str(regressionError) + "\n")
        return regressionError
示例#12
0
    def __ESNTrain__(self, x):

        #Extract the parameters
        spectralRadius = x[0]
        leakingRate = x[1]

        #Create the reservoir
        esn = EchoStateNetwork.EchoStateNetwork(
            size=self.size,
            inputData=self.trainingInputData,
            outputData=self.trainingOutputData,
            spectralRadius=spectralRadius,
            leakingRate=leakingRate,
            initialTransient=self.initialTransient,
            inputWeightConn=self.inputWeightConn,
            reservoirWeightConn=self.reservoirWeightConn,
            reservoirTopology=topology.RandomTopology(
                self.size, self.reservoirConnectivityOptimum))

        #Train the reservoir
        esn.trainReservoir()

        #Predict for the validation data
        predictedOutputData = esn.predict(self.validationInputData)

        #Calcuate the regression error
        errorFunction = rmse.MeanSquareError()
        regressionError = errorFunction.compute(self.validationOutputData,
                                                predictedOutputData)

        #Free the memory
        gc.collect()

        #Return the error
        #print("Optimizing spectral radius and leaking rate...")
        #print("\nRegression error"+str(regressionError)+"\n")
        return regressionError
示例#13
0
                                     featureVectors=featureTrainingVectors,
                                     targetVectors=targetTrainingVectors,
                                     initialTransient=50,
                                     inputConnectivity=0.5,
                                     reservoirConnectivity=0.5,
                                     inputScaling=0.5,
                                     reservoirScaling=0.0,
                                     spectralRadius=0.79,
                                     leakingRate=0.30)

# Step 7 - Predict the future
predictedSeries = util.predictFuture(trainingSeries, depth, horizon)

# # Step 8 - De-scale the series
actualSeries = util.descaleSeries(testingSeries)
predictedSeries = util.descaleSeries(predictedSeries)

error = metrics.MeanSquareError()
regressionError = error.compute(testingSeries.values, predictedSeries.values)
print(regressionError)

# Step 9 - Plot the results
details = profileName + "_yearsOfData_" + str(yearsOfData) + "_horizon_" + str(
    daysOfHorizon) + "_depth_" + str(daysOfDepth) + "_network_size_" + str(
        networkSize)
util.plotSeries("Outputs/Outputs_" + str(datetime.now()) + details,
                [actualSeries, predictedSeries],
                ["Actual Output", "Predicted Output"],
                "Facebook Own Posts Interaction Rate - " + profileName,
                "Interaction Rate")
                    inputScaling=0.5,
                    reservoirScaling=0.5,
                    leakingRate=0.3,
                    initialTransient=initialTransient,
                    inputWeightRandom=inputWeight,
                    reservoirWeightRandom=reservoirWeight)
res.trainReservoir()

#Warm up
predictedTrainingOutputData = res.predict(inputTrainingData)

#Predict future values
predictedTestOutputData = util.predictFuture(res, trainingData[-1], nTesting)

#Calculate the error
errorFunction = rmse.MeanSquareError()
error = errorFunction.compute(testingData, predictedTestOutputData)
print("Regression error:" + str(error))

#Plotting of the prediction output and error
outputFolderName = "Outputs/Outputs" + datetime.now().strftime(
    "%Y_%m_%d_%H_%M_%S")
os.mkdir(outputFolderName)
outplot = outputPlot.OutputPlot(outputFolderName + "/Prediction.html",
                                "Mackey-Glass Time Series - Classic ESN",
                                "Prediction of future values", "Time",
                                "Output")
outplot.setXSeries(np.arange(1, nTesting + 1))
outplot.setYSeries('Actual Output',
                   minMax.inverse_transform(testingData[:nTesting, 0]))
outplot.setYSeries(
# Split the data into training, validation and testing
trainingData, validationData, testingData = util.splitData(
    data, 0.5, 0.25, 0.25)
nValidation = validationData.shape[0]
nTesting = testingData.shape[0]

# Form feature vectors for training data
trainingInputData, trainingOutputData = util.formFeatureVectors(trainingData)
actualOutputData = minMax.inverse_transform(testingData)[:, 0]

# Initial seed
initialSeedForValidation = trainingData[-1]

# Error function
errorFun = metrics.MeanSquareError()

# Number of iterations
iterations = 1
reservoirSize = 256

# Run Classic ESN
classicESNError = 0
print("\n Running Classic ESN Tuner..")
for i in range(iterations):
    predictedOutputData, error = util.tuneTrainPredict(
        trainingInputData=trainingInputData,
        trainingOutputData=trainingOutputData,
        validationOutputData=validationData,
        initialInputSeedForValidation=initialSeedForValidation,
        reservoirTopology=topology.ClassicReservoirTopology(