Ejemplo n.º 1
0
def gradient_check():
    error_function = lambda o: o.sum()
    rl = RecurrentLayer(3, 2, IdentityActivator(), 1e-3)
    x, d = data_set()
    rl.forward(x[0])
    rl.forward(x[1])
    sensitivity_array = np.ones(rl.state_list[-1].shape, dtype=np.float64)
    rl.backward(sensitivity_array, IdentityActivator())
    epsilon = 10e-4
    for i in range(rl.W.shape[0]):
        for j in range(rl.W.shape[1]):
            rl.W[i, j] += epsilon
            rl.reset_state()
            rl.forward(x[0])
            rl.forward(x[1])
            err1 = error_function(rl.state_list[-1])
            rl.W[i, j] -= 2 * epsilon
            rl.reset_state()
            rl.forward(x[0])
            rl.forward(x[1])
            err2 = error_function(rl.state_list[-1])
            expect_grad = (err1 - err2) / (2 * epsilon)
            rl.W[i, j] += epsilon
            print 'weights(%d,%d): expected - actural %f - %f' % (
                i, j, expect_grad, rl.gradient[i, j])
Ejemplo n.º 2
0
def gradient_check():
    '''
    梯度检查
    '''
    print("gradient_check Test!")
    # 设计一个误差函数,取所有节点输出项之和
    error_function = lambda o: o.sum()
    rl = RecurrentLayer(3, 2, IdentityActivator(), 1e-3)
    # 计算forward值
    x, d = data_set()
    rl.forward(x[0])
    rl.forward(x[1])
    # 求取sensitivity map
    sensitivity_array = np.ones(rl.state_list[-1].shape, dtype=np.float64)
    # 计算梯度
    rl.backward(sensitivity_array, IdentityActivator())
    # 检查梯度
    epsilon = 10e-4
    for i in range(rl.W.shape[0]):
        for j in range(rl.W.shape[1]):
            rl.W[i, j] += epsilon
            rl.reset_state()
            rl.forward(x[0])
            rl.forward(x[1])
            err1 = error_function(rl.state_list[-1])
            rl.W[i, j] -= 2 * epsilon
            rl.reset_state()
            rl.forward(x[0])
            rl.forward(x[1])
            err2 = error_function(rl.state_list[-1])
            expect_grad = (err1 - err2) / (2 * epsilon)
            rl.W[i, j] += epsilon
            print('weights(%d,%d): expected - actural %f - %f' %
                  (i, j, expect_grad, rl.gradient[i, j]))
Ejemplo n.º 3
0
def init_test():
    a = np.array([[[0, 1, 1, 0, 2], [2, 2, 2, 2, 1], [1, 0, 0, 2, 0],
                   [0, 1, 1, 0, 0], [1, 2, 0, 0, 2]],
                  [[1, 0, 2, 2, 0], [0, 0, 0, 2, 0], [1, 2, 1, 2, 1],
                   [1, 0, 0, 0, 0], [1, 2, 1, 1, 1]],
                  [[
                      2,
                      1,
                      2,
                      0,
                      0,
                  ], [1, 0, 0, 1, 0], [0, 2, 1, 0, 1], [0, 1, 2, 2, 2],
                   [2, 1, 0, 0, 1]]])
    b = np.array([[[0, 1, 1], [2, 2, 2], [1, 0, 0]],
                  [[1, 0, 2], [0, 0, 0], [1, 2, 1]]])
    c1 = ConvLayer(5, 5, 3, 3, 3, 2, 1, 2, IdentityActivator(), 0.001)
    c1.filters[0].weights = np.array([[[-1, 1, 0], [0, 1, 0], [0, 1, 1]],
                                      [[-1, -1, 0], [0, 0, 0], [0, -1, 0]],
                                      [[0, 0, -1], [0, 1, 0], [1, -1, -1]]],
                                     dtype=np.float64)
    c1.filters[0].bias = 1
    c1.filters[1].weights = np.array([[[1, 1, -1], [-1, -1, 1], [0, -1, 1]],
                                      [[0, 1, 0], [-1, 0, -1], [-1, 1, 0]],
                                      [[-1, 0, 0], [-1, 0, 1], [-1, 0, 0]]],
                                     dtype=np.float64)
    return a, b, c1
