def train(x, target, convol_size, convol_stride, pool_size, pool_stride,
          learning_rate, iterate):

    weight = np.random.normal(size=convol_size)
    bias = np.zeros((1))

    for i in range(iterate):

        y = conv.forward(x, weight, bias, convol_stride)

        s = sigmoid.forward(y)

        predict = pool.forward(s, pool_size, pool_stride)

        error = (predict - target)

        mse = np.average(error**2)

        print_summary(i, (iterate - i), mse, predict, target)

        error = pool.backward(s, error, pool_size, pool_stride)

        error = sigmoid.backward(y, error)

        error, weight_delta, bias_delta = conv.backward(
            x, error, weight, bias, convol_stride)

        weight -= (learning_rate * weight_delta)
        bias -= (learning_rate * bias_delta)

    return weight, bias
Beispiel #2
0
def train(x, target, learning_rate, iterate):

    inputNodes = x.shape[-1]
    outputNodes = target.shape[-1]

    h1_units = 25

    h1_weight = np.random.normal(size=(inputNodes, h1_units))
    h1_bias = np.zeros((h1_units))

    h2_weight = np.random.normal(size=(h1_units, outputNodes))
    h2_bias = np.zeros((outputNodes))

    for i in range(iterate):

        y1 = linear.forward(x, h1_weight, h1_bias)
        s1 = sigmoid.forward(y1)
        y2 = linear.forward(s1, h2_weight, h2_bias)
        predict = sigmoid.forward(y2)

        error = (predict - target)

        mse = np.average(error**2)

        print_summary(i, mse)

        error = sigmoid.backward(y2, error)

        error, h2_weight_delta, h2_bias_delta = linear.backward(
            s1, error, h2_weight, h2_bias)

        h2_weight -= (learning_rate * h2_weight_delta)
        h2_bias -= (learning_rate * h2_bias_delta)

        error = sigmoid.backward(y1, error)

        error, h1_weight_delta, h1_bias_delta = linear.backward(
            x, error, h1_weight, h1_bias)

        h1_weight -= (learning_rate * h1_weight_delta)
        h1_bias -= (learning_rate * h1_bias_delta)

    return h1_weight, h1_bias, h2_weight, h2_bias
Beispiel #3
0
def train(x, target, convol_size, convol_stride, pool_size, pool_stride, layer_count, learning_rate, iterate):

    weight_list = [np.random.normal(size=convol_size).copy()] * layer_count
    bias_list = [np.zeros((1)).copy()] * layer_count

    for i in range(iterate):

        x, target = shuffle(x, target)

        input = x

        input_conv_list = []
        input_sigmoid_list = []
        input_pool_list = []

        for l in range(layer_count):
            input_conv_list.append(input)
            c = conv.forward(input, weight_list[l], bias_list[l], convol_stride)

            input_sigmoid_list.append(c)
            s = sigmoid.forward(c)

            input_pool_list.append(s)
            p = pool.forward(s, pool_size, pool_stride)

            input = p

        predict = p

        error = (predict - target)

        mse = np.average(error**2)

        print_summary(i, iterate, mse, predict)

        for l in reversed(range(layer_count)):

            input_pool = input_pool_list[l]
            error = pool.backward(input_pool, error, pool_size, pool_stride)

            input_sigmoid = input_sigmoid_list[l]
            error = sigmoid.backward(input_sigmoid, error)

            weight = weight_list[l]
            bias = bias_list[l]

            input_conv = input_conv_list[l]
            error, weight_delta, bias_delta = conv.backward(input_conv, error, weight, bias, convol_stride)

            weight -= (learning_rate * weight_delta)
            bias -= (learning_rate * bias_delta)

    return weight_list, bias_list
Beispiel #4
0
def train(x, target, learning_rate, iterate):

    inputNodes = x.shape[-1]
    outputNodes = target.shape[-1]

    weight = np.zeros((inputNodes, outputNodes))
    bias = np.zeros((outputNodes))

    for i in range(iterate):

        y = linear.forward(x, weight, bias)
        p = sigmoid.forward(y)

        p_error = (p - target)

        loss = np.average(p_error**2)
        print_numpy('w', weight)
        print_numpy('b', bias)
        print_numpy('y', y)
        print_numpy('p', p)
        print_numpy('L', loss)

        y_error = sigmoid.backward(y, p_error)
        x_error, w_error, b_error = linear.backward(x, y_error, weight, bias)

        weight -= (learning_rate * w_error)
        bias -= (learning_rate * b_error)

        print_numpy('~p', p_error)
        print_numpy('~y', y_error)
        print_numpy('~x', x_error)
        print_numpy('~w', w_error)
        print_numpy('~b', b_error)

        print('----- epoch : ', (i + 1), '------')

    return weight, bias
Beispiel #5
0
        print_numpy('b', bias)
        print_numpy('y', y)
        print_numpy('p', p)
        print_numpy('L', loss)

        y_error = sigmoid.backward(y, p_error)
        x_error, w_error, b_error = linear.backward(x, y_error, weight, bias)

        weight -= (learning_rate * w_error)
        bias -= (learning_rate * b_error)

        print_numpy('~p', p_error)
        print_numpy('~y', y_error)
        print_numpy('~x', x_error)
        print_numpy('~w', w_error)
        print_numpy('~b', b_error)

        print('----- epoch : ', (i + 1), '------')

    return weight, bias

train_x = np.array([[5]])
target = np.array([[1]])

weight, bias = train(train_x, target, learning_rate = 0.2, iterate = 4)

y = linear.forward(train_x, weight, bias)
predict = sigmoid.forward(y)

print('predict : ', predict)
pool_stride = pool_size

train_x, train_t, test_x, test_t = loadDataSet('cnn/image/shape/train',
                                               'cnn/image/shape/target',
                                               'cnn/image/shape/test')

train_t = pool.forward(train_t, pool_size, pool_stride)
test_t = pool.forward(test_t, pool_size, pool_stride)

weight, bias = train(train_x,
                     train_t,
                     convol_size,
                     convol_stride,
                     pool_size,
                     pool_stride,
                     learning_rate=0.001,
                     iterate=50000)

y = conv.forward(test_x, weight, bias, convol_stride)
s = sigmoid.forward(y)
predict = pool.forward(s, pool_size, pool_stride)

predict = np.where(predict >= 0.5, 1.0, predict)
predict = np.where(predict < 0.5, 0.0, predict)

print('predict', predict)
print('test', test_t)

equal = np.array_equal(predict, test_t)
print('equal : ', equal)
Beispiel #7
0
        h2_weight -= (learning_rate * h2_weight_delta)
        h2_bias -= (learning_rate * h2_bias_delta)

        error = sigmoid.backward(y1, error)

        error, h1_weight_delta, h1_bias_delta = linear.backward(
            x, error, h1_weight, h1_bias)

        h1_weight -= (learning_rate * h1_weight_delta)
        h1_bias -= (learning_rate * h1_bias_delta)

    return h1_weight, h1_bias, h2_weight, h2_bias


train_x = np.array([[0, 0], [1, 1]])
target = np.array([[0], [1]])

h1_weight, h1_bias, h2_weight, h2_bias = train(train_x,
                                               target,
                                               learning_rate=0.01,
                                               iterate=5000)

y1 = linear.forward(train_x, h1_weight, h1_bias)
s1 = sigmoid.forward(y1)

y2 = linear.forward(s1, h2_weight, h2_bias)
predict = sigmoid.forward(y2)

print('predict : ', predict)