Ejemplo n.º 1
0
 def _createWeightsTensorAndConvolve(self, rng, filterShape, convWInitMethod, 
                                     inputToConvTrain, inputToConvVal, inputToConvTest,
                                     inputToConvShapeTrain, inputToConvShapeVal, inputToConvShapeTest) :
     # Behaviour: Create W, set self._W, set self.params, convolve, return ouput and outputShape.
     # The created filters are either 1-dimensional (rank=1) or 2-dim (rank=2), depending  on the self._rank
     # If 1-dim: rSubconv is the input convolved with the row-1dimensional filter.
     # If 2-dim: rSubconv is the input convolved with the RC-2D filter, cSubconv with CZ-2D filter, zSubconv with ZR-2D filter. 
     
     #----- Initialise the weights and Convolve for 3 separate, low rank filters, R,C,Z. -----
     # W shape: [#FMs of this layer, #FMs of Input, rKernDim, cKernDim, zKernDim]
     
     rSubconvFilterShape = [ filterShape[0]//3, filterShape[1], filterShape[2], 1 if self._rank == 1 else filterShape[3], 1 ]
     rSubconvW = createAndInitializeWeightsTensor(rSubconvFilterShape, convWInitMethod, rng)
     rSubconvTupleWithOuputAndShapeTrValTest = convolveWithGivenWeightMatrix(rSubconvW, rSubconvFilterShape, inputToConvTrain, inputToConvVal, inputToConvTest, inputToConvShapeTrain, inputToConvShapeVal, inputToConvShapeTest)
     
     cSubconvFilterShape = [ filterShape[0]//3, filterShape[1], 1, filterShape[3], 1 if self._rank == 1 else filterShape[4] ]
     cSubconvW = createAndInitializeWeightsTensor(cSubconvFilterShape, convWInitMethod, rng)
     cSubconvTupleWithOuputAndShapeTrValTest = convolveWithGivenWeightMatrix(cSubconvW, cSubconvFilterShape, inputToConvTrain, inputToConvVal, inputToConvTest, inputToConvShapeTrain, inputToConvShapeVal, inputToConvShapeTest)
     
     numberOfFmsForTotalToBeExact = filterShape[0] - 2*(filterShape[0]//3) # Cause of possibly inexact integer division.
     zSubconvFilterShape = [ numberOfFmsForTotalToBeExact, filterShape[1], 1 if self._rank == 1 else filterShape[2], 1, filterShape[4] ]
     zSubconvW = createAndInitializeWeightsTensor(zSubconvFilterShape, convWInitMethod, rng)
     zSubconvTupleWithOuputAndShapeTrValTest = convolveWithGivenWeightMatrix(zSubconvW, zSubconvFilterShape, inputToConvTrain, inputToConvVal, inputToConvTest, inputToConvShapeTrain, inputToConvShapeVal, inputToConvShapeTest)
     
     # Set the W attribute and trainable parameters.
     self._WperSubconv = [rSubconvW, cSubconvW, zSubconvW] # Bear in mind that these sub tensors have different shapes! Treat carefully.
     self.params = self._WperSubconv + self.params
     
     # concatenate together.
     (concatSubconvOutputsTrain, concatOutputShapeTrain) = self._cropSubconvOutputsToSameDimsAndConcatenateFms(rSubconvTupleWithOuputAndShapeTrValTest[0], rSubconvTupleWithOuputAndShapeTrValTest[3],
                                                                                                     cSubconvTupleWithOuputAndShapeTrValTest[0], cSubconvTupleWithOuputAndShapeTrValTest[3],
                                                                                                     zSubconvTupleWithOuputAndShapeTrValTest[0], zSubconvTupleWithOuputAndShapeTrValTest[3],
                                                                                                     filterShape)
     (concatSubconvOutputsVal, concatOutputShapeVal) = self._cropSubconvOutputsToSameDimsAndConcatenateFms(rSubconvTupleWithOuputAndShapeTrValTest[1], rSubconvTupleWithOuputAndShapeTrValTest[4],
                                                                                                     cSubconvTupleWithOuputAndShapeTrValTest[1], cSubconvTupleWithOuputAndShapeTrValTest[4],
                                                                                                     zSubconvTupleWithOuputAndShapeTrValTest[1], zSubconvTupleWithOuputAndShapeTrValTest[4],
                                                                                                     filterShape)
     (concatSubconvOutputsTest, concatOutputShapeTest) = self._cropSubconvOutputsToSameDimsAndConcatenateFms(rSubconvTupleWithOuputAndShapeTrValTest[2], rSubconvTupleWithOuputAndShapeTrValTest[5],
                                                                                                     cSubconvTupleWithOuputAndShapeTrValTest[2], cSubconvTupleWithOuputAndShapeTrValTest[5],
                                                                                                     zSubconvTupleWithOuputAndShapeTrValTest[2], zSubconvTupleWithOuputAndShapeTrValTest[5],
                                                                                                     filterShape)
     
     return (concatSubconvOutputsTrain, concatSubconvOutputsVal, concatSubconvOutputsTest, concatOutputShapeTrain, concatOutputShapeVal, concatOutputShapeTest)
Ejemplo n.º 2
0
 def _createWeightsTensorAndConvolve(self, rng, filterShape, convWInitMethod, 
                                     inputToConvTrain, inputToConvVal, inputToConvTest,
                                     inputToConvShapeTrain, inputToConvShapeVal, inputToConvShapeTest) :
     #-----------------------------------------------
     #------------------ Convolution ----------------
     #-----------------------------------------------
     #----- Initialise the weights -----
     # W shape: [#FMs of this layer, #FMs of Input, rKernDim, cKernDim, zKernDim]
     self._W = createAndInitializeWeightsTensor(filterShape, convWInitMethod, rng)
     self.params = [self._W] + self.params
     
     #---------- Convolve --------------
     tupleWithOuputAndShapeTrValTest = convolveWithGivenWeightMatrix(self._W, filterShape, inputToConvTrain, inputToConvVal, inputToConvTest, inputToConvShapeTrain, inputToConvShapeVal, inputToConvShapeTest)
     
     return tupleWithOuputAndShapeTrValTest
Ejemplo n.º 3
0
    def _createWeightsTensorAndConvolve(self, rng, filterShape,
                                        convWInitMethod, inputToConvTrain,
                                        inputToConvVal, inputToConvTest,
                                        inputToConvShapeTrain,
                                        inputToConvShapeVal,
                                        inputToConvShapeTest):
        #-----------------------------------------------
        #------------------ Convolution ----------------
        #-----------------------------------------------
        #----- Initialise the weights -----
        # W shape: [#FMs of this layer, #FMs of Input, rKernDim, cKernDim, zKernDim]
        self._W = createAndInitializeWeightsTensor(filterShape,
                                                   convWInitMethod, rng)
        self.params = [self._W] + self.params

        #---------- Convolve --------------
        tupleWithOuputAndShapeTrValTest = convolveWithGivenWeightMatrix(
            self._W, filterShape, inputToConvTrain, inputToConvVal,
            inputToConvTest, inputToConvShapeTrain, inputToConvShapeVal,
            inputToConvShapeTest)

        return tupleWithOuputAndShapeTrValTest
Ejemplo n.º 4
0
    def _createWeightsTensorAndConvolve(self, rng, filterShape,
                                        convWInitMethod, inputToConvTrain,
                                        inputToConvVal, inputToConvTest,
                                        inputToConvShapeTrain,
                                        inputToConvShapeVal,
                                        inputToConvShapeTest):
        # Behaviour: Create W, set self._W, set self.params, convolve, return ouput and outputShape.
        # The created filters are either 1-dimensional (rank=1) or 2-dim (rank=2), depending  on the self._rank
        # If 1-dim: rSubconv is the input convolved with the row-1dimensional filter.
        # If 2-dim: rSubconv is the input convolved with the RC-2D filter, cSubconv with CZ-2D filter, zSubconv with ZR-2D filter.

        #----- Initialise the weights and Convolve for 3 separate, low rank filters, R,C,Z. -----
        # W shape: [#FMs of this layer, #FMs of Input, rKernDim, cKernDim, zKernDim]

        rSubconvFilterShape = [
            filterShape[0] // 3, filterShape[1], filterShape[2],
            1 if self._rank == 1 else filterShape[3], 1
        ]
        rSubconvW = createAndInitializeWeightsTensor(rSubconvFilterShape,
                                                     convWInitMethod, rng)
        rSubconvTupleWithOuputAndShapeTrValTest = convolveWithGivenWeightMatrix(
            rSubconvW, rSubconvFilterShape, inputToConvTrain, inputToConvVal,
            inputToConvTest, inputToConvShapeTrain, inputToConvShapeVal,
            inputToConvShapeTest)

        cSubconvFilterShape = [
            filterShape[0] // 3, filterShape[1], 1, filterShape[3],
            1 if self._rank == 1 else filterShape[4]
        ]
        cSubconvW = createAndInitializeWeightsTensor(cSubconvFilterShape,
                                                     convWInitMethod, rng)
        cSubconvTupleWithOuputAndShapeTrValTest = convolveWithGivenWeightMatrix(
            cSubconvW, cSubconvFilterShape, inputToConvTrain, inputToConvVal,
            inputToConvTest, inputToConvShapeTrain, inputToConvShapeVal,
            inputToConvShapeTest)

        numberOfFmsForTotalToBeExact = filterShape[0] - 2 * (
            filterShape[0] // 3)  # Cause of possibly inexact integer division.
        zSubconvFilterShape = [
            numberOfFmsForTotalToBeExact, filterShape[1],
            1 if self._rank == 1 else filterShape[2], 1, filterShape[4]
        ]
        zSubconvW = createAndInitializeWeightsTensor(zSubconvFilterShape,
                                                     convWInitMethod, rng)
        zSubconvTupleWithOuputAndShapeTrValTest = convolveWithGivenWeightMatrix(
            zSubconvW, zSubconvFilterShape, inputToConvTrain, inputToConvVal,
            inputToConvTest, inputToConvShapeTrain, inputToConvShapeVal,
            inputToConvShapeTest)

        # Set the W attribute and trainable parameters.
        self._WperSubconv = [
            rSubconvW, cSubconvW, zSubconvW
        ]  # Bear in mind that these sub tensors have different shapes! Treat carefully.
        self.params = self._WperSubconv + self.params

        # concatenate together.
        (concatSubconvOutputsTrain, concatOutputShapeTrain
         ) = self._cropSubconvOutputsToSameDimsAndConcatenateFms(
             rSubconvTupleWithOuputAndShapeTrValTest[0],
             rSubconvTupleWithOuputAndShapeTrValTest[3],
             cSubconvTupleWithOuputAndShapeTrValTest[0],
             cSubconvTupleWithOuputAndShapeTrValTest[3],
             zSubconvTupleWithOuputAndShapeTrValTest[0],
             zSubconvTupleWithOuputAndShapeTrValTest[3], filterShape)
        (concatSubconvOutputsVal, concatOutputShapeVal
         ) = self._cropSubconvOutputsToSameDimsAndConcatenateFms(
             rSubconvTupleWithOuputAndShapeTrValTest[1],
             rSubconvTupleWithOuputAndShapeTrValTest[4],
             cSubconvTupleWithOuputAndShapeTrValTest[1],
             cSubconvTupleWithOuputAndShapeTrValTest[4],
             zSubconvTupleWithOuputAndShapeTrValTest[1],
             zSubconvTupleWithOuputAndShapeTrValTest[4], filterShape)
        (concatSubconvOutputsTest, concatOutputShapeTest
         ) = self._cropSubconvOutputsToSameDimsAndConcatenateFms(
             rSubconvTupleWithOuputAndShapeTrValTest[2],
             rSubconvTupleWithOuputAndShapeTrValTest[5],
             cSubconvTupleWithOuputAndShapeTrValTest[2],
             cSubconvTupleWithOuputAndShapeTrValTest[5],
             zSubconvTupleWithOuputAndShapeTrValTest[2],
             zSubconvTupleWithOuputAndShapeTrValTest[5], filterShape)

        return (concatSubconvOutputsTrain, concatSubconvOutputsVal,
                concatSubconvOutputsTest, concatOutputShapeTrain,
                concatOutputShapeVal, concatOutputShapeTest)