コード例 #1
0
ファイル: Operator.py プロジェクト: BinWang-shu/MyPythonCNN
    def __init__(self, input_variable=Variable, output_num=int, name=str):
        if not isinstance(input_variable, Variable):
            raise Exception(
                "Operator Conv2D name: %s's input_variable is not instance of Variable"
                % name)

        self.batch_size = input_variable.shape[0]
        input_len = reduce(lambda x, y: x * y, input_variable.shape[1:])
        self.output_num = output_num
        self.weights = Variable([input_len, self.output_num],
                                name='weights',
                                scope=name,
                                init='const',
                                learnable=True)
        self.bias = Variable([self.output_num],
                             name='bias',
                             scope=name,
                             init='const',
                             learnable=True)

        self.output_variables = Variable([self.batch_size, self.output_num],
                                         name='out',
                                         scope=name)
        self.input_variables = input_variable
        Operator.__init__(self, name, self.input_variables,
                          self.output_variables)
コード例 #2
0
ファイル: Operator.py プロジェクト: BinWang-shu/MyPythonCNN
    def __init__(self, predict=Variable, label=Variable, name=str):
        self.batch_size = predict.shape[0]
        self.input_variables = [predict, label]
        self.loss = Variable([1], name='loss', scope=name, init='None')
        self.prediction = Variable(predict.shape,
                                   name='prediction',
                                   scope=name)
        self.softmax = np.zeros(self.prediction.shape)

        self.output_variables = [self.loss, self.prediction]
        Operator.__init__(self, name, self.input_variables,
                          self.output_variables)
コード例 #3
0
ファイル: Operator.py プロジェクト: BinWang-shu/MyPythonCNN
    def __init__(self,
                 input_variable,
                 kernel_shape=list,
                 name=str,
                 stride=1,
                 padding=0):
        # kernel_shape = [ksize, ksize, input_channels, output_channels]
        for i in kernel_shape:
            if not isinstance(i, int):
                raise Exception(
                    "Operator Conv2d name: %s kernel_shape is not list of int"
                    % name)

        if not isinstance(input_variable, Variable):
            raise Exception(
                "Operator Conv2d name: %s's input_variable is not instance of Variable"
                % name)

        if len(input_variable.shape) != 4:
            raise Exception(
                "Operator Conv2d name: %s's input_variable's shape != 4d Variable!"
                % name)

        self.ksize = kernel_shape[0]
        self.stride = stride
        self.output_num = kernel_shape[-1]
        self.padding = padding

        self.padding = padding

        self.weights = Variable(kernel_shape,
                                scope=name,
                                name='weights',
                                learnable=True)
        self.bias = Variable([self.output_num],
                             scope=name,
                             name='bias',
                             learnable=True)
        self.batchsize = input_variable.shape[0]

        # w = (W + 2*pad - kernel) / stride + 1
        H = int((input_variable.shape[1] + 2 * padding - self.ksize) /
                self.stride + 1)
        W = int((input_variable.shape[2] + 2 * padding - self.ksize) /
                self.stride + 1)
        _output_shape = [self.batchsize, H, W, self.output_num]

        self.output_variables = Variable(_output_shape, name='out', scope=name)
        self.input_variables = input_variable

        Operator.__init__(self, name, self.input_variables,
                          self.output_variables)
コード例 #4
0
ファイル: Operator.py プロジェクト: BinWang-shu/MyPythonCNN
    def __init__(self, input_variable=Variable, ksize=2, stride=2, name=str):

        if not isinstance(input_variable, Variable):
            raise Exception(
                "Operator Conv2D name: %s's input_variable is not instance of Variable"
                % name)

        self.ksize = ksize
        self.stride = stride
        self.batch_size = input_variable.shape[0]
        self.output_channels = input_variable.shape[-1]
        # self.index = np.zeros(input_variable.shape)

        self.input_variables = input_variable
        self.output_H = int((input_variable.shape[1] - self.ksize) /
                            self.stride + 1)
        self.output_W = int((input_variable.shape[2] - self.ksize) /
                            self.stride + 1)
        _output_shape = [
            self.batch_size, self.output_H, self.output_W, self.output_channels
        ]
        self.output_variables = Variable(_output_shape, name='out', scope=name)

        Operator.__init__(self, name, self.input_variables,
                          self.output_variables)