Ejemplo n.º 4
0
def init_test():
    # channel=3*height=5*width=5
    a = np.array([[[0, 1, 1, 0, 2], [2, 2, 2, 2, 1], [1, 0, 0, 2, 0],
                   [0, 1, 1, 0, 0], [1, 2, 0, 0, 2]],
                  [[1, 0, 2, 2, 0], [0, 0, 0, 2, 0], [1, 2, 1, 2, 1],
                   [1, 0, 0, 0, 0], [1, 2, 1, 1, 1]],
                  [[2, 1, 2, 0, 0], [1, 0, 0, 1, 0], [0, 2, 1, 0, 1],
                   [0, 1, 2, 2, 2], [2, 1, 0, 0, 1]]])
    #3*3*3
    b = np.array([[[0, 1, 1], [2, 2, 2], [1, 0, 0]],
                  [[1, 0, 2], [0, 0, 0], [1, 2, 1]]])

    # def ConvLayer.__init__(self, input_width, input_height,
    #              channel_number, filter_width,
    #              filter_height, filter_number,
    #              zero_padding, stride, activator,
    #              learning_rate):
    cl = ConvLayer(5, 5, 3, 3, 3, 2, 1, 2, IdentityActivator(), 0.001)
    cl.filters[0].weights = np.array([[[-1, 1, 0], [0, 1, 0], [0, 1, 1]],
                                      [[-1, -1, 0], [0, 0, 0], [0, -1, 0]],
                                      [[0, 0, -1], [0, 1, 0], [1, -1, -1]]],
                                     dtype=np.float64)
    cl.filters[0].bias = 1
    cl.filters[1].weights = np.array([[[1, 1, -1], [-1, -1, 1], [0, -1, 1]],
                                      [[0, 1, 0], [-1, 0, -1], [-1, 1, 0]],
                                      [[-1, 0, 0], [-1, 0, 1], [-1, 0, 0]]],
                                     dtype=np.float64)
    return a, b, cl
Ejemplo n.º 5
0
def test_bp():
    a, b, cl = init_test()
    cl.backward(a, b, IdentityActivator())
    cl.update()
    print(cl.filters[0])

    print(cl.filters[1])
Ejemplo n.º 6
0
def gradient_check():
    '''
    梯度检查
    '''
    # 设计一个误差函数,取所有节点输出项之和
    error_function = lambda o: o.sum()

    # 计算forward值
    a, b, cl = init_test()
    cl.forward(a)

    # 求取sensitivity map
    sensitivity_array = np.ones(cl.output_array.shape, dtype=np.float64)
    # 计算梯度
    cl.backward(a, sensitivity_array, IdentityActivator())
    # 检查梯度
    epsilon = 10e-4
    for d in range(cl.filters[0].weights_grad.shape[0]):
        for i in range(cl.filters[0].weights_grad.shape[1]):
            for j in range(cl.filters[0].weights_grad.shape[2]):
                cl.filters[0].weights[d, i, j] += epsilon
                cl.forward(a)
                err1 = error_function(cl.output_array)
                cl.filters[0].weights[d, i, j] -= 2 * epsilon
                cl.forward(a)
                err2 = error_function(cl.output_array)
                expect_grad = (err1 - err2) / (2 * epsilon)
                cl.filters[0].weights[d, i, j] += epsilon
                print('weights(%d,%d,%d): expected - actural %f - %f' %
                      (d, i, j, expect_grad, cl.filters[0].weights_grad[d, i,
                                                                        j]))
