示例#1
0
def test():
    # first layer
    [W_i, W_f, W_c, W_o, U_i, U_f, U_c, U_o, b_i, b_f, b_c,
     b_o] = load_lstm_weights_from_txt('stacked_lstm_weights/cell0',
                                       hidden_size)
    # second layer
    [W1_i, W1_f, W1_c, W1_o, U1_i, U1_f, U1_c, U1_o, b1_i, b1_f, b1_c,
     b1_o] = load_lstm_weights_from_txt('stacked_lstm_weights/cell1',
                                        hidden_size)
    [W2_i, W2_f, W2_c, W2_o, U2_i, U2_f, U2_c, U2_o, b2_i, b2_f, b2_c,
     b2_o] = load_lstm_weights_from_txt('stacked_lstm_weights/cell2',
                                        hidden_size)
    [W3_i, W3_f, W3_c, W3_o, U3_i, U3_f, U3_c, U3_o, b3_i, b3_f, b3_c,
     b3_o] = load_lstm_weights_from_txt('stacked_lstm_weights/cell3',
                                        hidden_size)

    Why = np.loadtxt('stacked_lstm_weights/fc/Why.h', delimiter=',')
    by = np.loadtxt('stacked_lstm_weights/fc/by.h',
                    delimiter=',',
                    usecols=range(num_classes)).flatten()

    h0, h1, h2, h3 = {}, {}, {}, {}
    h0[-1] = np.zeros(hidden_size)
    h1[-1] = np.zeros(hidden_size)
    h2[-1] = np.zeros(hidden_size)
    h3[-1] = np.zeros(hidden_size)

    c0, c1, c2, c3 = {}, {}, {}, {}
    c0[-1] = np.zeros(hidden_size)
    c1[-1] = np.zeros(hidden_size)
    c2[-1] = np.zeros(hidden_size)
    c3[-1] = np.zeros(hidden_size)

    from models import my_lstm
    counter = 0
    for i in range(len(x_test)):
        for t in range(img_cols):
            (h0[t], c0[t]) = my_lstm(x_test[i][t], h0[t - 1], c0[t - 1], W_i,
                                     W_f, W_c, W_o, U_i, U_f, U_c, U_o, b_i,
                                     b_f, b_c, b_o)
            (h1[t], c1[t]) = my_lstm(h0[t], h1[t - 1], c1[t - 1], W1_i, W1_f,
                                     W1_c, W1_o, U1_i, U1_f, U1_c, U1_o, b1_i,
                                     b1_f, b1_c, b1_o)
            (h2[t], c2[t]) = my_lstm(h1[t], h2[t - 1], c2[t - 1], W2_i, W2_f,
                                     W2_c, W2_o, U2_i, U2_f, U2_c, U2_o, b2_i,
                                     b2_f, b2_c, b2_o)
            (h3[t], c3[t]) = my_lstm(h2[t], h3[t - 1], c3[t - 1], W3_i, W3_f,
                                     W3_c, W3_o, U3_i, U3_f, U3_c, U3_o, b3_i,
                                     b3_f, b3_c, b3_o)

        yt = np.dot(Why, h3[img_cols - 1]) + by
        counter += (np.argmax(yt) == np.argmax(y_test[i]))

    print(counter / 10000.0)
示例#2
0
def write_good_values(path_test_data, path_written_data):
    # first layer
    [W_i, W_f, W_c, W_o, U_i, U_f, U_c, U_o, b_i, b_f, b_c,
     b_o] = load_lstm_weights_from_txt('bi_lstm_weights/cell0', hidden_size)
    # second layer
    [W2_i, W2_f, W2_c, W2_o, U2_i, U2_f, U2_c, U2_o, b2_i, b2_f, b2_c,
     b2_o] = load_lstm_weights_from_txt('bi_lstm_weights/cell1', hidden_size)

    Why = np.loadtxt('bi_lstm_weights/fc/Why.h', delimiter=',')
    by = np.loadtxt('bi_lstm_weights/fc/by.h',
                    delimiter=',',
                    usecols=range(num_classes)).flatten()

    h0 = {}
    h1 = {}
    h0[-1] = np.zeros(hidden_size)
    h1[-1] = np.zeros(hidden_size)

    c0 = {}
    c1 = {}
    c0[-1] = np.zeros(hidden_size)
    c1[-1] = np.zeros(hidden_size)

    y = {}
    from models import my_lstm

    test_image = np.loadtxt(path_test_data, delimiter=',')
    for t in range(28):
        (h0[t], c0[t]) = my_lstm(test_image[t], h0[t - 1], c0[t - 1], W_i, W_f,
                                 W_c, W_o, U_i, U_f, U_c, U_o, b_i, b_f, b_c,
                                 b_o)
        (h1[t], c1[t]) = my_lstm(test_image[27 - t], h1[t - 1], c1[t - 1],
                                 W2_i, W2_f, W2_c, W2_o, U2_i, U2_f, U2_c,
                                 U2_o, b2_i, b2_f, b2_c, b2_o)
    hl_concat = np.concatenate((h0[27], h1[27]))
    yt = np.dot(Why, hl_concat) + by
    print(yt)

    # h_dict = {'h0': h0, 'h1':h1}
    with open(path_written_data, 'w') as file:
        file.write('------h0-------\n\n')
        for k, v in h0.items():
            file.write(str(k) + ' >>> ' + str(v) + '\n\n')
        file.write('------h1-------\n\n')
        for k, v in h1.items():
            file.write(str(k) + ' >>> ' + str(v) + '\n\n')

    # prepare headers
    save_lstm_headers('bi_lstm_weights/cell0')
    save_lstm_headers('bi_lstm_weights/cell1')
    save_fc_headers('bi_lstm_weights')