コード例 #5
0
ファイル: Activation.py プロジェクト: BinWang-shu/MyPythonCNN
 def __init__(self, input_variable=Variable, name=str, alpha = 0.25):
     self.input_variables = input_variable
     self.output_variables = Variable(self.input_variables.shape, name='out', scope=name)
     self.alpha = alpha
     self.momentum  = 0.9
     self.eta = 1e-4
     Operator.__init__(self, name, self.input_variables, self.output_variables)
コード例 #6
0
ファイル: Operator.py プロジェクト: BinWang-shu/MyPythonCNN
    def __init__(self,
                 input_variable=Variable,
                 name=str,
                 phase='train',
                 prob=0.5):
        self.input_variables = input_variable
        self.output_variables = Variable(shape=input_variable.shape,
                                         scope=name,
                                         name='out')
        self.prob = prob
        self.phase = phase
        self.index = np.ones(input_variable.shape)

        Operator.__init__(self, name, self.input_variables,
                          self.output_variables)
コード例 #7
0
ファイル: Operator.py プロジェクト: BinWang-shu/MyPythonCNN
            operator.parent.append(_input.name)
        for output in output_variable:
            output.parent.append(operator.name)
            operator.child.append(output.name)

    else:
        raise Exception('Operator name %s input,output list error' %
                        operator.name)


def im2col(image, ksize, stride):
    # image is a 4d tensor([batchsize, width ,height, channel])
    # print image.shape
    # print image
    image_col = []
    for i in range(0, image.shape[1] - ksize + 1, stride):
        for j in range(0, image.shape[2] - ksize + 1, stride):
            col = image[:, i:i + ksize, j:j + ksize, :].reshape([-1])

            image_col.append(col)
    image_col = np.array(image_col)

    return image_col


if __name__ == "__main__":
    A = Variable((2, 2, 3, 3), 'A')
    B = Variable((3, 3, 4, 4), 'B')
    print(A.shape)
    print(A.name)
コード例 #8
0
ファイル: Activation.py プロジェクト: BinWang-shu/MyPythonCNN
 def __init__(self, input_variable=Variable, name=str):
     self.input_variables = input_variable
     self.output_variables = Variable(self.input_variables.shape, name='out', scope=name)
     Operator.__init__(self, name, self.input_variables, self.output_variables)
コード例 #9
0
ファイル: Activation.py プロジェクト: BinWang-shu/MyPythonCNN
 def __init__(self, input_variable=Variable, name=str, alpha = 0.01):
     self.input_variables = input_variable
     self.output_variables = Variable(self.input_variables.shape, name='out', scope=name)
     self.alpha = alpha
     Operator.__init__(self, name, self.input_variables, self.output_variables)
コード例 #10
0
ファイル: Activation.py プロジェクト: BinWang-shu/MyPythonCNN
            for child in self.child:
                GLOBAL_VARIABLE_SCOPE[child].diff_eval()
            self.alpha = self.momentum * self.alpha + self.eta * np.sum(np.minimum(self.input_variables.data, 0))
            self.input_variables.diff = self.output_variables.diff
            self.input_variables.diff[self.input_variables.data <= 0] *= self.alpha
            self.wait_forward = True

            return



if __name__ == "__main__":

    # check grad
    shape = ([10])
    a = Variable(shape, 'a')
    # print a.name
    # print 'a.data ', a.data
    # print 'a.diff ', a.diff

    test_layer = Prelu(a, 'Tanh')
    b = test_layer.output_variables

    epsilon = 1e-5

    a.data -= epsilon
    print ('a -eps ', a.data)
    out1 = b.eval()

    # refresh graph
    b.wait_bp = False