Ejemplo n.º 7
0
def gradient_check():
    error_function = lambda o: o.sum()
    rnn = RecursiveLayer(2, 2, IdentityActivator(), 1e-3)
    x, d = data_set()
    rnn.forward(x[0], x[1])
    rnn.forward(rnn.root, x[2])
    sensitivity_array = np.ones((rnn.node_width, 1), dtype=np.float64)
    rnn.backward(sensitivity_array)
    epsilon = 10e-4
    for i in range(rnn.W.shape[0]):
        for j in range(rnn.W.shape[1]):
            rnn.W[i, j] += epsilon
            rnn.reset_state()
            rnn.forward(x[0], x[1])
            rnn.forward(rnn.root, x[2])
            err1 = error_function(rnn.root.data)
            rnn.W[i, j] -= 2 * epsilon
            rnn.reset_state()
            rnn.forward(x[0], x[1])
            rnn.forward(rnn.root, x[2])
            err2 = error_function(rnn.root.data)
            expect_grad = (err1 - err2) / (2 * epsilon)
            rnn.W[i, j] += epsilon
            print 'weights(%d,%d): expected - actural %.4e - %.4e' % (
                i, j, expect_grad, rnn.W_grad[i, j])
    return rnn
Ejemplo n.º 8
0
def test():
    l = LstmLayer(3, 2, 1e-3)
    x, d = data_set()
    l.forward(x[0])
    l.forward(x[1])
    l.backward(x[1], d, IdentityActivator())
    return l
Ejemplo n.º 9
0
def gradient_check():
    error_function = lambda o: o.sum()
    lstm = LstmLayer(3, 2, 1e-3)
    x, d = data_set()
    lstm.forward(x[0])
    lstm.forward(x[1])
    sensitivity_array = np.ones(lstm.h_list[-1].shape, dtype=np.float64)
    lstm.backward(x[1], sensitivity_array, IdentityActivator())
    epsilon = 10e-4
    for i in range(lstm.Wfh.shape[0]):
        for j in range(lstm.Wfh.shape[1]):
            lstm.Wfh[i, j] += epsilon
            lstm.reset_state()
            lstm.forward(x[0])
            lstm.forward(x[1])
            err1 = error_function(lstm.h_list[-1])
            lstm.Wfh[i, j] -= 2 * epsilon
            lstm.reset_state()
            lstm.forward(x[0])
            lstm.forward(x[1])
            err2 = error_function(lstm.h_list[-1])
            expect_grad = (err1 - err2) / (2 * epsilon)
            lstm.Wfh[i, j] += epsilon
            print 'weights(%d,%d): expected - actural %.4e - %.4e' % (
                i, j, expect_grad, lstm.Wfh_grad[i, j])
    return lstm
Ejemplo n.º 10
0
def test():
    lstm = LstmLayer(3, 2, 1e-3)
    X, D = data_set()
    lstm.forward(X[0])
    lstm.forward(X[1])
    lstm.backward(X[1], D, IdentityActivator())
    return lstm
Ejemplo n.º 11
0
def init_test():
    # 3*5*5
    a = np.array([[[0, 1, 1, 0, 2], [2, 2, 2, 2, 1], [1, 0, 0, 2, 0],
                   [0, 1, 1, 0, 0], [1, 2, 0, 0, 2]],
                  [[1, 0, 2, 2, 0], [0, 0, 0, 2, 0], [1, 2, 1, 2, 1],
                   [1, 0, 0, 0, 0], [1, 2, 1, 1, 1]],
                  [[2, 1, 2, 0, 0], [1, 0, 0, 1, 0], [0, 2, 1, 0, 1],
                   [0, 1, 2, 2, 2], [2, 1, 0, 0, 1]]])
    # 2*3*3
    b = np.array([[[0, 1, 1], [2, 2, 2], [1, 0, 0]],
                  [[1, 0, 2], [0, 0, 0], [1, 2, 1]]])
    # parameters:(input_width, input_height, channel_number, filter_width, filter_height, filter_number,zero_padding, stride, activator, learning_rate):
    cl = ConvLayer(5, 5, 3, 3, 3, 2, 1, 2, IdentityActivator(), 0.001)
    # 卷积核为3*3*3
    cl.filters[0].weights = np.array([[[-1, 1, 0], [0, 1, 0], [0, 1, 1]],
                                      [[-1, -1, 0], [0, 0, 0], [0, -1, 0]],
                                      [[0, 0, -1], [0, 1, 0], [1, -1, -1]]],
                                     dtype=np.float64)
    cl.filters[0].bias = 1
    # 3*3*3
    cl.filters[1].weights = np.array([[[1, 1, -1], [-1, -1, 1], [0, -1, 1]],
                                      [[0, 1, 0], [-1, 0, -1], [-1, 1, 0]],
                                      [[-1, 0, 0], [-1, 0, 1], [-1, 0, 0]]],
                                     dtype=np.float64)
    return a, b, cl
