예제 #1
0
model.add(RepeatVector(DIGITS + 1))
# The decoder RNN could be multiple layers stacked or a single keras_layer
for _ in range(LAYERS):
    model.add(RNN(HIDDEN_SIZE, return_sequences=True))

# For each of step of the output sequence, decide which character should be chosen
model.add(TimeDistributedDense(convertor.get_dim()))
model.add(Activation("softmax"))

model.compile(loss="categorical_crossentropy", optimizer="adam")

# Train the model each generation and show predictions against the validation dataset
for iteration in range(1, 200):
    print()
    print("-" * 50)
    print("Iteration", iteration)
    model.fit(X_train, y_train, batch_size=BATCH_SIZE, nb_epoch=1, validation_data=(X_val, y_val), show_accuracy=True)
    ###
    # Select 10 samples from the validation set at random so we can visualize errors
    for i in range(10):
        ind = np.random.randint(0, len(X_val))
        rowX, rowy = X_val[np.array([ind])], y_val[np.array([ind])]
        preds = model.predict_classes(rowX, verbose=0)
        q = convertor.decode(rowX[0], invert=True)
        correct = convertor.decode(rowy[0])
        guess = convertor.decode(preds[0], calc_argmax=False)
        print("Q", q)
        print("T", correct)
        print(colors.ok + "☑" + colors.close if correct == guess else colors.fail + "☒" + colors.close, guess)
        print("---")
예제 #2
0
              nb_epoch=1,
              show_accuracy=True,
              verbose=2,
              validation_data={
                  'X': D_X_val,
                  'y': D_y_val
              })
    ###
    # Select 10 samples from the validation set at random so we can visualize errors
    for i in range(10):
        ind = np.random.randint(0, len(D_X_val))
        rowX, rowy = D_X_val[np.array([ind])], D_y_val[np.array([ind])]
        preds = model.predict({'X': rowX},
                              class_mode={'y': "categorical"},
                              verbose=0)['y']
        q = convertor.decode(rowX[0], invert=INVERT)
        correct = convertor.decode(rowy[0])
        guess = convertor.decode(preds[0], calc_argmax=False)
        print('Q', q)
        print('T', correct)
        print(
            colors.ok + '☑' +
            colors.close if correct == guess else colors.fail + '☒' +
            colors.close, guess)
        print('---')

import os
usr_home = os.path.expanduser('~')
save_dir = os.path.join(usr_home, ".dlx/saved_model/")
if not os.path.isdir(save_dir):
    os.mkdir(save_dir)
예제 #3
0
              validation_data={
                  'X': D_X_val,
                  'y': D_y_val
              })
    ###
    # Select 10 samples from the validation set at random so we can visualize errors
    for i in range(10):
        ind = np.random.randint(0, len(D_X_val))
        rowX, rowy = D_X_val[np.array([ind])], D_y_val[np.array([ind])]
        preds = model.predict({'X': rowX},
                              class_mode={
                                  'y': "categorical",
                                  'alpha': None
                              },
                              verbose=0)
        q = convertor.decode(rowX[0], invert=True)
        correct = convertor.decode(rowy[0])
        guess = convertor.decode(preds['y'][0], calc_argmax=False)
        alpha = preds['alpha']

        print('Q', q)
        print('T', correct)
        print(
            colors.ok + '☑' +
            colors.close if correct == guess else colors.fail + '☒' +
            colors.close, guess)

        print('Alpha:', alpha[0].shape)
        sys.stdout.write('   A\\Q')
        for j in range(len(q)):
            sys.stdout.write("%6s" % q[j])
    ###
    # Select 10 samples from the validation set at random so we can visualize errors
    for ii in range(10):
        ind = np.random.randint(0, len(D_A_val))
        rowA, rowB, rowy = D_A_val[np.array([ind])], D_B_val[np.array(
            [ind])], D_y_val[np.array([ind])]
        preds = model.predict({
            'A': rowA,
            'B': rowB
        },
                              class_mode={
                                  'y': "categorical",
                                  'alpha': None
                              },
                              verbose=0)
        string_A = convertor.decode(rowA[0], invert=True, index=True)
        string_B = convertor.decode(rowB[0], invert=True, index=True)
        correct = convertor.decode(rowy[0], invert=True)
        guess = convertor.decode(preds['y'][0], calc_argmax=False)[::-1]
        alpha = preds['alpha']

        print('A', string_A)
        print(colors.ok + '+' + colors.close)
        print('B', string_B)
        print(colors.ok + '=' + colors.close)
        print('T', correct)
        print(
            colors.ok + '☑' +
            colors.close if correct == guess else colors.fail + '☒' +
            colors.close, guess)
    history = model.fit(
        data={"A": D_A_train, "B": D_B_train, "y": D_y_train},
        batch_size=BATCH_SIZE,
        nb_epoch=1,
        show_accuracy=True,
        verbose=2,
        validation_data={"A": D_A_val, "B": D_B_val, "y": D_y_val},
    )

    ###
    # Select 10 samples from the validation set at random so we can visualize errors
    for ii in range(10):
        ind = np.random.randint(0, len(D_A_val))
        rowA, rowB, rowy = D_A_val[np.array([ind])], D_B_val[np.array([ind])], D_y_val[np.array([ind])]
        preds = model.predict({"A": rowA, "B": rowB}, class_mode={"y": "categorical", "alpha": None}, verbose=0)
        string_A = convertor.decode(rowA[0], invert=True, index=True)
        string_B = convertor.decode(rowB[0], invert=True, index=True)
        correct = convertor.decode(rowy[0], invert=True)
        guess = convertor.decode(preds["y"][0], calc_argmax=False)[::-1]
        alpha = preds["alpha"]

        print("A", string_A)
        print(colors.ok + "+" + colors.close)
        print("B", string_B)
        print(colors.ok + "=" + colors.close)
        print("T", correct)
        print(colors.ok + "☑" + colors.close if correct == guess else colors.fail + "☒" + colors.close, guess)

        if ii == 9:
            print("Alpha:", alpha[0].shape)
            sys.stdout.write("GUESS    A\\B")