示例#3
0
def write_good_values(path_test_data, path_written_data):
    # first layer
    [W_i, W_f, W_c, W_o, U_i, U_f, U_c, U_o, b_i, b_f, b_c,
     b_o] = load_lstm_weights_from_txt('quant_test/cell0', hidden_size)

    Why = np.loadtxt('quant_test/fc/Why.h', delimiter=',')
    by = np.loadtxt('quant_test/fc/by.h',
                    delimiter=',',
                    usecols=range(num_classes)).flatten()

    h0, c0 = {}, {}
    h0[-1] = np.zeros(hidden_size)
    c0[-1] = np.zeros(hidden_size)

    from models import my_lstm
    test_image = np.loadtxt(path_test_data, delimiter=',')
    for t in range(28):
        (h0[t], c0[t]) = my_lstm(test_image[t], h0[t - 1], c0[t - 1], W_i, W_f,
                                 W_c, W_o, U_i, U_f, U_c, U_o, b_i, b_f, b_c,
                                 b_o)

    yt = np.dot(Why, h0[img_cols - 1]) + by
    print(yt)

    # h_dict = {'h0': h0, 'h1':h1}
    with open(path_written_data, 'w') as file:
        file.write('------h0-------\n\n')
        for k, v in h0.items():
            file.write(str(k) + ' >>> ' + str(v) + '\n\n')

    # prepare headers
    save_lstm_headers('quant_test/cell0', 0)
    save_fc_headers('quant_test')
示例#4
0
def test():
    # first layer
    [W_i, W_f, W_c, W_o, U_i, U_f, U_c, U_o, b_i, b_f, b_c,
     b_o] = load_lstm_weights_from_txt('bi_lstm_weights/cell0', hidden_size)
    # second layer
    [W2_i, W2_f, W2_c, W2_o, U2_i, U2_f, U2_c, U2_o, b2_i, b2_f, b2_c,
     b2_o] = load_lstm_weights_from_txt('bi_lstm_weights/cell1', hidden_size)

    Why = np.loadtxt('bi_lstm_weights/fc/Why.h', delimiter=',')
    by = np.loadtxt('bi_lstm_weights/fc/by.h',
                    delimiter=',',
                    usecols=range(num_classes)).flatten()

    h0 = {}
    h1 = {}
    h0[-1] = np.zeros(hidden_size)
    h1[-1] = np.zeros(hidden_size)

    c0 = {}
    c1 = {}
    c0[-1] = np.zeros(hidden_size)
    c1[-1] = np.zeros(hidden_size)

    y = {}
    from models import my_lstm
    counter = 0
    for i in range(len(x_test)):
        for t in range(28):
            (h0[t], c0[t]) = my_lstm(x_test[i][t], h0[t - 1], c0[t - 1], W_i,
                                     W_f, W_c, W_o, U_i, U_f, U_c, U_o, b_i,
                                     b_f, b_c, b_o)
            (h1[t], c1[t]) = my_lstm(x_test[i][27 - t], h1[t - 1], c1[t - 1],
                                     W2_i, W2_f, W2_c, W2_o, U2_i, U2_f, U2_c,
                                     U2_o, b2_i, b2_f, b2_c, b2_o)
        hl_concat = np.concatenate((h0[27], h1[27]))
        yt = np.dot(Why, hl_concat) + by
        counter += (np.argmax(yt) == np.argmax(y_test[i]))

    print(counter / 10000.0)
示例#5
0
x_test = np.array(test_emb)
y_test = np.array(test_labels)

x_dev = np.array(dev_emb)
y_dev = np.array(dev_labels)

x_val = np.array(val_emb)
y_val = np.array(val_labels)

x_sub = np.array(sub_emb)


# 모델 정의 & 학습
input_shape = x_dev.shape[1:]
# y_dev = y_dev.reshape(len(y_dev), 1, y_dev.shape[1])
model = models.my_lstm(input_shape, 3)