Ejemplo n.º 12
0
def gradient_check():
    '''
    梯度检查
    我们只对Wfh做了检查,读者可以自行增加对其他梯度的检查
    '''
    # 设计一个误差函数,取所有节点输出项之和
    error_function = lambda o: o.sum()
    lstm = LstmLayer(3, 2, 1e-3)
    # 计算forward值
    x, d = data_set()
    lstm.forward(x[0])
    lstm.forward(x[1])
    # 求取 sensitivity map
    sensitivity_array = np.ones(lstm.h_list[-1].shape, dtype=np.float64)
    # 计算梯度
    lstm.backward(x[1], sensitivity_array, IdentityActivator())
    # 检查梯度
    epsilon = 10e-4
    for i in range(lstm.Wfh.shape[0]):
        for j in range(lstm.Wfh.shape[1]):
            lstm.Wfh[i, j] += epsilon
            lstm.reset_state()
            lstm.forward(x[0])
            lstm.forward(x[1])
            err1 = error_function(lstm.h_list[-1])
            lstm.Wfh[i, j] -= 2 * epsilon
            lstm.reset_state()
            lstm.forward(x[0])
            lstm.forward(x[1])
            err2 = error_function(lstm.h_list[-1])
            expect_grad = (err1 - err2) / (2 * epsilon)
            lstm.Wfh[i, j] += epsilon
            print('weights(%d,%d): expected - actural %.4e - %.4e' % (i, j, expect_grad, lstm.Wfh_grad[i, j]))
    return lstm
Ejemplo n.º 13
0
def test():
    a, b, cl = init_test()
    cl.forward(a)
    print "前向传播结果:", cl.output_array
    cl.backward(a, b, IdentityActivator())
    cl.update()
    print "反向传播后更新得到的filter1:", cl.filters[0]
    print "反向传播后更新得到的filter2:", cl.filters[1]
Ejemplo n.º 14
0
def test():
    lstm = LstmLayer(3, 2, 1e-3)
    x, d = data_set()
    lstm.forward(x[0])
    lstm.forward(x[1])
    sensitivity_array = np.ones(lstm.h_list[-1].shape, dtype=np.float64)
    lstm.backward(x[1], sensitivity_array, IdentityActivator())
    print lstm
Ejemplo n.º 15
0
def test_bp():
    a, b, cl = init_test()
    #完成计算误差 + 计算梯度
    cl.backward(a, b, IdentityActivator())
    #更新权重参数
    cl.update()
    print(cl.filters[0])
    print(cl.filters[1])
Ejemplo n.º 16
0
 def __init__(self, state_size, action_size, seed):
     
     super(QNetwork, self).__init__()   # the line you give simply calls the __init__ method of ClassNames parent class.
     self.seed = seed.random(seed)
     self.fc1 = FCLayer(state_size, 256,ReluActivator(),learning_rate)
     self.fc2 = FCLayer(256,128,ReluActivator(),learning_rate)
     self.fc3 = FCLayer(128,64,ReluActivator(),learning_rate)
     self.out = FCLayer(64, action_size,IdentityActivator() ,learning_rate)
     self.data = [self.fc1.parameters,self.fc2.parameters,self.fc3.parameters,self.out.parameters]
