def __collectOutputs__(self, featureIndices):

        # Form the feature and target vectors
        featureVectors = self.__formFeaturevectors__(self.inputData,
                                                     featureIndices)
        targetVectors = self.outputData

        # Append bias
        featureVectors = np.hstack((np.ones(
            (featureVectors.shape[0], 1)), featureVectors))

        # Input weight matrix
        inputSize = featureVectors.shape[1]
        reservoirSize = self.lowerLayerParameters['size']
        inputWeightRandom = topology.RandomInputTopology(
            inputSize, reservoirSize, self.
            lowerLayerParameters['inputConnectivity']).generateWeightMatrix()
        reservoirWeightRandom = topology.RandomReservoirTopology(
            reservoirSize, self.lowerLayerParameters['reservoirConnectivity']
        ).generateWeightMatrix()

        # Generate the reservoir
        res = esn.Reservoir(
            size=self.lowerLayerParameters['size'],
            spectralRadius=self.lowerLayerParameters['spectralRadius'],
            inputScaling=self.lowerLayerParameters['inputScaling'],
            reservoirScaling=self.lowerLayerParameters['reservoirScaling'],
            leakingRate=self.lowerLayerParameters['leakingRate'],
            initialTransient=self.lowerLayerParameters['initialTransient'],
            inputData=featureVectors,
            outputData=targetVectors,
            inputWeightRandom=inputWeightRandom,
            reservoirWeightRandom=reservoirWeightRandom,
            reservoirActivationFunction=self.
            lowerLayerParameters['reservoirActivation'],
            outputActivationFunction=self.
            lowerLayerParameters['outputActivation'])

        # Train the reservoir
        res.trainReservoir()

        # Just assign the weights randomly

        # Store the reservoir
        self.lowerLayerNetworks.append(res)

        # Collect the outputs
        outputs = res.predict(featureVectors)

        return outputs
    def trainReservoir(self):

        # Features for the network in the higher layer
        features = None

        # Collect outputs from the lower layer
        for i in range(self.lowerLayerCount):
            if (features is None):  # First time
                features = self.__collectOutputs__(self.featureIndicesList[i])
            else:
                features = np.hstack(
                    (features,
                     self.__collectOutputs__(self.featureIndicesList[i])))

        # Append bias
        features = np.hstack((np.ones((features.shape[0], 1)), features))

        # Generate the higher layer reservoir
        # where features are the outputs of the lower layer networks
        inputSize = features.shape[1]
        reservoirSize = self.higherLayerParameters['size']

        inputWeightRandom = topology.RandomInputTopology(
            inputSize, reservoirSize, self.
            higherLayerParameters['inputConnectivity']).generateWeightMatrix()
        reservoirWeightRandom = topology.RandomReservoirTopology(
            self.higherLayerParameters['size'],
            self.higherLayerParameters['reservoirConnectivity']
        ).generateWeightMatrix()

        self.higherLayerReservoir = esn.Reservoir(
            size=self.higherLayerParameters['size'],
            spectralRadius=self.higherLayerParameters['spectralRadius'],
            inputScaling=self.higherLayerParameters['inputScaling'],
            reservoirScaling=self.higherLayerParameters['reservoirScaling'],
            leakingRate=self.higherLayerParameters['leakingRate'],
            initialTransient=self.higherLayerParameters['initialTransient'],
            inputData=features,
            outputData=self.outputData,
            inputWeightRandom=inputWeightRandom,
            reservoirWeightRandom=reservoirWeightRandom,
            reservoirActivationFunction=self.
            higherLayerParameters['reservoirActivation'],
            outputActivationFunction=self.
            higherLayerParameters['outputActivation'])
        # Train the composite network
        self.higherLayerReservoir.trainReservoir()
    def __createTransformer__(self, featureVectors):

        # Append the bias
        #featureVectors = np.hstack((np.ones((featureVectors.shape[0],1)),featureVectors))
        featureVectors = featureVectors
        targetVectors = self.outputData

        # Input weight matrix
        inputSize = featureVectors.shape[1]
        reservoirSize = self.featureTransformerParameters['size']
        inputWeightRandom = topology.RandomInputTopology(
            inputSize, reservoirSize,
            self.featureTransformerParameters['inputConnectivity']
        ).generateWeightMatrix()
        reservoirWeightRandom = topology.RandomReservoirTopology(
            reservoirSize,
            self.featureTransformerParameters['reservoirConnectivity']
        ).generateWeightMatrix()

        # Generate the reservoir
        self.transformer = esn.Reservoir(
            size=self.featureTransformerParameters['size'],
            spectralRadius=self.featureTransformerParameters['spectralRadius'],
            inputScaling=self.featureTransformerParameters['inputScaling'],
            reservoirScaling=self.
            featureTransformerParameters['reservoirScaling'],
            leakingRate=self.featureTransformerParameters['leakingRate'],
            initialTransient=self.
            featureTransformerParameters['initialTransient'],
            inputData=featureVectors,
            outputData=targetVectors,
            inputWeightRandom=inputWeightRandom,
            reservoirWeightRandom=reservoirWeightRandom,
            reservoirActivationFunction=self.
            featureTransformerParameters['reservoirActivation'],
            outputActivationFunction=self.
            featureTransformerParameters['outputActivation'])

        # Collect and return the internal state
        internalStates = self.transformer.collectInternalStates(featureVectors)

        return internalStates
    def trainReservoir(self):

        # Feature transformation
        features = self.__createTransformer__(self.inputData)

        # Append bias
        features = np.hstack((np.ones((features.shape[0], 1)), features))

        # Generate the predictor
        # where features are transformed using transformer(esn)
        inputSize = features.shape[1]
        reservoirSize = self.predictorParameters['size']
        inputWeightRandom = topology.RandomInputTopology(
            inputSize, reservoirSize, self.
            predictorParameters['inputConnectivity']).generateWeightMatrix()
        reservoirWeightRandom = topology.RandomReservoirTopology(
            self.predictorParameters['size'],
            self.predictorParameters['reservoirConnectivity']
        ).generateWeightMatrix()

        self.predictor = esn.Reservoir(
            size=self.predictorParameters['size'],
            spectralRadius=self.predictorParameters['spectralRadius'],
            inputScaling=self.predictorParameters['inputScaling'],
            reservoirScaling=self.predictorParameters['reservoirScaling'],
            leakingRate=self.predictorParameters['leakingRate'],
            initialTransient=self.predictorParameters['initialTransient'],
            inputData=features,
            outputData=self.outputData,
            inputWeightRandom=inputWeightRandom,
            reservoirWeightRandom=reservoirWeightRandom,
            reservoirActivationFunction=self.
            predictorParameters['reservoirActivation'],
            outputActivationFunction=self.
            predictorParameters['outputActivation'])
        # Train the predictor network
        self.predictor.trainReservoir()