filename = 'checkpoint-epoch-{}-trial-001.h5'.format(25)
early_stopping = EarlyStopping()
checkpoint = ModelCheckpoint(filename,
                             monitor='loss',
                             verbose=1,
                             save_best_only=True,
                             mode='auto'
                             )

model.fit(x_dev, y_dev, epochs=25, verbose=1, callbacks=[checkpoint, early_stopping])

test_data = pd.read_csv('./test_stage_2.csv')
test_text = test_data["Text"]
test_df = pd.DataFrame(test_text)
示例#6
0
def write_good_values(path_test_data, path_written_data):
    # first layer
    [W_i, W_f, W_c, W_o, U_i, U_f, U_c, U_o, b_i, b_f, b_c,
     b_o] = load_lstm_weights_from_txt('stacked_lstm_weights/cell0',
                                       hidden_size)
    # second layer
    [W1_i, W1_f, W1_c, W1_o, U1_i, U1_f, U1_c, U1_o, b1_i, b1_f, b1_c,
     b1_o] = load_lstm_weights_from_txt('stacked_lstm_weights/cell1',
                                        hidden_size)
    [W2_i, W2_f, W2_c, W2_o, U2_i, U2_f, U2_c, U2_o, b2_i, b2_f, b2_c,
     b2_o] = load_lstm_weights_from_txt('stacked_lstm_weights/cell2',
                                        hidden_size)
    [W3_i, W3_f, W3_c, W3_o, U3_i, U3_f, U3_c, U3_o, b3_i, b3_f, b3_c,
     b3_o] = load_lstm_weights_from_txt('stacked_lstm_weights/cell3',
                                        hidden_size)

    Why = np.loadtxt('stacked_lstm_weights/fc/Why.h', delimiter=',')
    by = np.loadtxt('stacked_lstm_weights/fc/by.h',
                    delimiter=',',
                    usecols=range(num_classes)).flatten()

    h0, h1, h2, h3 = {}, {}, {}, {}
    h0[-1] = np.zeros(hidden_size)
    h1[-1] = np.zeros(hidden_size)
    h2[-1] = np.zeros(hidden_size)
    h3[-1] = np.zeros(hidden_size)

    c0, c1, c2, c3 = {}, {}, {}, {}
    c0[-1] = np.zeros(hidden_size)
    c1[-1] = np.zeros(hidden_size)
    c2[-1] = np.zeros(hidden_size)
    c3[-1] = np.zeros(hidden_size)

    from models import my_lstm
    test_image = np.loadtxt(path_test_data, delimiter=',')
    for t in range(28):
        (h0[t], c0[t]) = my_lstm(test_image[t], h0[t - 1], c0[t - 1], W_i, W_f,
                                 W_c, W_o, U_i, U_f, U_c, U_o, b_i, b_f, b_c,
                                 b_o)
        (h1[t], c1[t]) = my_lstm(h0[t], h1[t - 1], c1[t - 1], W1_i, W1_f, W1_c,
                                 W1_o, U1_i, U1_f, U1_c, U1_o, b1_i, b1_f,
                                 b1_c, b1_o)
        (h2[t], c2[t]) = my_lstm(h1[t], h2[t - 1], c2[t - 1], W2_i, W2_f, W2_c,
                                 W2_o, U2_i, U2_f, U2_c, U2_o, b2_i, b2_f,
                                 b2_c, b2_o)
        (h3[t], c3[t]) = my_lstm(h2[t], h3[t - 1], c3[t - 1], W3_i, W3_f, W3_c,
                                 W3_o, U3_i, U3_f, U3_c, U3_o, b3_i, b3_f,
                                 b3_c, b3_o)

    yt = np.dot(Why, h3[img_cols - 1]) + by
    print(yt)

    # h_dict = {'h0': h0, 'h1':h1}
    with open(path_written_data, 'w') as file:
        file.write('------h0-------\n\n')
        for k, v in h0.items():
            file.write(str(k) + ' >>> ' + str(v) + '\n\n')
        file.write('------h1-------\n\n')
        for k, v in h1.items():
            file.write(str(k) + ' >>> ' + str(v) + '\n\n')
        file.write('------h2-------\n\n')
        for k, v in h2.items():
            file.write(str(k) + ' >>> ' + str(v) + '\n\n')
        file.write('------h3-------\n\n')
        for k, v in h3.items():
            file.write(str(k) + ' >>> ' + str(v) + '\n\n')

    # prepare headers
    save_lstm_headers('stacked_lstm_weights/cell0', 0)
    save_lstm_headers('stacked_lstm_weights/cell1', 1)
    save_lstm_headers('stacked_lstm_weights/cell2', 2)
    save_lstm_headers('stacked_lstm_weights/cell3', 3)
    save_fc_headers('stacked_lstm_weights')