Ejemplo n.º 17
0
def test_bp():
    # a : 输入【5,5,3】
    # sensitivity map【2, 3,3】
    a, b, cl = init_test()
    print(cl)
    cl.backward(a, b, IdentityActivator())
    cl.update()
    print(cl.filters[0])
    print(cl.filters[1])
Ejemplo n.º 18
0
def test():
    children, d = data_set()
    rnn = RecursiveLayer(2, 2, IdentityActivator(), 1e-3)
    rnn.forward(children[0], children[1])
    rnn.dump()
    rnn.forward(rnn.root, children[2])
    rnn.dump()
    rnn.backward(d)
    rnn.dump(dump_grad='true')
    return rnn
Ejemplo n.º 19
0
def gradient_check():
    error_function = lambda o: o.sum()
    a, b, c1 = init_test()
    c1.forward(a)
    sensitivity_array = np.ones(c1.output_array.shape, dtype=np.float64)
    c1.backward(a, sensitivity_array, IdentityActivator())
    epsilon = 10e-4
    for d in range(c1.filters[0].weights_grad.shape[0]):
        for i in range(c1.filters[0].weights_grad.shape[1]):
            for j in range(c1.filters[0].weights_grad.shape[2]):
                c1.filters[0].weights[d, i, j] += epsilon
                c1.forward(a)
                err1 = error_function(c1.output_array)
                c1.filters[0].weights[d, i, j] -= 2 * epsilon
                c1.forward(a)
                err2 - error_function(c1.output_array)
                expect_grad = (err1 - err2) / (2 * epsilon)
                c1.filters[0].weights[d, i, j] += epsilon
                print('weights(%d,%d,%d): expected - actural %f - %f' %
                      (d, i, j, expect_grad, c1.filters[0].weights_grad[d, i,
                                                                        j]))
Ejemplo n.º 20
0
def init_test():
    # 输入【3, 5,5】
    a = np.array([[[0, 1, 1, 0, 2], [2, 2, 2, 2, 1], [1, 0, 0, 2, 0],
                   [0, 1, 1, 0, 0], [1, 2, 0, 0, 2]],
                  [[1, 0, 2, 2, 0], [0, 0, 0, 2, 0], [1, 2, 1, 2, 1],
                   [1, 0, 0, 0, 0], [1, 2, 1, 1, 1]],
                  [[2, 1, 2, 0, 0], [1, 0, 0, 1, 0], [0, 2, 1, 0, 1],
                   [0, 1, 2, 2, 2], [2, 1, 0, 0, 1]]])
    #sensitivity map【2, 3,3】
    b = np.array([[[0, 1, 1], [2, 2, 2], [1, 0, 0]],
                  [[1, 0, 2], [0, 0, 0], [1, 2, 1]]])
    cl = ConvLayer(5, 5, 3, 3, 3, 2, 1, 2, IdentityActivator(), 0.001)
    # cl = ConvLayer(5, 5, 3, 3, 3, 2, 1, 2, ReluActivator(), 0.001)
    #重新对Filter的权重进行赋值
    cl.filters[0].weights = np.array([[[-1, 1, 0], [0, 1, 0], [0, 1, 1]],
                                      [[-1, -1, 0], [0, 0, 0], [0, -1, 0]],
                                      [[0, 0, -1], [0, 1, 0], [1, -1, -1]]],
                                     dtype=np.float64)
    cl.filters[0].bias = 1
    cl.filters[1].weights = np.array([[[1, 1, -1], [-1, -1, 1], [0, -1, 1]],
                                      [[0, 1, 0], [-1, 0, -1], [-1, 1, 0]],
                                      [[-1, 0, 0], [-1, 0, 1], [-1, 0, 0]]],
                                     dtype=np.float64)
    return a, b, cl