data = data[:5000].reshape((5000, 1))

# Number of points - 5000
trainingData, testingData = util.splitData2(data, 0.4)
nTesting = testingData.shape[0]

# Form feature vectors
inputTrainingData, outputTrainingData = util.formFeatureVectors(trainingData)

# Tune the network
size = 256
initialTransient = 50

# Input-to-reservoir 60% connected
inputWeight = topology.RandomInputTopology(
    inputSize=inputTrainingData.shape[1],
    reservoirSize=size,
    inputConnectivity=1.0).generateWeightMatrix()

# Reservoir-to-reservoir 50% connected
reservoirWeight = topology.RandomReservoirTopology(
    size=size, connectivity=0.92).generateWeightMatrix()

res = ESN.Reservoir(size=size,
                    inputData=inputTrainingData,
                    outputData=outputTrainingData,
                    spectralRadius=0.79,
                    inputScaling=0.5,
                    reservoirScaling=0.5,
                    leakingRate=0.3,
                    initialTransient=initialTransient,
                    inputWeightRandom=inputWeight,
Exemplo n.º 6
0
# Remove the outliers
resampledSeries = util.detectAndRemoveOutliers(resampledSeries)

# Step 3 - Rescale the series
normalizedSeries = util.scaleSeriesStandard(resampledSeries)
del resampledSeries

# Step 4 - Split the data into training and testing series
trainingSeries, testingSeries = util.splitIntoTrainingAndTestingSeries(normalizedSeries, horizon)

# Step 4 - Form feature and target vectors
featureVectors, targetVectors = util.formContinousFeatureAndTargetVectors(trainingSeries, depth)

# Input-to-reservoir fully connected
size = 2000
inputConnMatrix = topology.RandomInputTopology(inputSize=featureVectors.shape[1], reservoirSize=size, inputConnectivity=0.7).generateConnectivityMatrix()
correlationMatrix = util.getCorrelationMatrix(featureVectors, targetVectors, size)
inputWeightMatrix = inputConnMatrix * correlationMatrix


# Reservoir-to-reservoir fully connected
reservoirWeight = topology.RandomReservoirTopology(size=size, connectivity=0.4).generateWeightMatrix()

res = ESN.Reservoir(size=size,
                    inputData=featureVectors,
                    outputData=targetVectors,
                    spectralRadius=0.85,
                    inputScaling=0.0019,
                    reservoirScaling=0.07,
                    leakingRate=0.17,
                    initialTransient=0,