In [384]:
from __future__ import print_function
from keras.models import Sequential
from keras.engine.training import slice_X
from keras.layers import Lambda, Flatten, Permute 
from keras.layers import merge, Merge, Activation, TimeDistributed, Dense, RepeatVector, recurrent
import numpy as np
from six.moves import range
import keras.backend as K
from sklearn.metrics import accuracy_score

In [385]:
class CharacterTable(object):
    '''
    Given a set of characters:
    + Encode them to a one hot integer representation
    + Decode the one hot integer representation to their character output
    + Decode a vector of probabilities to their character output
    '''
    def __init__(self, chars, maxlen):
        self.chars = sorted(set(chars))
        self.char_indices = dict((c, i) for i, c in enumerate(self.chars))
        self.indices_char = dict((i, c) for i, c in enumerate(self.chars))
        self.maxlen = maxlen

    def encode(self, C, maxlen=None):
        maxlen = maxlen if maxlen else self.maxlen
        X = np.zeros((maxlen, len(self.chars)))
        for i, c in enumerate(C):
            X[i, self.char_indices[c]] = 1
        return X

    def decode(self, X, calc_argmax=True):
        if calc_argmax:
            X = X.argmax(axis=-1)
        return ''.join(self.indices_char[x] for x in X)

In [386]:
class colors:
    ok = '\033[92m'
    fail = '\033[91m'
    close = '\033[0m'

In [387]:
# Parameters for the model and dataset
TRAINING_SIZE = 5000
DIGITS = 2
OPS = 2
INVERT = True
# Try replacing GRU, or SimpleRNN
RNN = recurrent.LSTM
HIDDEN_SIZE = 128
BATCH_SIZE = 128
LAYERS = 1
MAXLEN = OPS * DIGITS + OPS - 1

In [388]:
chars = '0123456789+ '
ctable = CharacterTable(chars, MAXLEN)

In [389]:
def generate_data(training_size, num_digits, num_ops):
    MAXLEN = num_ops * num_digits + num_ops - 1
    questions = []
    expected = []
    seen = set()
    print('Generating data... ')
    while len(questions) < training_size:
        f = lambda: int(''.join(np.random.choice(list('0123456789')) for i in range(np.random.randint(1, num_digits + 1))))

        ops = []
        for i in range(num_ops):
            ops.append(f())
        # Skip any addition questions we've already seen
        # Also skip any such that X+Y == Y+X (hence the sorting)
        ops.sort()
        key = tuple(ops)
        if key in seen:
            continue
        seen.add(key)
        # Pad the data with spaces such that it is always MAXLEN
        q = '+'.join([str(op) for op in ops])
        query = q + ' ' * (MAXLEN - len(q))
        ans = str(sum(ops))
        # Answers can be of maximum size DIGITS + 1
        ans += ' ' * (num_digits + 1 - len(ans))
        if INVERT:
            query = query[::-1]
            ans = ans[::-1]
        questions.append(query)
        expected.append(ans)
    print('Total addition questions:', len(questions))
    
    return questions, expected

In [390]:
def create_train_valid(questions, expected, num_digits, num_ops):
    MAXLEN = num_ops * num_digits + num_ops - 1
    print('Vectorization...')
    X = np.zeros((len(questions), MAXLEN, len(chars)), dtype=np.bool)
    y = np.zeros((len(questions), num_digits + 1, len(chars)), dtype=np.bool)
    for i, sentence in enumerate(questions):
        X[i] = ctable.encode(sentence, maxlen=MAXLEN)
    for i, sentence in enumerate(expected):
        y[i] = ctable.encode(sentence, maxlen=num_digits + 1)

    # Shuffle (X, y) in unison as the later parts of X will almost all be larger digits
    indices = np.arange(len(y))
    np.random.shuffle(indices)
    X = X[indices]
    y = y[indices]

    # Explicitly set apart 10% for validation data that we never train over
    split_at = len(X) - len(X) / 10
    (X_train, X_val) = (slice_X(X, 0, split_at), slice_X(X, split_at))
    (y_train, y_val) = (y[:split_at], y[split_at:])

    print(X_train.shape)
    print(y_train.shape)
    
    return X_train, y_train, X_val, y_val

In [8]:
def build_model(hidden_size, num_layers, num_digits, num_ops):
    MAXLEN = num_ops * num_digits + num_ops - 1
    # Most simple seq2seq model using encoder-decoder framework
    print('Build model...')
    model = Sequential()
    # "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE
    # note: in a situation where your input sequences have a variable length,
    # use input_shape=(None, nb_feature).
    encoder = RNN(hidden_size, input_shape=(MAXLEN, len(chars)))
    model.add(encoder)
    # For the decoder's input, we repeat the encoded input for each time step
    model.add(RepeatVector(num_digits + 1))
    # The decoder RNN could be multiple layers stacked or a single layer
    for _ in range(num_layers):
        decoder = RNN(hidden_size, return_sequences=True)
        model.add(decoder)

    # For each of step of the output sequence, decide which character should be chosen
    mapper = TimeDistributed(Dense(len(chars)))
    model.add(mapper)
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    
    inputs = [K.learning_phase()] + model.inputs
    encoder_f = K.function(inputs, [encoder.output])
    decoder_f = K.function(inputs, [decoder.output])
    mapper_f = K.function(inputs, [mapper.output])
    
    return model, encoder_f, decoder_f, mapper_f, encoder, decoder, mapper

In [391]:
import theano.tensor as T

def get_Y(X, xmaxlen):
    return X[:, :xmaxlen, :]  # get first xmaxlen elem from time dim

def get_R(X):
    Y, alpha = X[0], X[1]
    ans = K.T.batched_dot(Y, alpha)
    return ans

def stack_R(X):
    r_1, r_2, r_3 = X[0], X[1], X[2]
    ans = K.concatenate([r_1, r_2, r_3], axis=1)
    return ans

def build_att_seq2seq_model(hidden_size, num_layers, num_digits, num_ops, chars):
    SEQ_LEN = num_ops * num_digits + num_ops - 1
    
    main_input = Input(shape=(SEQ_LEN,len(chars)), name='main_input')
    encoder = RNN(hidden_size, 
                  input_shape=(SEQ_LEN, len(chars)),
                  return_sequences=True)(main_input)
    Y = Lambda(get_Y, arguments={"xmaxlen": SEQ_LEN}, name="Y", output_shape=(SEQ_LEN, hidden_size))(encoder)
    WY1 = TimeDistributed(Dense(hidden_size), name="WY1")(Y)
#     WY2 = TimeDistributed(Dense(hidden_size), name="WY2")(Y)
#     WY3 = TimeDistributed(Dense(hidden_size), name="WY3")(Y)
    M1 = Activation('tanh', name="M1")(WY1)
#     M2 = Activation('tanh', name="M2")(WY2)
#     M3 = Activation('tanh', name="M3")(WY3)

    alpha_1 = TimeDistributed(Dense(1, activation='linear'), name="alpha_1")(M1)
    alpha_2 = TimeDistributed(Dense(1, activation='linear'), name="alpha_2")(M1)
    alpha_3 = TimeDistributed(Dense(1, activation='linear'), name="alpha_3")(M1)
    flat_alpha1 = Flatten(name="flat_alpha1")(alpha_1)
    flat_alpha2 = Flatten(name="flat_alpha2")(alpha_2)
    flat_alpha3 = Flatten(name="flat_alpha3")(alpha_3)
    alpha1 = Dense(SEQ_LEN, activation='softmax', name="alpha1")(flat_alpha1)
    alpha2 = Dense(SEQ_LEN, activation='softmax', name="alpha2")(flat_alpha2)
    alpha3 = Dense(SEQ_LEN, activation='softmax', name="alpha3")(flat_alpha3)

    Y_trans = Permute((2, 1), name="y_trans")(Y)  # of shape (None,300,20)

    r_1 = merge([Y_trans, alpha1], output_shape=(hidden_size, 1), name="r_1", mode=get_R)
    r_2 = merge([Y_trans, alpha2], output_shape=(hidden_size, 1), name="r_2", mode=get_R)
    r_3 = merge([Y_trans, alpha3], output_shape=(hidden_size, 1), name="r_3", mode=get_R)
    
    r1 = Reshape((1,hidden_size))(r_1)
    r2 = Reshape((1,hidden_size))(r_2)
    r3 = Reshape((1,hidden_size))(r_3)
    
#     r_1_trans = Permute((2, 1))(r1)
#     r_2_trans = Permute((2, 1))(r2)
#     r_3_trans = Permute((2, 1))(r3)

#     r = RepeatVector(num_digits+1)(r_1)
#     r = T.stack([r_1, r_2, r_3])
    r = merge([r1, r2, r3], mode=stack_R, output_shape=(3, hidden_size))
#     decoder_input = Permute((2, 1))(r)
    decoded_result = RNN(hidden_size, input_shape=(num_digits+1, hidden_size), return_sequences=True)(r)
    mapping = TimeDistributed(Dense(len(chars)))(decoded_result)
    out = Activation('softmax')(mapping)
    
    output = out
    model = Model(input=[main_input], output=output)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model

In [261]:
from keras.layers import Reshape

def build_seq_model(hidden_size, num_layers, num_digits, num_ops):
    MAXLEN = num_ops * num_digits + num_ops - 1
    # Most simple seq2seq model using encoder-decoder framework
    print('Build model...')
    model = Sequential()
    # "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE
    # note: in a situation where your input sequences have a variable length,
    # use input_shape=(None, nb_feature).
    encoder = RNN(hidden_size*(num_digits+1), input_shape=(MAXLEN, len(chars)))
    model.add(encoder)
    # For the decoder's input, we repeat the encoded input for each time step
    model.add(Reshape((num_digits+1, hidden_size)))
    # The decoder RNN could be multiple layers stacked or a single layer
    for _ in range(num_layers):
        decoder = RNN(hidden_size, return_sequences=True)
        model.add(decoder)

    # For each of step of the output sequence, decide which character should be chosen
    mapper = TimeDistributed(Dense(len(chars)))
    model.add(mapper)
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    
    inputs = [K.learning_phase()] + model.inputs
    encoder_f = K.function(inputs, [encoder.output])
    decoder_f = K.function(inputs, [decoder.output])
    mapper_f = K.function(inputs, [mapper.output])
    
    return model, encoder_f, decoder_f, mapper_f, encoder, decoder, mapper

In [10]:
from keras.layers import Input, merge, TimeDistributedDense
from keras.models import Model

def feedback_model(hidden_size, num_layers, num_digits, num_ops):
    MAXLEN = num_ops * num_digits + num_ops - 1
    ## Alternative model architecture
    encoder_input = Input(shape=(MAXLEN, len(chars)), name = 'encoder_input')
    decoder_input = Input(shape=(num_digits + 1, len(chars)), name = 'decoder_input')

    x = RNN(hidden_size)(encoder_input)
    context_input = RepeatVector(num_digits + 1)(x)
    x = merge([context_input, decoder_input], mode='concat')
    x = RNN(hidden_size, return_sequences=True)(x)

#     loss = TimeDistributed(Dense(len(chars), activation='softmax'))(x)
    loss = TimeDistributedDense(len(chars), activation='softmax')(x)
    
    model_alt = Model(input=[encoder_input, decoder_input], output=[loss])
    model_alt.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

    return model_alt

In [392]:
def learning(model, X_train, y_train, iterations, X_val, y_val):
    y_true = []
    for idx in range(y_val.shape[0]):
        y_true.append(ctable.decode(y_val[idx]))

    val_acc_array = []
    # Train the model each generation and show predictions against the validation dataset
    for iteration in range(1, iterations):
        print()
        print('-' * 50)
        print('Iteration', iteration)
        training_obj = model.fit(X_train, y_train, batch_size=BATCH_SIZE, nb_epoch=1,
                  validation_data=(X_val, 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(X_val))
    #         rowX, rowy = X_val[np.array([ind])], y_val[np.array([ind])]
    #         preds = model.predict_classes(rowX, verbose=0)
    #         q = ctable.decode(rowX[0])
    #         correct = ctable.decode(rowy[0])
    #         guess = ctable.decode(preds[0], calc_argmax=False)
    #         print('Q', q[::-1] if INVERT else q)
    #         print('T', correct)
    #         print(colors.ok + '☑' + colors.close if correct == guess else colors.fail + '☒' + colors.close, guess)
    #         print('---')

#         preds = model.predict_classes(X_val, verbose=0)
#         y_preds = []
#         for idx in range(preds.shape[0]):
#             y_preds.append(ctable.decode(preds[idx], calc_argmax=False))

#         acc = accuracy_score(y_true, y_preds)
        acc = training_obj.history['val_acc']
        val_acc_array.append(acc)
        print('Current validation accuracy = ' + str(acc))
    
    return val_acc_array

In [393]:
DIGITS = 2
OPS = 2
TRAINING_SIZE = 5000
questions, expected = generate_data(TRAINING_SIZE, DIGITS, OPS)
X_train, y_train, X_val, y_val = create_train_valid(questions, expected, DIGITS, OPS)
# model, encoder_f_, decoder_f_, mapper_f_, encoder, decoder, mapper = build_att_seq2seq_model(HIDDEN_SIZE, LAYERS, DIGITS, OPS, chars)
# val_acc_2_2 = learning(model, X_train, y_train, 50, X_val, y_val)
model = build_att_seq2seq_model(HIDDEN_SIZE, LAYERS, DIGITS, OPS, chars)
val_acc_2_2 = learning(model, X_train, y_train, 50, X_val, y_val)

# y_train_pre = np.zeros(y_train.shape)
# for idx in range(len(y_train_pre)):
#     y_train_pre[idx][1:] = y_train[idx][:-1]
# y_val_pre = np.zeros(y_val.shape)
# for idx in range(len(y_val_pre)):
#     y_val_pre[idx][1:] = y_val[idx][:-1]
    
# fb_model = feedback_model(HIDDEN_SIZE, LAYERS, DIGITS, OPS)
# fb_val_acc_2_2 = learning(fb_model, [X_train, y_train_pre], y_train, 50, [X_val, y_val_pre], y_val)


Generating data... 
Total addition questions: 5000
Vectorization...
(4500, 5, 12)
(4500, 3, 12)

--------------------------------------------------
Iteration 1
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 2.3033 - acc: 0.2388 - val_loss: 2.1453 - val_acc: 0.3120
Current validation accuracy = [0.312]

--------------------------------------------------
Iteration 2
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.9556 - acc: 0.3758 - val_loss: 1.7939 - val_acc: 0.3853
Current validation accuracy = [0.38533333333333336]

--------------------------------------------------
Iteration 3
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7723 - acc: 0.3912 - val_loss: 1.7279 - val_acc: 0.3847
Current validation accuracy = [0.38466666666666671]

--------------------------------------------------
Iteration 4
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7054 - acc: 0.3983 - val_loss: 1.6274 - val_acc: 0.3940
Current validation accuracy = [0.39400000000000002]

--------------------------------------------------
Iteration 5
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.6130 - acc: 0.4101 - val_loss: 1.5544 - val_acc: 0.4293
Current validation accuracy = [0.42933333333333334]

--------------------------------------------------
Iteration 6
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.5584 - acc: 0.4217 - val_loss: 1.5323 - val_acc: 0.4200
Current validation accuracy = [0.41999999999999998]

--------------------------------------------------
Iteration 7
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.5138 - acc: 0.4396 - val_loss: 1.4737 - val_acc: 0.4320
Current validation accuracy = [0.432]

--------------------------------------------------
Iteration 8
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.4686 - acc: 0.4605 - val_loss: 1.4384 - val_acc: 0.4633
Current validation accuracy = [0.46333333333333332]

--------------------------------------------------
Iteration 9
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.4302 - acc: 0.4743 - val_loss: 1.4049 - val_acc: 0.4793
Current validation accuracy = [0.47933333333333339]

--------------------------------------------------
Iteration 10
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.3978 - acc: 0.4901 - val_loss: 1.3876 - val_acc: 0.4653
Current validation accuracy = [0.46533333333333332]

--------------------------------------------------
Iteration 11
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.3648 - acc: 0.5064 - val_loss: 1.3425 - val_acc: 0.4960
Current validation accuracy = [0.496]

--------------------------------------------------
Iteration 12
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 7s - loss: 1.3147 - acc: 0.5262 - val_loss: 1.2715 - val_acc: 0.5373
Current validation accuracy = [0.53733333333333333]

--------------------------------------------------
Iteration 13
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 8s - loss: 1.2653 - acc: 0.5515 - val_loss: 1.2309 - val_acc: 0.5620
Current validation accuracy = [0.56200000000000006]

--------------------------------------------------
Iteration 14
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 10s - loss: 1.2259 - acc: 0.5716 - val_loss: 1.2263 - val_acc: 0.5380
Current validation accuracy = [0.53800000000000003]

--------------------------------------------------
Iteration 15
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 7s - loss: 1.2007 - acc: 0.5746 - val_loss: 1.1700 - val_acc: 0.5927
Current validation accuracy = [0.59266666666666667]

--------------------------------------------------
Iteration 16
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.1574 - acc: 0.6122 - val_loss: 1.1470 - val_acc: 0.5767
Current validation accuracy = [0.57666666666666666]

--------------------------------------------------
Iteration 17
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.1348 - acc: 0.6108 - val_loss: 1.1328 - val_acc: 0.5787
Current validation accuracy = [0.57866666666666666]

--------------------------------------------------
Iteration 18
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.1143 - acc: 0.6116 - val_loss: 1.0950 - val_acc: 0.6113
Current validation accuracy = [0.6113333333333334]

--------------------------------------------------
Iteration 19
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.0767 - acc: 0.6350 - val_loss: 1.0684 - val_acc: 0.6173
Current validation accuracy = [0.6173333333333334]

--------------------------------------------------
Iteration 20
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.0391 - acc: 0.6567 - val_loss: 1.0244 - val_acc: 0.6413
Current validation accuracy = [0.64133333333333342]

--------------------------------------------------
Iteration 21
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.0019 - acc: 0.6696 - val_loss: 0.9985 - val_acc: 0.6467
Current validation accuracy = [0.64666666666666661]

--------------------------------------------------
Iteration 22
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 7s - loss: 0.9742 - acc: 0.6724 - val_loss: 0.9706 - val_acc: 0.6673
Current validation accuracy = [0.66733333333333322]

--------------------------------------------------
Iteration 23
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 7s - loss: 0.9399 - acc: 0.6867 - val_loss: 0.9299 - val_acc: 0.6627
Current validation accuracy = [0.66266666666666663]

--------------------------------------------------
Iteration 24
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 0.9099 - acc: 0.6921 - val_loss: 0.9099 - val_acc: 0.6727
Current validation accuracy = [0.67266666666666675]

--------------------------------------------------
Iteration 25
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 7s - loss: 0.8740 - acc: 0.6998 - val_loss: 0.8674 - val_acc: 0.6907
Current validation accuracy = [0.69066666666666676]

--------------------------------------------------
Iteration 26
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 0.8446 - acc: 0.7109 - val_loss: 0.8159 - val_acc: 0.7127
Current validation accuracy = [0.71266666666666678]

--------------------------------------------------
Iteration 27
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.7977 - acc: 0.7364 - val_loss: 0.7931 - val_acc: 0.7273
Current validation accuracy = [0.72733333333333339]

--------------------------------------------------
Iteration 28
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.7691 - acc: 0.7471 - val_loss: 0.7631 - val_acc: 0.7373
Current validation accuracy = [0.73733333333333329]

--------------------------------------------------
Iteration 29
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 7s - loss: 0.7368 - acc: 0.7558 - val_loss: 0.7279 - val_acc: 0.7480
Current validation accuracy = [0.748]

--------------------------------------------------
Iteration 30
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 0.7060 - acc: 0.7721 - val_loss: 0.7061 - val_acc: 0.7760
Current validation accuracy = [0.77600000000000002]

--------------------------------------------------
Iteration 31
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 0.6729 - acc: 0.7904 - val_loss: 0.6643 - val_acc: 0.7913
Current validation accuracy = [0.79133333333333333]

--------------------------------------------------
Iteration 32
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 0.6405 - acc: 0.8004 - val_loss: 0.6496 - val_acc: 0.8000
Current validation accuracy = [0.80000000000000004]

--------------------------------------------------
Iteration 33
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 0.6181 - acc: 0.8156 - val_loss: 0.6248 - val_acc: 0.7940
Current validation accuracy = [0.79400000000000004]

--------------------------------------------------
Iteration 34
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 0.5948 - acc: 0.8199 - val_loss: 0.6028 - val_acc: 0.8300
Current validation accuracy = [0.82999999999999996]

--------------------------------------------------
Iteration 35
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.5611 - acc: 0.8452 - val_loss: 0.5738 - val_acc: 0.8267
Current validation accuracy = [0.82666666666666655]

--------------------------------------------------
Iteration 36
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.5495 - acc: 0.8428 - val_loss: 0.5475 - val_acc: 0.8573
Current validation accuracy = [0.85733333333333339]

--------------------------------------------------
Iteration 37
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.5186 - acc: 0.8594 - val_loss: 0.5448 - val_acc: 0.8533
Current validation accuracy = [0.8533333333333335]

--------------------------------------------------
Iteration 38
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.5003 - acc: 0.8697 - val_loss: 0.5049 - val_acc: 0.8793
Current validation accuracy = [0.87933333333333341]

--------------------------------------------------
Iteration 39
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.4810 - acc: 0.8776 - val_loss: 0.4947 - val_acc: 0.8680
Current validation accuracy = [0.86799999999999999]

--------------------------------------------------
Iteration 40
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.4661 - acc: 0.8802 - val_loss: 0.4765 - val_acc: 0.8820
Current validation accuracy = [0.88200000000000001]

--------------------------------------------------
Iteration 41
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.4525 - acc: 0.8841 - val_loss: 0.5094 - val_acc: 0.8527
Current validation accuracy = [0.85266666666666668]

--------------------------------------------------
Iteration 42
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.4413 - acc: 0.8867 - val_loss: 0.4543 - val_acc: 0.8860
Current validation accuracy = [0.88600000000000001]

--------------------------------------------------
Iteration 43
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.4150 - acc: 0.8983 - val_loss: 0.4321 - val_acc: 0.8940
Current validation accuracy = [0.89400000000000002]

--------------------------------------------------
Iteration 44
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.4014 - acc: 0.9008 - val_loss: 0.4337 - val_acc: 0.8847
Current validation accuracy = [0.8846666666666666]

--------------------------------------------------
Iteration 45
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.3905 - acc: 0.9047 - val_loss: 0.4155 - val_acc: 0.9040
Current validation accuracy = [0.90400000000000003]

--------------------------------------------------
Iteration 46
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.3734 - acc: 0.9085 - val_loss: 0.3946 - val_acc: 0.9053
Current validation accuracy = [0.90533333333333332]

--------------------------------------------------
Iteration 47
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.3570 - acc: 0.9134 - val_loss: 0.3775 - val_acc: 0.9080
Current validation accuracy = [0.90800000000000003]

--------------------------------------------------
Iteration 48
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 0.3466 - acc: 0.9176 - val_loss: 0.3810 - val_acc: 0.8953
Current validation accuracy = [0.89533333333333354]

--------------------------------------------------
Iteration 49
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 0.3358 - acc: 0.9199 - val_loss: 0.3618 - val_acc: 0.9040
Current validation accuracy = [0.90400000000000003]

In [300]:
len(model.layers[3].get_weights()[1])


Out[300]:
128

In [11]:
DIGITS = 3
OPS = 3
TRAINING_SIZE = 50000
questions, expected = generate_data(TRAINING_SIZE, DIGITS, OPS)
X_train, y_train, X_val, y_val = create_train_valid(questions, expected, DIGITS, OPS)
model = build_model(HIDDEN_SIZE, LAYERS, DIGITS, OPS)
val_acc_3_3 = learning(model, X_train, y_train, 50, X_val, y_val)

y_train_pre = np.zeros(y_train.shape)
for idx in range(len(y_train_pre)):
    y_train_pre[idx][1:] = y_train[idx][:-1]
y_val_pre = np.zeros(y_val.shape)
for idx in range(len(y_val_pre)):
    y_val_pre[idx][1:] = y_val[idx][:-1]
    
fb_model = feedback_model(HIDDEN_SIZE, LAYERS, DIGITS, OPS)
fb_val_acc_3_3 = learning(fb_model, [X_train, y_train_pre], y_train, 50, [X_val, y_val_pre], y_val)


Generating data... 
Total addition questions: 50000
Vectorization...
(45000, 11, 12)
(45000, 4, 12)
Build model...

--------------------------------------------------
Iteration 1
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 39s - loss: 1.8136 - acc: 0.3585 - val_loss: 1.6993 - val_acc: 0.3807
Current validation accuracy = [0.38069999999999998]

--------------------------------------------------
Iteration 2
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 1.5668 - acc: 0.4196 - val_loss: 1.4374 - val_acc: 0.4574
Current validation accuracy = [0.45745000000000002]

--------------------------------------------------
Iteration 3
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 1.3562 - acc: 0.4888 - val_loss: 1.2984 - val_acc: 0.5108
Current validation accuracy = [0.51075000000000004]

--------------------------------------------------
Iteration 4
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 39s - loss: 1.2600 - acc: 0.5225 - val_loss: 1.2244 - val_acc: 0.5350
Current validation accuracy = [0.53495000000000004]

--------------------------------------------------
Iteration 5
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 1.1883 - acc: 0.5480 - val_loss: 1.1680 - val_acc: 0.5526
Current validation accuracy = [0.55259999999999998]

--------------------------------------------------
Iteration 6
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 1.1375 - acc: 0.5684 - val_loss: 1.1199 - val_acc: 0.5722
Current validation accuracy = [0.57220000000000004]

--------------------------------------------------
Iteration 7
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 39s - loss: 1.0935 - acc: 0.5847 - val_loss: 1.0921 - val_acc: 0.5855
Current validation accuracy = [0.58545000000000003]

--------------------------------------------------
Iteration 8
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 1.0505 - acc: 0.6009 - val_loss: 1.0378 - val_acc: 0.6086
Current validation accuracy = [0.60860000000000003]

--------------------------------------------------
Iteration 9
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 1.0103 - acc: 0.6168 - val_loss: 1.0080 - val_acc: 0.6122
Current validation accuracy = [0.61219999999999997]

--------------------------------------------------
Iteration 10
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 39s - loss: 0.9795 - acc: 0.6284 - val_loss: 0.9703 - val_acc: 0.6262
Current validation accuracy = [0.62619999999999998]

--------------------------------------------------
Iteration 11
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.9460 - acc: 0.6415 - val_loss: 0.9495 - val_acc: 0.6407
Current validation accuracy = [0.64070000000000005]

--------------------------------------------------
Iteration 12
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.9218 - acc: 0.6506 - val_loss: 0.9201 - val_acc: 0.6519
Current validation accuracy = [0.65185000000000004]

--------------------------------------------------
Iteration 13
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 39s - loss: 0.8969 - acc: 0.6602 - val_loss: 0.9054 - val_acc: 0.6532
Current validation accuracy = [0.6532]

--------------------------------------------------
Iteration 14
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.8733 - acc: 0.6687 - val_loss: 0.8987 - val_acc: 0.6516
Current validation accuracy = [0.65164999999999995]

--------------------------------------------------
Iteration 15
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.8489 - acc: 0.6795 - val_loss: 0.8587 - val_acc: 0.6714
Current validation accuracy = [0.67135]

--------------------------------------------------
Iteration 16
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 39s - loss: 0.8327 - acc: 0.6841 - val_loss: 0.8446 - val_acc: 0.6747
Current validation accuracy = [0.67474999999999996]

--------------------------------------------------
Iteration 17
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.8103 - acc: 0.6935 - val_loss: 0.8460 - val_acc: 0.6684
Current validation accuracy = [0.66839999999999999]

--------------------------------------------------
Iteration 18
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.7914 - acc: 0.7003 - val_loss: 0.8073 - val_acc: 0.6871
Current validation accuracy = [0.68710000000000004]

--------------------------------------------------
Iteration 19
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 43s - loss: 0.7751 - acc: 0.7057 - val_loss: 0.7897 - val_acc: 0.6976
Current validation accuracy = [0.69764999999999999]

--------------------------------------------------
Iteration 20
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.7536 - acc: 0.7149 - val_loss: 0.7832 - val_acc: 0.6968
Current validation accuracy = [0.69684999999999997]

--------------------------------------------------
Iteration 21
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.7386 - acc: 0.7211 - val_loss: 0.7735 - val_acc: 0.7023
Current validation accuracy = [0.70230000000000004]

--------------------------------------------------
Iteration 22
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.7257 - acc: 0.7249 - val_loss: 0.7463 - val_acc: 0.7132
Current validation accuracy = [0.71319999999999995]

--------------------------------------------------
Iteration 23
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.7063 - acc: 0.7336 - val_loss: 0.7552 - val_acc: 0.7087
Current validation accuracy = [0.7087]

--------------------------------------------------
Iteration 24
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.6899 - acc: 0.7400 - val_loss: 0.7275 - val_acc: 0.7163
Current validation accuracy = [0.71625000000000005]

--------------------------------------------------
Iteration 25
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.6759 - acc: 0.7459 - val_loss: 0.6959 - val_acc: 0.7334
Current validation accuracy = [0.73340000000000005]

--------------------------------------------------
Iteration 26
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 39s - loss: 0.6562 - acc: 0.7541 - val_loss: 0.6892 - val_acc: 0.7326
Current validation accuracy = [0.73255000000000003]

--------------------------------------------------
Iteration 27
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.6428 - acc: 0.7587 - val_loss: 0.6793 - val_acc: 0.7379
Current validation accuracy = [0.7379]

--------------------------------------------------
Iteration 28
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.6269 - acc: 0.7644 - val_loss: 0.6440 - val_acc: 0.7532
Current validation accuracy = [0.75319999999999998]

--------------------------------------------------
Iteration 29
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.6086 - acc: 0.7734 - val_loss: 0.6706 - val_acc: 0.7379
Current validation accuracy = [0.73785000000000001]

--------------------------------------------------
Iteration 30
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.6008 - acc: 0.7752 - val_loss: 0.6293 - val_acc: 0.7575
Current validation accuracy = [0.75749999999999995]

--------------------------------------------------
Iteration 31
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.5858 - acc: 0.7806 - val_loss: 0.6230 - val_acc: 0.7610
Current validation accuracy = [0.76100000000000001]

--------------------------------------------------
Iteration 32
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.5736 - acc: 0.7861 - val_loss: 0.6184 - val_acc: 0.7634
Current validation accuracy = [0.76339999999999997]

--------------------------------------------------
Iteration 33
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.5568 - acc: 0.7937 - val_loss: 0.5892 - val_acc: 0.7737
Current validation accuracy = [0.77370000000000005]

--------------------------------------------------
Iteration 34
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.5520 - acc: 0.7943 - val_loss: 0.6027 - val_acc: 0.7698
Current validation accuracy = [0.76975000000000005]

--------------------------------------------------
Iteration 35
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.5350 - acc: 0.8027 - val_loss: 0.5934 - val_acc: 0.7721
Current validation accuracy = [0.77210000000000001]

--------------------------------------------------
Iteration 36
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.5257 - acc: 0.8067 - val_loss: 0.5725 - val_acc: 0.7810
Current validation accuracy = [0.78095000000000003]

--------------------------------------------------
Iteration 37
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.5166 - acc: 0.8090 - val_loss: 0.5689 - val_acc: 0.7788
Current validation accuracy = [0.77880000000000005]

--------------------------------------------------
Iteration 38
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.5113 - acc: 0.8103 - val_loss: 0.5573 - val_acc: 0.7914
Current validation accuracy = [0.79144999999999999]

--------------------------------------------------
Iteration 39
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4980 - acc: 0.8164 - val_loss: 0.5549 - val_acc: 0.7893
Current validation accuracy = [0.78925000000000001]

--------------------------------------------------
Iteration 40
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4933 - acc: 0.8183 - val_loss: 0.5476 - val_acc: 0.7935
Current validation accuracy = [0.79349999999999998]

--------------------------------------------------
Iteration 41
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4776 - acc: 0.8260 - val_loss: 0.5414 - val_acc: 0.7933
Current validation accuracy = [0.79325000000000001]

--------------------------------------------------
Iteration 42
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.4761 - acc: 0.8252 - val_loss: 0.5271 - val_acc: 0.8006
Current validation accuracy = [0.80064999999999997]

--------------------------------------------------
Iteration 43
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4692 - acc: 0.8283 - val_loss: 0.5401 - val_acc: 0.7966
Current validation accuracy = [0.79664999999999997]

--------------------------------------------------
Iteration 44
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4632 - acc: 0.8293 - val_loss: 0.5250 - val_acc: 0.8020
Current validation accuracy = [0.80195000000000005]

--------------------------------------------------
Iteration 45
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4521 - acc: 0.8353 - val_loss: 0.5069 - val_acc: 0.8102
Current validation accuracy = [0.81020000000000003]

--------------------------------------------------
Iteration 46
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4451 - acc: 0.8377 - val_loss: 0.5033 - val_acc: 0.8118
Current validation accuracy = [0.81184999999999996]

--------------------------------------------------
Iteration 47
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4427 - acc: 0.8371 - val_loss: 0.5059 - val_acc: 0.8112
Current validation accuracy = [0.81115000000000004]

--------------------------------------------------
Iteration 48
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4324 - acc: 0.8421 - val_loss: 0.5008 - val_acc: 0.8102
Current validation accuracy = [0.81015000000000004]

--------------------------------------------------
Iteration 49
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.4276 - acc: 0.8441 - val_loss: 0.4918 - val_acc: 0.8162
Current validation accuracy = [0.81620000000000004]
/home/CNSHYQH/anaconda2/lib/python2.7/site-packages/Keras-1.0.3-py2.7.egg/keras/layers/core.py:1015: UserWarning: TimeDistributedDense is deprecated, please use TimeDistributed(Dense(...)) instead.
  warnings.warn('TimeDistributedDense is deprecated, '
--------------------------------------------------
Iteration 1
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 1.7630 - acc: 0.3662 - val_loss: 1.6598 - val_acc: 0.3878
Current validation accuracy = [0.38784999999999997]

--------------------------------------------------
Iteration 2
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 43s - loss: 1.5903 - acc: 0.4066 - val_loss: 1.4825 - val_acc: 0.4408
Current validation accuracy = [0.44080000000000003]

--------------------------------------------------
Iteration 3
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 1.4016 - acc: 0.4626 - val_loss: 1.3256 - val_acc: 0.4864
Current validation accuracy = [0.48644999999999999]

--------------------------------------------------
Iteration 4
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 1.2804 - acc: 0.5060 - val_loss: 1.2344 - val_acc: 0.5220
Current validation accuracy = [0.52200000000000002]

--------------------------------------------------
Iteration 5
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 1.1913 - acc: 0.5424 - val_loss: 1.1569 - val_acc: 0.5553
Current validation accuracy = [0.55525000000000002]

--------------------------------------------------
Iteration 6
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 43s - loss: 1.1209 - acc: 0.5720 - val_loss: 1.1009 - val_acc: 0.5764
Current validation accuracy = [0.57640000000000002]

--------------------------------------------------
Iteration 7
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 1.0668 - acc: 0.5923 - val_loss: 1.0511 - val_acc: 0.5981
Current validation accuracy = [0.59809999999999997]

--------------------------------------------------
Iteration 8
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 1.0121 - acc: 0.6147 - val_loss: 0.9941 - val_acc: 0.6204
Current validation accuracy = [0.62039999999999995]

--------------------------------------------------
Iteration 9
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.9618 - acc: 0.6332 - val_loss: 0.9482 - val_acc: 0.6359
Current validation accuracy = [0.63590000000000002]

--------------------------------------------------
Iteration 10
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.9186 - acc: 0.6485 - val_loss: 0.9204 - val_acc: 0.6379
Current validation accuracy = [0.63790000000000002]

--------------------------------------------------
Iteration 11
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.8764 - acc: 0.6644 - val_loss: 0.8893 - val_acc: 0.6517
Current validation accuracy = [0.65169999999999995]

--------------------------------------------------
Iteration 12
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.8360 - acc: 0.6799 - val_loss: 0.8365 - val_acc: 0.6763
Current validation accuracy = [0.67625000000000002]

--------------------------------------------------
Iteration 13
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.8053 - acc: 0.6915 - val_loss: 0.8169 - val_acc: 0.6817
Current validation accuracy = [0.68169999999999997]

--------------------------------------------------
Iteration 14
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.7792 - acc: 0.6999 - val_loss: 0.7885 - val_acc: 0.6936
Current validation accuracy = [0.69364999999999999]

--------------------------------------------------
Iteration 15
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.7496 - acc: 0.7110 - val_loss: 0.7694 - val_acc: 0.6936
Current validation accuracy = [0.69364999999999999]

--------------------------------------------------
Iteration 16
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.7280 - acc: 0.7185 - val_loss: 0.7433 - val_acc: 0.7078
Current validation accuracy = [0.70784999999999998]

--------------------------------------------------
Iteration 17
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.7018 - acc: 0.7288 - val_loss: 0.7121 - val_acc: 0.7217
Current validation accuracy = [0.72170000000000001]

--------------------------------------------------
Iteration 18
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.6823 - acc: 0.7362 - val_loss: 0.7087 - val_acc: 0.7188
Current validation accuracy = [0.71884999999999999]

--------------------------------------------------
Iteration 19
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.6643 - acc: 0.7419 - val_loss: 0.6881 - val_acc: 0.7275
Current validation accuracy = [0.72745000000000004]

--------------------------------------------------
Iteration 20
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.6404 - acc: 0.7522 - val_loss: 0.6692 - val_acc: 0.7312
Current validation accuracy = [0.73119999999999996]

--------------------------------------------------
Iteration 21
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.6284 - acc: 0.7557 - val_loss: 0.6456 - val_acc: 0.7446
Current validation accuracy = [0.74460000000000004]

--------------------------------------------------
Iteration 22
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 43s - loss: 0.6080 - acc: 0.7654 - val_loss: 0.6469 - val_acc: 0.7365
Current validation accuracy = [0.73650000000000004]

--------------------------------------------------
Iteration 23
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.6037 - acc: 0.7627 - val_loss: 0.6218 - val_acc: 0.7543
Current validation accuracy = [0.75429999999999997]

--------------------------------------------------
Iteration 24
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.5787 - acc: 0.7760 - val_loss: 0.6460 - val_acc: 0.7379
Current validation accuracy = [0.73794999999999999]

--------------------------------------------------
Iteration 25
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.5645 - acc: 0.7812 - val_loss: 0.6045 - val_acc: 0.7590
Current validation accuracy = [0.75895000000000001]

--------------------------------------------------
Iteration 26
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.5528 - acc: 0.7851 - val_loss: 0.5821 - val_acc: 0.7675
Current validation accuracy = [0.76749999999999996]

--------------------------------------------------
Iteration 27
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.5439 - acc: 0.7875 - val_loss: 0.5878 - val_acc: 0.7644
Current validation accuracy = [0.76439999999999997]

--------------------------------------------------
Iteration 28
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 40s - loss: 0.5321 - acc: 0.7916 - val_loss: 0.5718 - val_acc: 0.7690
Current validation accuracy = [0.76895000000000002]

--------------------------------------------------
Iteration 29
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 43s - loss: 0.5217 - acc: 0.7968 - val_loss: 0.5707 - val_acc: 0.7680
Current validation accuracy = [0.76800000000000002]

--------------------------------------------------
Iteration 30
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.5070 - acc: 0.8028 - val_loss: 0.5539 - val_acc: 0.7755
Current validation accuracy = [0.77549999999999997]

--------------------------------------------------
Iteration 31
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4988 - acc: 0.8057 - val_loss: 0.5483 - val_acc: 0.7782
Current validation accuracy = [0.77825]

--------------------------------------------------
Iteration 32
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4912 - acc: 0.8076 - val_loss: 0.5205 - val_acc: 0.7923
Current validation accuracy = [0.79225000000000001]

--------------------------------------------------
Iteration 33
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 44s - loss: 0.4831 - acc: 0.8106 - val_loss: 0.5174 - val_acc: 0.7900
Current validation accuracy = [0.79000000000000004]

--------------------------------------------------
Iteration 34
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4709 - acc: 0.8158 - val_loss: 0.5207 - val_acc: 0.7872
Current validation accuracy = [0.78720000000000001]

--------------------------------------------------
Iteration 35
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4644 - acc: 0.8185 - val_loss: 0.5005 - val_acc: 0.7994
Current validation accuracy = [0.79944999999999999]

--------------------------------------------------
Iteration 36
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 44s - loss: 0.4618 - acc: 0.8179 - val_loss: 0.4971 - val_acc: 0.8019
Current validation accuracy = [0.80189999999999995]

--------------------------------------------------
Iteration 37
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 43s - loss: 0.4485 - acc: 0.8242 - val_loss: 0.5088 - val_acc: 0.7964
Current validation accuracy = [0.7964]

--------------------------------------------------
Iteration 38
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4443 - acc: 0.8257 - val_loss: 0.4846 - val_acc: 0.8038
Current validation accuracy = [0.80384999999999995]

--------------------------------------------------
Iteration 39
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 44s - loss: 0.4396 - acc: 0.8271 - val_loss: 0.4818 - val_acc: 0.8046
Current validation accuracy = [0.80464999999999998]

--------------------------------------------------
Iteration 40
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 43s - loss: 0.4282 - acc: 0.8324 - val_loss: 0.4976 - val_acc: 0.7943
Current validation accuracy = [0.79425000000000001]

--------------------------------------------------
Iteration 41
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 43s - loss: 0.4218 - acc: 0.8345 - val_loss: 0.4928 - val_acc: 0.7995
Current validation accuracy = [0.79949999999999999]

--------------------------------------------------
Iteration 42
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4213 - acc: 0.8345 - val_loss: 0.4706 - val_acc: 0.8078
Current validation accuracy = [0.80784999999999996]

--------------------------------------------------
Iteration 43
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4120 - acc: 0.8377 - val_loss: 0.4542 - val_acc: 0.8166
Current validation accuracy = [0.81664999999999999]

--------------------------------------------------
Iteration 44
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.4030 - acc: 0.8427 - val_loss: 0.4515 - val_acc: 0.8158
Current validation accuracy = [0.81579999999999997]

--------------------------------------------------
Iteration 45
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.4036 - acc: 0.8409 - val_loss: 0.4745 - val_acc: 0.8074
Current validation accuracy = [0.80735000000000001]

--------------------------------------------------
Iteration 46
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.3945 - acc: 0.8459 - val_loss: 0.4672 - val_acc: 0.8057
Current validation accuracy = [0.80569999999999997]

--------------------------------------------------
Iteration 47
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.3880 - acc: 0.8483 - val_loss: 0.4528 - val_acc: 0.8164
Current validation accuracy = [0.81640000000000001]

--------------------------------------------------
Iteration 48
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 42s - loss: 0.3896 - acc: 0.8459 - val_loss: 0.4415 - val_acc: 0.8195
Current validation accuracy = [0.81950000000000001]

--------------------------------------------------
Iteration 49
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 41s - loss: 0.3858 - acc: 0.8468 - val_loss: 0.4454 - val_acc: 0.8196
Current validation accuracy = [0.81955]

In [42]:
X_train, y_train, X_val, y_val = create_train_valid(questions, expected, 3, 2)


Vectorization...
(4500, 7, 12)
(4500, 4, 12)

In [43]:
model = build_model(HIDDEN_SIZE, LAYERS, 3, 2)


Build model...

In [44]:
val_acc_3_2 = learning(model, X_train, y_train, 50, X_val, y_val)


--------------------------------------------------
Iteration 1
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 2.1080 - acc: 0.2800 - val_loss: 1.9476 - val_acc: 0.3295
Current validation accuracy = [0.32950000214576719]

--------------------------------------------------
Iteration 2
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.8172 - acc: 0.3652 - val_loss: 1.7454 - val_acc: 0.3705
Current validation accuracy = [0.37049999833106995]

--------------------------------------------------
Iteration 3
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.6995 - acc: 0.3834 - val_loss: 1.7012 - val_acc: 0.3735
Current validation accuracy = [0.37350000119209292]

--------------------------------------------------
Iteration 4
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.6591 - acc: 0.3922 - val_loss: 1.6597 - val_acc: 0.3845
Current validation accuracy = [0.38450000047683713]

--------------------------------------------------
Iteration 5
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.6244 - acc: 0.4018 - val_loss: 1.6345 - val_acc: 0.3930
Current validation accuracy = [0.39300000286102293]

--------------------------------------------------
Iteration 6
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.5838 - acc: 0.4188 - val_loss: 1.6107 - val_acc: 0.4240
Current validation accuracy = [0.4239999985694885]

--------------------------------------------------
Iteration 7
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.5436 - acc: 0.4353 - val_loss: 1.5457 - val_acc: 0.4315
Current validation accuracy = [0.43149999737739564]

--------------------------------------------------
Iteration 8
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.5042 - acc: 0.4491 - val_loss: 1.5555 - val_acc: 0.4455
Current validation accuracy = [0.44550000286102293]

--------------------------------------------------
Iteration 9
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.4735 - acc: 0.4644 - val_loss: 1.4741 - val_acc: 0.4520
Current validation accuracy = [0.45199999713897704]

--------------------------------------------------
Iteration 10
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.4188 - acc: 0.4804 - val_loss: 1.4458 - val_acc: 0.4605
Current validation accuracy = [0.4604999988079071]

--------------------------------------------------
Iteration 11
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.3818 - acc: 0.4914 - val_loss: 1.4223 - val_acc: 0.4745
Current validation accuracy = [0.47450000047683716]

--------------------------------------------------
Iteration 12
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.3426 - acc: 0.5058 - val_loss: 1.3815 - val_acc: 0.4900
Current validation accuracy = [0.48999999904632568]

--------------------------------------------------
Iteration 13
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.3145 - acc: 0.5136 - val_loss: 1.3685 - val_acc: 0.4925
Current validation accuracy = [0.49249999785423276]

--------------------------------------------------
Iteration 14
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.2798 - acc: 0.5268 - val_loss: 1.3274 - val_acc: 0.4985
Current validation accuracy = [0.49849999952316282]

--------------------------------------------------
Iteration 15
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.2489 - acc: 0.5349 - val_loss: 1.3007 - val_acc: 0.5125
Current validation accuracy = [0.5125000057220459]

--------------------------------------------------
Iteration 16
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.2259 - acc: 0.5440 - val_loss: 1.3098 - val_acc: 0.5120
Current validation accuracy = [0.51200000119209288]

--------------------------------------------------
Iteration 17
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.2263 - acc: 0.5479 - val_loss: 1.2555 - val_acc: 0.5155
Current validation accuracy = [0.51549999809265135]

--------------------------------------------------
Iteration 18
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.1825 - acc: 0.5594 - val_loss: 1.2366 - val_acc: 0.5270
Current validation accuracy = [0.52700000143051151]

--------------------------------------------------
Iteration 19
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.1538 - acc: 0.5718 - val_loss: 1.2195 - val_acc: 0.5415
Current validation accuracy = [0.54149999904632573]

--------------------------------------------------
Iteration 20
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.1356 - acc: 0.5778 - val_loss: 1.2036 - val_acc: 0.5425
Current validation accuracy = [0.54249999332427978]

--------------------------------------------------
Iteration 21
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.1158 - acc: 0.5831 - val_loss: 1.1916 - val_acc: 0.5515
Current validation accuracy = [0.55149999666213989]

--------------------------------------------------
Iteration 22
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0935 - acc: 0.5948 - val_loss: 1.1728 - val_acc: 0.5495
Current validation accuracy = [0.54949999904632574]

--------------------------------------------------
Iteration 23
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0725 - acc: 0.6042 - val_loss: 1.1548 - val_acc: 0.5665
Current validation accuracy = [0.56649999761581415]

--------------------------------------------------
Iteration 24
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0539 - acc: 0.6096 - val_loss: 1.1479 - val_acc: 0.5680
Current validation accuracy = [0.5680000023841858]

--------------------------------------------------
Iteration 25
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0404 - acc: 0.6130 - val_loss: 1.1342 - val_acc: 0.5730
Current validation accuracy = [0.57300000619888303]

--------------------------------------------------
Iteration 26
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0211 - acc: 0.6235 - val_loss: 1.1272 - val_acc: 0.5700
Current validation accuracy = [0.56999999427795411]

--------------------------------------------------
Iteration 27
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9987 - acc: 0.6323 - val_loss: 1.1111 - val_acc: 0.5780
Current validation accuracy = [0.57800000619888303]

--------------------------------------------------
Iteration 28
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9818 - acc: 0.6417 - val_loss: 1.0868 - val_acc: 0.5880
Current validation accuracy = [0.58799999809265135]

--------------------------------------------------
Iteration 29
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9621 - acc: 0.6506 - val_loss: 1.0788 - val_acc: 0.5855
Current validation accuracy = [0.58549999809265141]

--------------------------------------------------
Iteration 30
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9459 - acc: 0.6524 - val_loss: 1.0993 - val_acc: 0.5800
Current validation accuracy = [0.57999999761581422]

--------------------------------------------------
Iteration 31
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9295 - acc: 0.6614 - val_loss: 1.0613 - val_acc: 0.5900
Current validation accuracy = [0.58999999475479126]

--------------------------------------------------
Iteration 32
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9182 - acc: 0.6679 - val_loss: 1.0540 - val_acc: 0.5895
Current validation accuracy = [0.58949999570846556]

--------------------------------------------------
Iteration 33
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8891 - acc: 0.6793 - val_loss: 1.0403 - val_acc: 0.5975
Current validation accuracy = [0.59750000667572023]

--------------------------------------------------
Iteration 34
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 0.8708 - acc: 0.6853 - val_loss: 1.0442 - val_acc: 0.5975
Current validation accuracy = [0.59750000190734864]

--------------------------------------------------
Iteration 35
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8509 - acc: 0.6923 - val_loss: 1.0143 - val_acc: 0.6080
Current validation accuracy = [0.60800000476837157]

--------------------------------------------------
Iteration 36
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8382 - acc: 0.6978 - val_loss: 1.0259 - val_acc: 0.6060
Current validation accuracy = [0.60600000667572018]

--------------------------------------------------
Iteration 37
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8196 - acc: 0.7062 - val_loss: 1.0023 - val_acc: 0.6120
Current validation accuracy = [0.6119999985694885]

--------------------------------------------------
Iteration 38
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.7986 - acc: 0.7147 - val_loss: 0.9883 - val_acc: 0.6180
Current validation accuracy = [0.61799999618530277]

--------------------------------------------------
Iteration 39
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 0.7867 - acc: 0.7184 - val_loss: 0.9890 - val_acc: 0.6170
Current validation accuracy = [0.61699999904632563]

--------------------------------------------------
Iteration 40
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 0.7644 - acc: 0.7274 - val_loss: 0.9809 - val_acc: 0.6240
Current validation accuracy = [0.6239999966621399]

--------------------------------------------------
Iteration 41
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.7465 - acc: 0.7366 - val_loss: 0.9539 - val_acc: 0.6275
Current validation accuracy = [0.62749999332427975]

--------------------------------------------------
Iteration 42
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.7310 - acc: 0.7430 - val_loss: 0.9569 - val_acc: 0.6285
Current validation accuracy = [0.62849999666213985]

--------------------------------------------------
Iteration 43
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.7112 - acc: 0.7470 - val_loss: 0.9401 - val_acc: 0.6395
Current validation accuracy = [0.63949999475479125]

--------------------------------------------------
Iteration 44
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.6918 - acc: 0.7579 - val_loss: 0.9259 - val_acc: 0.6365
Current validation accuracy = [0.63649999713897709]

--------------------------------------------------
Iteration 45
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.6741 - acc: 0.7684 - val_loss: 0.9227 - val_acc: 0.6395
Current validation accuracy = [0.63949999475479125]

--------------------------------------------------
Iteration 46
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.6615 - acc: 0.7702 - val_loss: 0.9193 - val_acc: 0.6445
Current validation accuracy = [0.6444999971389771]

--------------------------------------------------
Iteration 47
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.6454 - acc: 0.7759 - val_loss: 0.9099 - val_acc: 0.6470
Current validation accuracy = [0.64700000286102299]

--------------------------------------------------
Iteration 48
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.6246 - acc: 0.7888 - val_loss: 0.8916 - val_acc: 0.6650
Current validation accuracy = [0.66499999523162845]

--------------------------------------------------
Iteration 49
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.6071 - acc: 0.7960 - val_loss: 0.8935 - val_acc: 0.6515
Current validation accuracy = [0.65150000619888304]

In [46]:
questions, expected = generate_data(TRAINING_SIZE, 2, 3)
X_train, y_train, X_val, y_val = create_train_valid(questions, expected, 2, 3)
model = build_model(HIDDEN_SIZE, LAYERS, 2, 3)
val_acc_2_3 = learning(model, X_train, y_train, 50, X_val, y_val)


Generating data... 
Total addition questions: 5000
Vectorization...
(4500, 8, 12)
(4500, 3, 12)
Build model...

--------------------------------------------------
Iteration 1
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 2.2378 - acc: 0.2673 - val_loss: 2.0186 - val_acc: 0.3880
Current validation accuracy = [0.38799999690055847]

--------------------------------------------------
Iteration 2
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.8676 - acc: 0.3744 - val_loss: 1.7854 - val_acc: 0.3953
Current validation accuracy = [0.39533333849906921]

--------------------------------------------------
Iteration 3
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.7551 - acc: 0.3833 - val_loss: 1.7257 - val_acc: 0.3833
Current validation accuracy = [0.38333333277702331]

--------------------------------------------------
Iteration 4
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.7051 - acc: 0.3917 - val_loss: 1.7177 - val_acc: 0.3793
Current validation accuracy = [0.37933333468437197]

--------------------------------------------------
Iteration 5
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.6529 - acc: 0.4064 - val_loss: 1.6609 - val_acc: 0.3987
Current validation accuracy = [0.39866666674613954]

--------------------------------------------------
Iteration 6
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.6175 - acc: 0.4200 - val_loss: 1.6029 - val_acc: 0.4073
Current validation accuracy = [0.40733333611488343]

--------------------------------------------------
Iteration 7
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.5722 - acc: 0.4313 - val_loss: 1.5746 - val_acc: 0.4347
Current validation accuracy = [0.43466666889190675]

--------------------------------------------------
Iteration 8
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.5241 - acc: 0.4462 - val_loss: 1.5384 - val_acc: 0.4413
Current validation accuracy = [0.44133333015441895]

--------------------------------------------------
Iteration 9
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.4845 - acc: 0.4579 - val_loss: 1.4944 - val_acc: 0.4660
Current validation accuracy = [0.46599999833106992]

--------------------------------------------------
Iteration 10
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.4495 - acc: 0.4755 - val_loss: 1.4691 - val_acc: 0.4520
Current validation accuracy = [0.45199999928474427]

--------------------------------------------------
Iteration 11
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.3949 - acc: 0.4919 - val_loss: 1.4071 - val_acc: 0.4880
Current validation accuracy = [0.48800000047683717]

--------------------------------------------------
Iteration 12
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.3798 - acc: 0.4911 - val_loss: 1.3712 - val_acc: 0.4880
Current validation accuracy = [0.48800000548362732]

--------------------------------------------------
Iteration 13
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.3211 - acc: 0.5133 - val_loss: 1.3414 - val_acc: 0.5013
Current validation accuracy = [0.50133333063125607]

--------------------------------------------------
Iteration 14
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.2776 - acc: 0.5339 - val_loss: 1.3181 - val_acc: 0.5067
Current validation accuracy = [0.50666667318344116]

--------------------------------------------------
Iteration 15
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.2488 - acc: 0.5405 - val_loss: 1.2812 - val_acc: 0.5313
Current validation accuracy = [0.53133335351943967]

--------------------------------------------------
Iteration 16
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.2251 - acc: 0.5516 - val_loss: 1.2285 - val_acc: 0.5467
Current validation accuracy = [0.54666666412353515]

--------------------------------------------------
Iteration 17
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.2113 - acc: 0.5479 - val_loss: 1.2512 - val_acc: 0.5240
Current validation accuracy = [0.52399999618530269]

--------------------------------------------------
Iteration 18
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1743 - acc: 0.5653 - val_loss: 1.2097 - val_acc: 0.5427
Current validation accuracy = [0.54266665935516356]

--------------------------------------------------
Iteration 19
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.1655 - acc: 0.5681 - val_loss: 1.1834 - val_acc: 0.5647
Current validation accuracy = [0.56466665697097773]

--------------------------------------------------
Iteration 20
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.1664 - acc: 0.5627 - val_loss: 1.1724 - val_acc: 0.5560
Current validation accuracy = [0.55599999237060549]

--------------------------------------------------
Iteration 21
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.1391 - acc: 0.5762 - val_loss: 1.1466 - val_acc: 0.5600
Current validation accuracy = [0.55999999809265133]

--------------------------------------------------
Iteration 22
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.1015 - acc: 0.5896 - val_loss: 1.1232 - val_acc: 0.5847
Current validation accuracy = [0.58466665697097775]

--------------------------------------------------
Iteration 23
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0843 - acc: 0.5971 - val_loss: 1.1654 - val_acc: 0.5360
Current validation accuracy = [0.53600000190734864]

--------------------------------------------------
Iteration 24
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0651 - acc: 0.6021 - val_loss: 1.1375 - val_acc: 0.5620
Current validation accuracy = [0.56199999570846559]

--------------------------------------------------
Iteration 25
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.0572 - acc: 0.6044 - val_loss: 1.1366 - val_acc: 0.5553
Current validation accuracy = [0.55533333921432493]

--------------------------------------------------
Iteration 26
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0483 - acc: 0.6068 - val_loss: 1.0976 - val_acc: 0.5793
Current validation accuracy = [0.57933333063125614]

--------------------------------------------------
Iteration 27
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0236 - acc: 0.6230 - val_loss: 1.0776 - val_acc: 0.5907
Current validation accuracy = [0.59066666507720944]

--------------------------------------------------
Iteration 28
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 1.0254 - acc: 0.6161 - val_loss: 1.1125 - val_acc: 0.5800
Current validation accuracy = [0.5800000038146973]

--------------------------------------------------
Iteration 29
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.0232 - acc: 0.6171 - val_loss: 1.0576 - val_acc: 0.6067
Current validation accuracy = [0.60666665554046628]

--------------------------------------------------
Iteration 30
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 0.9876 - acc: 0.6335 - val_loss: 1.0549 - val_acc: 0.6020
Current validation accuracy = [0.60199999904632573]

--------------------------------------------------
Iteration 31
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 0.9934 - acc: 0.6321 - val_loss: 1.0385 - val_acc: 0.6087
Current validation accuracy = [0.60866665792465213]

--------------------------------------------------
Iteration 32
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9678 - acc: 0.6401 - val_loss: 1.0387 - val_acc: 0.6020
Current validation accuracy = [0.60200000381469732]

--------------------------------------------------
Iteration 33
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9605 - acc: 0.6429 - val_loss: 1.0339 - val_acc: 0.6027
Current validation accuracy = [0.60266666030883786]

--------------------------------------------------
Iteration 34
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 0.9491 - acc: 0.6515 - val_loss: 1.0153 - val_acc: 0.6107
Current validation accuracy = [0.6106666541099548]

--------------------------------------------------
Iteration 35
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9490 - acc: 0.6499 - val_loss: 1.0178 - val_acc: 0.6240
Current validation accuracy = [0.62399998950958246]

--------------------------------------------------
Iteration 36
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 0.9371 - acc: 0.6557 - val_loss: 1.0143 - val_acc: 0.6167
Current validation accuracy = [0.61666667985916135]

--------------------------------------------------
Iteration 37
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9265 - acc: 0.6613 - val_loss: 1.0095 - val_acc: 0.6193
Current validation accuracy = [0.61933333063125606]

--------------------------------------------------
Iteration 38
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9228 - acc: 0.6629 - val_loss: 1.0019 - val_acc: 0.6193
Current validation accuracy = [0.61933332967758181]

--------------------------------------------------
Iteration 39
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9041 - acc: 0.6738 - val_loss: 1.0669 - val_acc: 0.5860
Current validation accuracy = [0.58600002145767216]

--------------------------------------------------
Iteration 40
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9386 - acc: 0.6519 - val_loss: 0.9860 - val_acc: 0.6333
Current validation accuracy = [0.63333333873748776]

--------------------------------------------------
Iteration 41
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9060 - acc: 0.6667 - val_loss: 1.0196 - val_acc: 0.6080
Current validation accuracy = [0.60800000715255742]

--------------------------------------------------
Iteration 42
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8940 - acc: 0.6763 - val_loss: 0.9762 - val_acc: 0.6300
Current validation accuracy = [0.62999999904632564]

--------------------------------------------------
Iteration 43
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.9031 - acc: 0.6678 - val_loss: 0.9903 - val_acc: 0.6153
Current validation accuracy = [0.61533332729339596]

--------------------------------------------------
Iteration 44
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8774 - acc: 0.6806 - val_loss: 0.9847 - val_acc: 0.6320
Current validation accuracy = [0.63199999523162842]

--------------------------------------------------
Iteration 45
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8758 - acc: 0.6831 - val_loss: 0.9621 - val_acc: 0.6407
Current validation accuracy = [0.64066665887832641]

--------------------------------------------------
Iteration 46
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8733 - acc: 0.6803 - val_loss: 0.9744 - val_acc: 0.6280
Current validation accuracy = [0.62799998617172237]

--------------------------------------------------
Iteration 47
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 2s - loss: 0.8606 - acc: 0.6879 - val_loss: 0.9517 - val_acc: 0.6373
Current validation accuracy = [0.63733332586288449]

--------------------------------------------------
Iteration 48
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8455 - acc: 0.6956 - val_loss: 0.9483 - val_acc: 0.6453
Current validation accuracy = [0.64533334112167362]

--------------------------------------------------
Iteration 49
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 0.8528 - acc: 0.6902 - val_loss: 0.9388 - val_acc: 0.6547
Current validation accuracy = [0.65466666221618652]

In [48]:
questions, expected = generate_data(TRAINING_SIZE, 3, 3)
X_train, y_train, X_val, y_val = create_train_valid(questions, expected, 3, 3)
model = build_model(HIDDEN_SIZE, LAYERS, 3, 3)
val_acc_3_3 = learning(model, X_train, y_train, 50, X_val, y_val)


Generating data... 
Total addition questions: 5000
Vectorization...
(4500, 11, 12)
(4500, 4, 12)
Build model...

--------------------------------------------------
Iteration 1
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 2.1491 - acc: 0.2719 - val_loss: 1.9375 - val_acc: 0.3605
Current validation accuracy = [0.36049999785423281]

--------------------------------------------------
Iteration 2
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.8317 - acc: 0.3777 - val_loss: 1.8095 - val_acc: 0.3755
Current validation accuracy = [0.37549999952316282]

--------------------------------------------------
Iteration 3
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7611 - acc: 0.3820 - val_loss: 1.7523 - val_acc: 0.3815
Current validation accuracy = [0.38150000119209287]

--------------------------------------------------
Iteration 4
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7450 - acc: 0.3866 - val_loss: 1.7818 - val_acc: 0.3840
Current validation accuracy = [0.38400000119209288]

--------------------------------------------------
Iteration 5
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7325 - acc: 0.3894 - val_loss: 1.7243 - val_acc: 0.3960
Current validation accuracy = [0.39599999761581423]

--------------------------------------------------
Iteration 6
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7126 - acc: 0.3907 - val_loss: 1.7112 - val_acc: 0.3930
Current validation accuracy = [0.39300000000000002]

--------------------------------------------------
Iteration 7
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.6986 - acc: 0.3945 - val_loss: 1.7356 - val_acc: 0.3865
Current validation accuracy = [0.38650000333786011]

--------------------------------------------------
Iteration 8
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.6931 - acc: 0.3953 - val_loss: 1.6847 - val_acc: 0.3880
Current validation accuracy = [0.38800000286102293]

--------------------------------------------------
Iteration 9
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.6699 - acc: 0.3984 - val_loss: 1.7331 - val_acc: 0.3985
Current validation accuracy = [0.39849999713897705]

--------------------------------------------------
Iteration 10
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.6694 - acc: 0.4018 - val_loss: 1.6519 - val_acc: 0.4100
Current validation accuracy = [0.41000000143051146]

--------------------------------------------------
Iteration 11
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.6414 - acc: 0.4029 - val_loss: 1.6289 - val_acc: 0.4170
Current validation accuracy = [0.41700000143051147]

--------------------------------------------------
Iteration 12
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.6189 - acc: 0.4107 - val_loss: 1.6019 - val_acc: 0.4255
Current validation accuracy = [0.42549999904632568]

--------------------------------------------------
Iteration 13
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.5977 - acc: 0.4187 - val_loss: 1.6026 - val_acc: 0.4230
Current validation accuracy = [0.42300000190734866]

--------------------------------------------------
Iteration 14
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.5696 - acc: 0.4253 - val_loss: 1.5525 - val_acc: 0.4270
Current validation accuracy = [0.42700000023841855]

--------------------------------------------------
Iteration 15
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.5477 - acc: 0.4307 - val_loss: 1.5471 - val_acc: 0.4355
Current validation accuracy = [0.43550000190734861]

--------------------------------------------------
Iteration 16
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.5119 - acc: 0.4419 - val_loss: 1.5903 - val_acc: 0.4305
Current validation accuracy = [0.43050000071525574]

--------------------------------------------------
Iteration 17
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.5039 - acc: 0.4462 - val_loss: 1.5131 - val_acc: 0.4545
Current validation accuracy = [0.45449999713897704]

--------------------------------------------------
Iteration 18
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.4677 - acc: 0.4559 - val_loss: 1.4655 - val_acc: 0.4530
Current validation accuracy = [0.45300000000000001]

--------------------------------------------------
Iteration 19
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.4438 - acc: 0.4671 - val_loss: 1.4515 - val_acc: 0.4710
Current validation accuracy = [0.47100000047683716]

--------------------------------------------------
Iteration 20
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.4170 - acc: 0.4746 - val_loss: 1.4253 - val_acc: 0.4680
Current validation accuracy = [0.46799999809265136]

--------------------------------------------------
Iteration 21
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.4030 - acc: 0.4834 - val_loss: 1.4145 - val_acc: 0.4820
Current validation accuracy = [0.48199999761581419]

--------------------------------------------------
Iteration 22
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.3805 - acc: 0.4886 - val_loss: 1.4226 - val_acc: 0.4665
Current validation accuracy = [0.46650000095367433]

--------------------------------------------------
Iteration 23
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.3605 - acc: 0.4968 - val_loss: 1.3972 - val_acc: 0.4770
Current validation accuracy = [0.47699999928474424]

--------------------------------------------------
Iteration 24
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 3s - loss: 1.3538 - acc: 0.4943 - val_loss: 1.3861 - val_acc: 0.4840
Current validation accuracy = [0.48400000309944152]

--------------------------------------------------
Iteration 25
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.3272 - acc: 0.5078 - val_loss: 1.3861 - val_acc: 0.4830
Current validation accuracy = [0.4830000026226044]

--------------------------------------------------
Iteration 26
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.3079 - acc: 0.5138 - val_loss: 1.3540 - val_acc: 0.4860
Current validation accuracy = [0.48599999952316286]

--------------------------------------------------
Iteration 27
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.2897 - acc: 0.5228 - val_loss: 1.3584 - val_acc: 0.4905
Current validation accuracy = [0.49050000190734866]

--------------------------------------------------
Iteration 28
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.2835 - acc: 0.5237 - val_loss: 1.3536 - val_acc: 0.4970
Current validation accuracy = [0.49700000286102297]

--------------------------------------------------
Iteration 29
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.2669 - acc: 0.5341 - val_loss: 1.3363 - val_acc: 0.5085
Current validation accuracy = [0.50849999761581421]

--------------------------------------------------
Iteration 30
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2528 - acc: 0.5383 - val_loss: 1.3528 - val_acc: 0.5010
Current validation accuracy = [0.50099999475479129]

--------------------------------------------------
Iteration 31
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.2503 - acc: 0.5392 - val_loss: 1.3543 - val_acc: 0.5035
Current validation accuracy = [0.50350000119209293]

--------------------------------------------------
Iteration 32
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.2312 - acc: 0.5462 - val_loss: 1.3076 - val_acc: 0.5190
Current validation accuracy = [0.51899999475479131]

--------------------------------------------------
Iteration 33
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.2117 - acc: 0.5510 - val_loss: 1.3136 - val_acc: 0.5050
Current validation accuracy = [0.50500000119209287]

--------------------------------------------------
Iteration 34
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.2127 - acc: 0.5507 - val_loss: 1.3248 - val_acc: 0.5075
Current validation accuracy = [0.50749999761581421]

--------------------------------------------------
Iteration 35
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1967 - acc: 0.5638 - val_loss: 1.3068 - val_acc: 0.5085
Current validation accuracy = [0.50850000286102293]

--------------------------------------------------
Iteration 36
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1832 - acc: 0.5642 - val_loss: 1.2960 - val_acc: 0.5215
Current validation accuracy = [0.52150000667572016]

--------------------------------------------------
Iteration 37
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1672 - acc: 0.5770 - val_loss: 1.2926 - val_acc: 0.5205
Current validation accuracy = [0.52050000095367432]

--------------------------------------------------
Iteration 38
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1549 - acc: 0.5777 - val_loss: 1.2870 - val_acc: 0.5295
Current validation accuracy = [0.52950000095367433]

--------------------------------------------------
Iteration 39
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1420 - acc: 0.5838 - val_loss: 1.2996 - val_acc: 0.5100
Current validation accuracy = [0.5099999947547913]

--------------------------------------------------
Iteration 40
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1373 - acc: 0.5831 - val_loss: 1.2896 - val_acc: 0.5250
Current validation accuracy = [0.52500000381469725]

--------------------------------------------------
Iteration 41
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1247 - acc: 0.5910 - val_loss: 1.2841 - val_acc: 0.5295
Current validation accuracy = [0.52950000143051146]

--------------------------------------------------
Iteration 42
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1073 - acc: 0.6016 - val_loss: 1.2655 - val_acc: 0.5345
Current validation accuracy = [0.53450000095367434]

--------------------------------------------------
Iteration 43
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.1060 - acc: 0.5986 - val_loss: 1.3013 - val_acc: 0.5150
Current validation accuracy = [0.51500000047683714]

--------------------------------------------------
Iteration 44
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.0888 - acc: 0.6068 - val_loss: 1.2677 - val_acc: 0.5385
Current validation accuracy = [0.53849999904632573]

--------------------------------------------------
Iteration 45
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.0854 - acc: 0.6093 - val_loss: 1.2675 - val_acc: 0.5380
Current validation accuracy = [0.53799999332427983]

--------------------------------------------------
Iteration 46
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.0686 - acc: 0.6152 - val_loss: 1.2771 - val_acc: 0.5360
Current validation accuracy = [0.53599999380111696]

--------------------------------------------------
Iteration 47
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.0619 - acc: 0.6198 - val_loss: 1.2921 - val_acc: 0.5270
Current validation accuracy = [0.52699999809265141]

--------------------------------------------------
Iteration 48
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.0453 - acc: 0.6278 - val_loss: 1.2754 - val_acc: 0.5335
Current validation accuracy = [0.53349999856948849]

--------------------------------------------------
Iteration 49
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.0375 - acc: 0.6270 - val_loss: 1.2800 - val_acc: 0.5215
Current validation accuracy = [0.52149999809265135]

In [50]:
questions, expected = generate_data(TRAINING_SIZE, 3, 4)
X_train, y_train, X_val, y_val = create_train_valid(questions, expected, 3, 4)
model = build_model(HIDDEN_SIZE, LAYERS, 3, 4)
val_acc_3_4 = learning(model, X_train, y_train, 50, X_val, y_val)


Generating data... 
Total addition questions: 5000
Vectorization...
(4500, 15, 12)
(4500, 4, 12)
Build model...

--------------------------------------------------
Iteration 1
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 2.1641 - acc: 0.2753 - val_loss: 1.9400 - val_acc: 0.3530
Current validation accuracy = [0.35299999856948855]

--------------------------------------------------
Iteration 2
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.9000 - acc: 0.3496 - val_loss: 1.8691 - val_acc: 0.3585
Current validation accuracy = [0.3585000026226044]

--------------------------------------------------
Iteration 3
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.8458 - acc: 0.3583 - val_loss: 1.8539 - val_acc: 0.3545
Current validation accuracy = [0.35449999856948855]

--------------------------------------------------
Iteration 4
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.8302 - acc: 0.3650 - val_loss: 1.8399 - val_acc: 0.3540
Current validation accuracy = [0.35399999785423281]

--------------------------------------------------
Iteration 5
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.8166 - acc: 0.3648 - val_loss: 1.8344 - val_acc: 0.3510
Current validation accuracy = [0.3509999969005585]

--------------------------------------------------
Iteration 6
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.8098 - acc: 0.3616 - val_loss: 1.8257 - val_acc: 0.3535
Current validation accuracy = [0.35349999737739562]

--------------------------------------------------
Iteration 7
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.8014 - acc: 0.3646 - val_loss: 1.8278 - val_acc: 0.3530
Current validation accuracy = [0.35299999666213988]

--------------------------------------------------
Iteration 8
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.7848 - acc: 0.3658 - val_loss: 1.8008 - val_acc: 0.3610
Current validation accuracy = [0.36099999904632568]

--------------------------------------------------
Iteration 9
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7666 - acc: 0.3690 - val_loss: 1.8429 - val_acc: 0.3410
Current validation accuracy = [0.34100000333786012]

--------------------------------------------------
Iteration 10
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7559 - acc: 0.3688 - val_loss: 1.7724 - val_acc: 0.3585
Current validation accuracy = [0.35849999785423281]

--------------------------------------------------
Iteration 11
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.7283 - acc: 0.3765 - val_loss: 1.7634 - val_acc: 0.3590
Current validation accuracy = [0.35899999666213989]

--------------------------------------------------
Iteration 12
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7362 - acc: 0.3760 - val_loss: 1.7657 - val_acc: 0.3540
Current validation accuracy = [0.35399999690055844]

--------------------------------------------------
Iteration 13
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.7125 - acc: 0.3811 - val_loss: 1.7013 - val_acc: 0.3730
Current validation accuracy = [0.37299999713897702]

--------------------------------------------------
Iteration 14
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.6646 - acc: 0.3902 - val_loss: 1.6765 - val_acc: 0.3765
Current validation accuracy = [0.37649999785423277]

--------------------------------------------------
Iteration 15
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.6397 - acc: 0.3967 - val_loss: 1.6472 - val_acc: 0.3820
Current validation accuracy = [0.3820000023841858]

--------------------------------------------------
Iteration 16
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.6235 - acc: 0.3999 - val_loss: 1.6408 - val_acc: 0.3945
Current validation accuracy = [0.39450000000000002]

--------------------------------------------------
Iteration 17
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.6018 - acc: 0.4021 - val_loss: 1.6262 - val_acc: 0.3930
Current validation accuracy = [0.39299999713897704]

--------------------------------------------------
Iteration 18
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.6044 - acc: 0.4074 - val_loss: 1.6926 - val_acc: 0.3775
Current validation accuracy = [0.3775]

--------------------------------------------------
Iteration 19
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.5871 - acc: 0.4133 - val_loss: 1.6209 - val_acc: 0.3925
Current validation accuracy = [0.39250000214576719]

--------------------------------------------------
Iteration 20
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.5477 - acc: 0.4226 - val_loss: 1.5862 - val_acc: 0.4200
Current validation accuracy = [0.42000000309944152]

--------------------------------------------------
Iteration 21
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.5223 - acc: 0.4312 - val_loss: 1.7211 - val_acc: 0.3820
Current validation accuracy = [0.38200000333786011]

--------------------------------------------------
Iteration 22
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.5479 - acc: 0.4262 - val_loss: 1.5753 - val_acc: 0.4075
Current validation accuracy = [0.40749999713897705]

--------------------------------------------------
Iteration 23
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.5042 - acc: 0.4403 - val_loss: 1.5841 - val_acc: 0.4045
Current validation accuracy = [0.40449999761581423]

--------------------------------------------------
Iteration 24
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.4886 - acc: 0.4430 - val_loss: 1.5262 - val_acc: 0.4255
Current validation accuracy = [0.42550000143051148]

--------------------------------------------------
Iteration 25
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.4717 - acc: 0.4505 - val_loss: 1.5498 - val_acc: 0.4145
Current validation accuracy = [0.41449999856948855]

--------------------------------------------------
Iteration 26
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.4673 - acc: 0.4462 - val_loss: 1.4976 - val_acc: 0.4400
Current validation accuracy = [0.44000000190734861]

--------------------------------------------------
Iteration 27
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.4476 - acc: 0.4561 - val_loss: 1.4727 - val_acc: 0.4450
Current validation accuracy = [0.44499999666213991]

--------------------------------------------------
Iteration 28
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.4271 - acc: 0.4670 - val_loss: 1.4679 - val_acc: 0.4430
Current validation accuracy = [0.44299999785423277]

--------------------------------------------------
Iteration 29
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.4193 - acc: 0.4679 - val_loss: 1.4574 - val_acc: 0.4475
Current validation accuracy = [0.44750000000000001]

--------------------------------------------------
Iteration 30
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.3991 - acc: 0.4744 - val_loss: 1.4553 - val_acc: 0.4465
Current validation accuracy = [0.4465000014305115]

--------------------------------------------------
Iteration 31
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.3895 - acc: 0.4789 - val_loss: 1.4599 - val_acc: 0.4455
Current validation accuracy = [0.44550000071525575]

--------------------------------------------------
Iteration 32
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.4017 - acc: 0.4729 - val_loss: 1.4458 - val_acc: 0.4630
Current validation accuracy = [0.46299999713897705]

--------------------------------------------------
Iteration 33
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.3912 - acc: 0.4805 - val_loss: 1.4496 - val_acc: 0.4395
Current validation accuracy = [0.43950000309944154]

--------------------------------------------------
Iteration 34
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 6s - loss: 1.3620 - acc: 0.4946 - val_loss: 1.4733 - val_acc: 0.4345
Current validation accuracy = [0.43449999737739564]

--------------------------------------------------
Iteration 35
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.3580 - acc: 0.4929 - val_loss: 1.4098 - val_acc: 0.4610
Current validation accuracy = [0.46099999713897705]

--------------------------------------------------
Iteration 36
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.3419 - acc: 0.4993 - val_loss: 1.4028 - val_acc: 0.4785
Current validation accuracy = [0.47850000214576721]

--------------------------------------------------
Iteration 37
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.3412 - acc: 0.4986 - val_loss: 1.4091 - val_acc: 0.4785
Current validation accuracy = [0.47849999690055844]

--------------------------------------------------
Iteration 38
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.3278 - acc: 0.5023 - val_loss: 1.3839 - val_acc: 0.4850
Current validation accuracy = [0.48500000143051147]

--------------------------------------------------
Iteration 39
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 4s - loss: 1.3184 - acc: 0.5079 - val_loss: 1.4641 - val_acc: 0.4350
Current validation accuracy = [0.43499999904632569]

--------------------------------------------------
Iteration 40
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.3214 - acc: 0.5070 - val_loss: 1.3885 - val_acc: 0.4770
Current validation accuracy = [0.47699999761581419]

--------------------------------------------------
Iteration 41
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2925 - acc: 0.5187 - val_loss: 1.3756 - val_acc: 0.4945
Current validation accuracy = [0.49450000023841856]

--------------------------------------------------
Iteration 42
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2917 - acc: 0.5201 - val_loss: 1.4000 - val_acc: 0.4635
Current validation accuracy = [0.46350000166893007]

--------------------------------------------------
Iteration 43
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2867 - acc: 0.5226 - val_loss: 1.3852 - val_acc: 0.4690
Current validation accuracy = [0.46900000047683715]

--------------------------------------------------
Iteration 44
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2791 - acc: 0.5286 - val_loss: 1.3635 - val_acc: 0.4870
Current validation accuracy = [0.48700000023841861]

--------------------------------------------------
Iteration 45
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2751 - acc: 0.5279 - val_loss: 1.3719 - val_acc: 0.4750
Current validation accuracy = [0.47499999976158142]

--------------------------------------------------
Iteration 46
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2638 - acc: 0.5326 - val_loss: 1.3718 - val_acc: 0.4805
Current validation accuracy = [0.48050000095367429]

--------------------------------------------------
Iteration 47
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2700 - acc: 0.5288 - val_loss: 1.3826 - val_acc: 0.4690
Current validation accuracy = [0.4690000021457672]

--------------------------------------------------
Iteration 48
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2477 - acc: 0.5385 - val_loss: 1.3579 - val_acc: 0.4885
Current validation accuracy = [0.48850000190734866]

--------------------------------------------------
Iteration 49
Train on 4500 samples, validate on 500 samples
Epoch 1/1
4500/4500 [==============================] - 5s - loss: 1.2344 - acc: 0.5434 - val_loss: 1.3476 - val_acc: 0.4900
Current validation accuracy = [0.48999999737739564]

In [13]:
import matplotlib.pyplot as plt
%matplotlib inline 
plt.figure()
# plt.plot(fb_val_acc_2_2, 'y')
# plt.plot(val_acc_2_2)
# plt.plot(val_acc_3_2, 'r')
# plt.plot(val_acc_2_3, 'g')
plt.plot(val_acc_3_3, 'k')
plt.plot(fb_val_acc_3_3, 'g')
# plt.plot(val_acc_3_4, 'c')
# plt.legend(['2 digits 2 ops', '3 digits 2 ops', '2 digits 3 ops', '3 digits 3 ops', '3 digits 4 ops'])


Out[13]:
[<matplotlib.lines.Line2D at 0x7ff1f8e7a410>]

In [70]:
# Analysis of the learned encoder-decoder model

In [13]:
def mapper_f(X):
    # The [0] is to disable the training phase flag
    return mapper_f_([0] + [X])

def decoder_f(X):
    # The [0] is to disable the training phase flag
    return decoder_f_([0] + [X])

def encoder_f(X):
    # The [0] is to disable the training phase flag
    return encoder_f_([0] + [X])

In [404]:
X_str = '81+27'
X_str = X_str[::-1]
print(X_str)
X = ctable.encode(X_str, maxlen=MAXLEN).reshape([1,5,12])
# preds = model.predict_classes(X, verbose=0)
preds = model.predict(X, verbose=0)
# answer = ctable.decode(preds[0], calc_argmax=False)
# print(answer[::-1])
# X = ctable.encode(X_str).reshape([1,5,12])
# W, b = mapper.get_weights()
# mapper_f(X)
# H = decoder_f(X)
X = preds[0].argmax(axis=-1)
''.join(ctable.indices_char[x] for x in X)# ctable.indices_char[x]


72+18
Out[404]:
' 23'

In [69]:
type(np.array(K.eval(model.layers[0].W_i)))
a = np.tanh
a(np.array([0.1, 0.2]))
model.layers[0].output_dim


Out[69]:
384

In [205]:
class LSTMVisualizer:
    # LSTM weights  
    
    def __sigmoid__(self, x):
        return (1 / (1 + np.exp(-x)))
        
    def __init__(self, LSTM):
        # input gate
        self.W_i = np.array(K.eval(LSTM.W_i))
        self.U_i = np.array(K.eval(LSTM.U_i))
        self.b_i = np.transpose(np.array(K.eval(LSTM.b_i)))

        # forget gate
        self.W_f = np.array(K.eval(LSTM.W_f))
        self.U_f = np.array(K.eval(LSTM.U_f))
        self.b_f = np.transpose(np.array(K.eval(LSTM.b_f)))
        
        # cell
        self.W_c = np.array(K.eval(LSTM.W_c))
        self.U_c = np.array(K.eval(LSTM.U_c))
        self.b_c = np.transpose(np.array(K.eval(LSTM.b_c)))
        
        # output gate
        self.W_o = np.array(K.eval(LSTM.W_o))
        self.U_o = np.array(K.eval(LSTM.U_o))
        self.b_o = np.transpose(np.array(K.eval(LSTM.b_o)))
        
        # activation functions
        self.activation = np.tanh # LSTM.activation
        self.inner_activation = self.__sigmoid__ # LSTM.inner_activation
        
        self.dim = LSTM.output_dim

    def compute(self, X):
        # Initialize h
        H = [np.zeros((self.dim,))]
        C = [np.zeros((self.dim,))]
        I = []
        F = []
        O = []
        
        for x in X:
            # Get previous hidden state
            print('x shape=', x.shape)
            h_tml = H[-1]
            print(h_tml.shape)
            c_tml = C[-1]
            
            x_i = np.inner(np.transpose(self.W_i), x) + self.b_i
            print('x_i.shape=', x_i.shape)
            x_f = np.inner(np.transpose(self.W_f), x) + self.b_f
            x_c = np.inner(np.transpose(self.W_c), x) + self.b_c
            x_o = np.inner(np.transpose(self.W_o), x) + self.b_o

            i = self.inner_activation(x_i + np.inner(np.transpose(self.U_i), H[-1]))
            print('i.shape=', i.shape)
            f = self.inner_activation(x_f + np.inner(np.transpose(self.U_f), H[-1]))
            print(f.shape)
            c_new = self.activation(x_c + np.inner(np.transpose(self.U_c), H[-1]))
            c = np.multiply(f, c_tml) + np.multiply(i, c_new)
            print(c.shape)
            o = self.inner_activation(x_o + np.inner(np.transpose(self.U_o), H[-1]))
            print(o.shape)
            h = np.multiply(o, self.activation(c))
            print(h.shape)
            
            I.append(i)
            F.append(f)
            C.append(c)
            O.append(o)
            H.append(h)
        
        return I, F, C, O, H

print(W[:,4]) np.argmax(W[:,4]), np.max(W[:,4])


In [206]:
X1_str = '95+43'
X1_str = X1_str[::-1]
print(X1_str)
X1 = ctable.encode(X1_str, maxlen=MAXLEN).reshape([1,5,12])
preds1 = model.predict_classes(X1, verbose=0)
answer1 = ctable.decode(preds1[0], calc_argmax=False)
print(answer1[::-1])

X2_str = '65+73'
X2_str = X2_str[::-1]
print(X2_str)
X2 = ctable.encode(X2_str, maxlen=MAXLEN).reshape([1,5,12])
preds2 = model.predict_classes(X2, verbose=0)
answer2 = ctable.decode(preds2[0], calc_argmax=False)
print(answer2[::-1])


34+59
78 
37+56
138

In [233]:
viz = LSTMVisualizer(model.layers[0])
np.transpose(viz.W_i).shape
I1, F1, C1, O1, H1 = viz.compute(X1[0])
I2, F2, C2, O2, H2 = viz.compute(X2[0])


x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)
x shape= (12,)
(384,)
x_i.shape= (384,)
i.shape= (384,)
(384,)
(384,)
(384,)
(384,)

In [237]:
H11 = encoder_f(X1)
H22 = encoder_f(X2)

In [238]:
H11[0][0][:10]-H22[0][0][:10]


Out[238]:
array([-0.02621288, -0.41299691,  0.01349065,  0.17890247, -0.28438289,
        0.00558295, -0.06101805, -0.04258914,  0.05178099,  0.20801063])

In [239]:
H1[5][:10]-H2[5][:10]


Out[239]:
array([-0.01842767, -0.46913334,  0.01314212,  0.14688788, -0.30514536,
       -0.01535442, -0.07694036, -0.05140045,  0.075449  ,  0.24241088])

In [243]:
H11[0][0][1], H22[0][0][1]


Out[243]:
(-0.79272263407823362, -0.37972572291801598)

In [242]:
decoder_f = K.function(decoder.inputs, [decoder.output])


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-242-65745f86e367> in <module>()
----> 1 decoder_f = K.function(decoder.inputs, [decoder.output])

AttributeError: 'LSTM' object has no attribute 'inputs'

In [173]:
H = decoder_f(X)
H[0].shape
np.argmax(H[0][0,0,:]), np.max(H[0][0,0,:])


Out[173]:
(121, 0.75900405282036232)

In [272]:
print(chars)
print([ np.inner(H[0][0,0,:], W[:,idx]) for idx in range(12) ])
preds = model.predict_classes(X, verbose=0)
ctable.decode(preds[0], calc_argmax=False)


0123456789+ 
[-4.3513234791601132, -4.9431112475557448, -4.6511908840946461, -8.9641301699517157, -4.4348598222546585, -1.963126190168381, 2.9753856653092701, 8.7274458271487507, 11.555584048872351, 7.987633832776071, 2.0469878540829969, -3.7234760182555617]
Out[272]:
'66 '

In [99]:
H[0][0,0,:]


Out[99]:
array([-0.06709065,  0.40383556,  0.16411367, -0.74047542, -0.35855955,
       -0.59028941, -0.26894078, -0.66406655, -0.37315661,  0.09656011,
       -0.22731853, -0.56127489, -0.49666747,  0.3873997 ,  0.32721272,
        0.74144036,  0.75600386,  0.50949365,  0.44447297, -0.56334132,
        0.67948729,  0.22404099, -0.18213749,  0.27260011, -0.35370538,
       -0.6508016 ,  0.5779109 , -0.21924061,  0.05315667, -0.39903998,
        0.39056793, -0.        ,  0.15501858, -0.62290728, -0.67472887,
        0.21198085,  0.12123933,  0.52974063, -0.65263051,  0.6788249 ,
        0.47473016, -0.53419101, -0.0291235 ,  0.74110025,  0.        ,
        0.75623643, -0.64309251,  0.39502251,  0.61232328, -0.62432057,
       -0.11690293, -0.01695574,  0.50563914, -0.21612525, -0.32952598,
        0.5745647 ,  0.18968037,  0.7037279 , -0.69483364, -0.62477803,
       -0.57743329, -0.42290473,  0.61531985,  0.26257876, -0.5781849 ,
        0.68098319, -0.73864007, -0.27567244, -0.7042762 ,  0.72349125,
        0.61089551, -0.03887191, -0.74912137, -0.67729241, -0.7189979 ,
        0.45572764, -0.47757691, -0.43260944,  0.63883197,  0.00961242,
       -0.35464063,  0.32595715, -0.75157613,  0.07013302, -0.1147771 ,
       -0.01545928, -0.06468385,  0.48737547, -0.49748945, -0.71463346,
       -0.75395423,  0.29033199,  0.58166301, -0.40639684,  0.47529167,
       -0.6166876 , -0.35756159,  0.29596832, -0.68779182,  0.08947203,
       -0.42900568,  0.66031361, -0.39926156,  0.45977357, -0.60209894,
        0.18464854, -0.19791   ,  0.75690162, -0.5247376 , -0.50292712,
       -0.24802937,  0.35192591,  0.66844153,  0.41236135,  0.72213209,
        0.03156203, -0.42111576,  0.30703384,  0.38017774,  0.75751692,
       -0.26518655, -0.13845894, -0.74450171,  0.29961479,  0.47395283,
       -0.30834025, -0.02306511, -0.30869997], dtype=float32)

In [100]:
H[0][0,:2,:]


Out[100]:
array([[-0.06709065,  0.40383556,  0.16411367, -0.74047542, -0.35855955,
        -0.59028941, -0.26894078, -0.66406655, -0.37315661,  0.09656011,
        -0.22731853, -0.56127489, -0.49666747,  0.3873997 ,  0.32721272,
         0.74144036,  0.75600386,  0.50949365,  0.44447297, -0.56334132,
         0.67948729,  0.22404099, -0.18213749,  0.27260011, -0.35370538,
        -0.6508016 ,  0.5779109 , -0.21924061,  0.05315667, -0.39903998,
         0.39056793, -0.        ,  0.15501858, -0.62290728, -0.67472887,
         0.21198085,  0.12123933,  0.52974063, -0.65263051,  0.6788249 ,
         0.47473016, -0.53419101, -0.0291235 ,  0.74110025,  0.        ,
         0.75623643, -0.64309251,  0.39502251,  0.61232328, -0.62432057,
        -0.11690293, -0.01695574,  0.50563914, -0.21612525, -0.32952598,
         0.5745647 ,  0.18968037,  0.7037279 , -0.69483364, -0.62477803,
        -0.57743329, -0.42290473,  0.61531985,  0.26257876, -0.5781849 ,
         0.68098319, -0.73864007, -0.27567244, -0.7042762 ,  0.72349125,
         0.61089551, -0.03887191, -0.74912137, -0.67729241, -0.7189979 ,
         0.45572764, -0.47757691, -0.43260944,  0.63883197,  0.00961242,
        -0.35464063,  0.32595715, -0.75157613,  0.07013302, -0.1147771 ,
        -0.01545928, -0.06468385,  0.48737547, -0.49748945, -0.71463346,
        -0.75395423,  0.29033199,  0.58166301, -0.40639684,  0.47529167,
        -0.6166876 , -0.35756159,  0.29596832, -0.68779182,  0.08947203,
        -0.42900568,  0.66031361, -0.39926156,  0.45977357, -0.60209894,
         0.18464854, -0.19791   ,  0.75690162, -0.5247376 , -0.50292712,
        -0.24802937,  0.35192591,  0.66844153,  0.41236135,  0.72213209,
         0.03156203, -0.42111576,  0.30703384,  0.38017774,  0.75751692,
        -0.26518655, -0.13845894, -0.74450171,  0.29961479,  0.47395283,
        -0.30834025, -0.02306511, -0.30869997],
       [-0.69994533,  0.76321411,  0.15535766, -0.65344214, -0.88647878,
         0.25560793, -0.81919712, -0.45533341,  0.27077356,  0.29419547,
        -0.02144295, -0.84962237, -0.91721439,  0.44998246,  0.        ,
         0.        ,  0.82862532,  0.64688653,  0.73954397, -0.91859907,
         0.87968051,  0.29914984, -0.07507572,  0.77487564, -0.62631953,
        -0.81438786, -0.02533447, -0.08969957,  0.77608752, -0.73654878,
         0.2907638 , -0.67405748,  0.44373825, -0.88843524,  0.03123279,
        -0.52790016, -0.6611138 ,  0.80778039, -0.75994819,  0.91814965,
         0.76106578, -0.51561689,  0.15538892,  0.93557227, -0.51685983,
         0.90239698, -0.94287002,  0.36629105,  0.53293186, -0.94712871,
         0.73345929,  0.0893545 ,  0.26360336, -0.76727957,  0.3423062 ,
         0.1201067 ,  0.798612  ,  0.45061913, -0.41951275, -0.53485352,
         0.36591163,  0.05451221,  0.93636763, -0.65462619, -0.68913627,
         0.66622269, -0.95830816, -0.65435702, -0.10083887,  0.89494902,
         0.93562615,  0.6598193 , -0.92536646,  0.11554859, -0.23388234,
         0.67674857, -0.41253474, -0.70742184,  0.87375456, -0.4631013 ,
        -0.08554054,  0.00632936, -0.88636178,  0.44608563, -0.34360796,
         0.67244905,  0.74465764,  0.41988492, -0.80310714, -0.96309108,
        -0.70842063,  0.17630763,  0.49206769, -0.77916503,  0.60389054,
        -0.93163276, -0.711384  , -0.08215633, -0.35217124,  0.554766  ,
        -0.42379117,  0.9456225 , -0.55936402,  0.86239368, -0.86113065,
        -0.20461668, -0.21869616,  0.91854304, -0.25319299, -0.2395523 ,
         0.59244412,  0.80410171,  0.76140451,  0.86761189,  0.78436691,
        -0.18879384, -0.34042352,  0.31742704,  0.04000176,  0.85967487,
        -0.42730716, -0.43447372, -0.96116805,  0.83471322,  0.94954866,
        -0.10928034, -0.7506845 , -0.69407344]], dtype=float32)

In [101]:
import matplotlib.pyplot as plt
import pylab as pl
import matplotlib.cm as cm
%matplotlib inline

pl.imshow(np.transpose(H[0][0,:3,:]), interpolation='nearest', cmap=cm.binary, aspect='auto')
plt.show()



In [102]:
pl.imshow(W, interpolation='nearest', cmap=cm.binary, aspect='auto')
plt.show()



In [103]:
(np.argmax(H[0][0,0,:] * W[:,4]), np.max(H[0][0,0,:] * W[:,4]))


Out[103]:
(15, 0.56187469)

In [104]:
(np.argmax(H[0][0,1,:] * W[:,4]), np.max(H[0][0,1,:] * W[:,4]))


Out[104]:
(101, 0.62769294)

In [105]:
H[0][0,:3,34]


Out[105]:
array([-0.67472887,  0.03123279,  0.        ], dtype=float32)

In [106]:
W[34,:]


Out[106]:
array([-0.1932454 , -0.17046466, -0.33151487, -0.28610313, -0.10930867,
       -0.22562845, -0.23864335, -0.23633261,  0.64464188,  0.514768  ,
        0.32287183,  0.23024265], dtype=float32)

In [107]:
(np.argmax(H[0][0,2,:] * W[:,4]), np.max(H[0][0,2,:] * W[:,4]))


Out[107]:
(101, 0.65876389)

In [108]:
(np.argmin(H[0][0,2,:] * W[:,4]), np.min(H[0][0,2,:] * W[:,4]))


Out[108]:
(24, -0.45415828)

In [109]:
(np.argmin(H[0][0,1,:] * W[:,4]), np.min(H[0][0,1,:] * W[:,4]))


Out[109]:
(24, -0.42374974)

In [110]:
np.sort(H[0][0,2,:]*W[:,4])


Out[110]:
array([-0.45415828, -0.32855698, -0.29704189, -0.2528812 , -0.24726333,
       -0.22769974, -0.22538853, -0.22083418, -0.20432733, -0.19023129,
       -0.17320378, -0.1695088 , -0.16207343, -0.15740235, -0.14236188,
       -0.14213942, -0.13477033, -0.12927851, -0.12831168, -0.11970972,
       -0.10363259, -0.1032828 , -0.09262865, -0.08448901, -0.07727309,
       -0.07494878, -0.0717946 , -0.06999763, -0.06153421, -0.05854693,
       -0.04606926, -0.04022109, -0.03889963, -0.03877258, -0.03622344,
       -0.03408409, -0.03324113, -0.0290855 , -0.02835351, -0.02560154,
       -0.02457534, -0.02231443, -0.01837168, -0.01154075, -0.00861657,
       -0.00798268, -0.00578552, -0.00291207, -0.00072087, -0.        ,
       -0.        ,  0.        ,  0.        ,  0.        , -0.        ,
        0.        ,  0.0006604 ,  0.00235866,  0.00646312,  0.01712832,
        0.02227856,  0.02414711,  0.02534644,  0.02723772,  0.02817344,
        0.02962844,  0.04378298,  0.04479284,  0.04778089,  0.05259071,
        0.05498044,  0.06114025,  0.06737013,  0.07038768,  0.07419734,
        0.07490229,  0.07688582,  0.07917839,  0.08645156,  0.08703602,
        0.08862983,  0.09577078,  0.0999976 ,  0.10052644,  0.12584311,
        0.12872617,  0.13169807,  0.13340101,  0.13913572,  0.15334426,
        0.15537268,  0.15956056,  0.16541971,  0.16779615,  0.17974967,
        0.1923687 ,  0.19505765,  0.2036147 ,  0.21206295,  0.22455968,
        0.22504662,  0.23316552,  0.24413237,  0.25332928,  0.2596457 ,
        0.28483191,  0.29851249,  0.30852795,  0.31414506,  0.31818533,
        0.34294248,  0.34718034,  0.34857944,  0.35602358,  0.37379876,
        0.37608159,  0.38508138,  0.38974813,  0.39278048,  0.43129802,
        0.44780952,  0.46707478,  0.48255435,  0.49487916,  0.56500721,
        0.57615417,  0.61665791,  0.65876389], dtype=float32)

In [111]:
np.sort(H[0][0,2,:]*W[:,0])


Out[111]:
array([ -3.07066858e-01,  -2.36525223e-01,  -1.86790660e-01,
        -1.79153278e-01,  -9.95083377e-02,  -8.52999687e-02,
        -7.92786181e-02,  -5.84232956e-02,  -5.36007360e-02,
        -3.76082137e-02,  -3.74200717e-02,  -2.97356788e-02,
        -2.87593957e-02,  -2.67301910e-02,  -2.66843513e-02,
        -1.54926321e-02,  -1.34472745e-02,  -1.23247402e-02,
        -1.15500055e-02,  -1.06033040e-02,  -9.94192995e-03,
        -9.92942695e-03,  -5.97132975e-03,  -5.30619640e-03,
        -2.57752021e-03,  -1.43820746e-03,  -0.00000000e+00,
        -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         0.00000000e+00,  -0.00000000e+00,   0.00000000e+00,
         3.32088035e-04,   1.00557832e-03,   1.51619432e-03,
         4.01117187e-03,   5.09049511e-03,   6.57067867e-03,
         8.09591822e-03,   8.31112824e-03,   1.21207628e-02,
         1.25999600e-02,   1.91498622e-02,   2.13483125e-02,
         2.26804074e-02,   2.47206800e-02,   2.87241414e-02,
         3.08485162e-02,   3.17067653e-02,   3.38971913e-02,
         3.80584821e-02,   4.31777164e-02,   4.38277684e-02,
         4.49320376e-02,   4.56416681e-02,   4.57284153e-02,
         4.57441099e-02,   4.62947823e-02,   4.70370874e-02,
         4.82262224e-02,   4.92709652e-02,   5.43154478e-02,
         5.52334860e-02,   5.61399423e-02,   6.85058013e-02,
         6.92265183e-02,   7.22152144e-02,   7.59950355e-02,
         8.21665227e-02,   9.25646722e-02,   1.00123212e-01,
         1.06100351e-01,   1.14388764e-01,   1.14692368e-01,
         1.15460992e-01,   1.16938144e-01,   1.19890802e-01,
         1.21548578e-01,   1.31272495e-01,   1.31615043e-01,
         1.34969592e-01,   1.35133177e-01,   1.36581033e-01,
         1.36625081e-01,   1.43077791e-01,   1.50115177e-01,
         1.53605819e-01,   1.54143378e-01,   1.57359287e-01,
         1.62541181e-01,   1.65562198e-01,   1.81113452e-01,
         1.89826876e-01,   2.07039639e-01,   2.11493328e-01,
         2.27072164e-01,   2.29275346e-01,   2.31581777e-01,
         2.50185251e-01,   2.56177783e-01,   2.58738548e-01,
         2.61075437e-01,   2.69964963e-01,   2.90376782e-01,
         2.93340057e-01,   3.02351445e-01,   3.05366158e-01,
         3.07923168e-01,   3.09660345e-01,   3.12246263e-01,
         3.22186142e-01,   3.26824278e-01,   3.34542394e-01,
         3.48860413e-01,   3.52766722e-01,   3.59418362e-01,
         3.67470324e-01,   3.87315333e-01,   3.89949232e-01,
         4.02509570e-01,   4.03534800e-01,   4.04645622e-01,
         4.28490102e-01,   4.46417272e-01,   4.53108191e-01,
         4.60138053e-01,   5.74842691e-01], dtype=float32)

In [112]:
np.sort(H[0][0,1,:]*W[:,0])


Out[112]:
array([-0.5335024 , -0.51823604, -0.50100958, -0.48577577, -0.47957054,
       -0.44241169, -0.37946013, -0.36722139, -0.35580841, -0.33394715,
       -0.23222774, -0.22796875, -0.2009044 , -0.17646785, -0.17416285,
       -0.17232595, -0.16369027, -0.16364257, -0.10973285, -0.10573573,
       -0.1032261 , -0.08143459, -0.0812537 , -0.07862101, -0.07631801,
       -0.07576364, -0.06466574, -0.06453831, -0.06009501, -0.05024419,
       -0.04648986, -0.04348138, -0.04183068, -0.03520668, -0.03504439,
       -0.02873553, -0.02783925, -0.02733363, -0.02595378, -0.01380991,
       -0.01290411, -0.01269832, -0.01256789, -0.00633566, -0.00606702,
       -0.00603559, -0.00495656, -0.00493447, -0.0049248 , -0.00277691,
        0.        , -0.        ,  0.00060533,  0.00067053,  0.00182039,
        0.00437887,  0.00475972,  0.01304994,  0.01382295,  0.01396575,
        0.01528975,  0.01548589,  0.01594696,  0.0162168 ,  0.01740837,
        0.01888322,  0.01985673,  0.02293642,  0.02334398,  0.029086  ,
        0.029867  ,  0.03536496,  0.03579383,  0.03616775,  0.03943265,
        0.0406722 ,  0.04438293,  0.04600561,  0.04622176,  0.04791395,
        0.04925878,  0.04982607,  0.05837914,  0.06185332,  0.06425659,
        0.06440832,  0.06462984,  0.06534637,  0.07266825,  0.07418796,
        0.07787652,  0.0859278 ,  0.086803  ,  0.09313124,  0.09348384,
        0.09621865,  0.10355424,  0.10773021,  0.11408503,  0.11869032,
        0.12508096,  0.12934865,  0.13050248,  0.13083667,  0.13169053,
        0.14707059,  0.15002634,  0.15020089,  0.15054029,  0.1561247 ,
        0.16261391,  0.16920441,  0.16961174,  0.17333853,  0.18566015,
        0.18759561,  0.19882663,  0.20133199,  0.21040641,  0.21242753,
        0.22286218,  0.23713812,  0.23777719,  0.24145626,  0.2661728 ,
        0.27632549,  0.28099507,  0.39949182], dtype=float32)

In [113]:
np.sort(H[0][0,2,:]*W[:,4])


Out[113]:
array([-0.45415828, -0.32855698, -0.29704189, -0.2528812 , -0.24726333,
       -0.22769974, -0.22538853, -0.22083418, -0.20432733, -0.19023129,
       -0.17320378, -0.1695088 , -0.16207343, -0.15740235, -0.14236188,
       -0.14213942, -0.13477033, -0.12927851, -0.12831168, -0.11970972,
       -0.10363259, -0.1032828 , -0.09262865, -0.08448901, -0.07727309,
       -0.07494878, -0.0717946 , -0.06999763, -0.06153421, -0.05854693,
       -0.04606926, -0.04022109, -0.03889963, -0.03877258, -0.03622344,
       -0.03408409, -0.03324113, -0.0290855 , -0.02835351, -0.02560154,
       -0.02457534, -0.02231443, -0.01837168, -0.01154075, -0.00861657,
       -0.00798268, -0.00578552, -0.00291207, -0.00072087, -0.        ,
       -0.        ,  0.        ,  0.        ,  0.        , -0.        ,
        0.        ,  0.0006604 ,  0.00235866,  0.00646312,  0.01712832,
        0.02227856,  0.02414711,  0.02534644,  0.02723772,  0.02817344,
        0.02962844,  0.04378298,  0.04479284,  0.04778089,  0.05259071,
        0.05498044,  0.06114025,  0.06737013,  0.07038768,  0.07419734,
        0.07490229,  0.07688582,  0.07917839,  0.08645156,  0.08703602,
        0.08862983,  0.09577078,  0.0999976 ,  0.10052644,  0.12584311,
        0.12872617,  0.13169807,  0.13340101,  0.13913572,  0.15334426,
        0.15537268,  0.15956056,  0.16541971,  0.16779615,  0.17974967,
        0.1923687 ,  0.19505765,  0.2036147 ,  0.21206295,  0.22455968,
        0.22504662,  0.23316552,  0.24413237,  0.25332928,  0.2596457 ,
        0.28483191,  0.29851249,  0.30852795,  0.31414506,  0.31818533,
        0.34294248,  0.34718034,  0.34857944,  0.35602358,  0.37379876,
        0.37608159,  0.38508138,  0.38974813,  0.39278048,  0.43129802,
        0.44780952,  0.46707478,  0.48255435,  0.49487916,  0.56500721,
        0.57615417,  0.61665791,  0.65876389], dtype=float32)

In [273]:
np.argsort(H[0][0,0,:]*W[:,8])


Out[273]:
array([ 65,  15, 121,  56, 110, 123,  44,  57,  22,  63, 102,  18,  28,
        67, 120,  27, 113,  42, 109,  66, 125,  92, 101,  91,  95,  31,
        45, 103,  87,  83, 127,  26,  94,  11,  99, 117, 106,  82,  76,
        54, 116,  61,  20,  46, 108,  36,  55,   0,  14,  21, 104,  93,
        19,  86, 114,  84,  70,  40, 105, 107,  62, 124,  53,  51,  47,
        29,  72,  50,  74, 115,   7,  35,   2,  81,   9,  23,  80, 122,
        97,  34,  43, 100,  85,  68,  96,  59,  48,   8,  41,  69,  38,
        77, 112, 111,  90,  12,   3,  64, 126,   5,  60,  32,   1,  79,
         6,  24, 118,  25,  13,  17,   4,  89,  16,  71,  52,  88,  75,
        10,  49,  58,  30,  37,  98,  33, 119,  39,  73,  78])

In [275]:
np.argsort(H[0][0,1,:]*W[:,8])


Out[275]:
array([ 65,  63, 101,  22,  42,  15,  25,  56,   6,  57, 123,  69,  28,
        39, 125,   1,  44,  92,  19,  26,  66,  45, 114,  83, 106,  91,
        87, 120,  78,  12,  54,  79, 103,  61,  11,  70, 102,  99,  88,
        30, 126,  98,  51,  76,  82,  67,  97,  94, 108,  20, 116,  46,
        93,  55, 127,  27,  21,  36, 105,  84,  40, 118,  62,  72, 109,
        74,  86, 107,  48, 124, 110, 115,  53,  35,   7,   5,  81,   3,
         8,  37, 121, 122, 117,  80,  43,  68,  23,  47,  34, 104, 100,
        29,  18,   9,  60,  50,  77,   0,  59,  41,  38,  32,  90, 111,
        85,  95,  96,   4,  89,  64,   2,  75, 113,  31,  24, 112,  16,
        14,  17,  13,  52,  10,  33,  58,  49, 119,  71,  73])

In [278]:
np.argsort(H[0][0,1,:]*W[:,8])


Out[278]:
array([ 73,  39,  30,  98,  17, 123,   4, 112,   6,  24,  65, 125, 106,
       110,  45,  14, 121,  15, 119,   5,  89, 126,  63,  35,  91,  56,
        67,  25, 120,  13,  49,  66,  88, 102,  32,  18,  23, 111,  44,
        57,   9, 109,  53,  27,  19,  38, 115,  28,  86,  74,  40,  36,
       122,  59,  43,  83,  93, 127,  87, 103,  46, 108,  81,  21,  54,
        94,  33,  99,  82,  11,  85,  76,  26,  20,  61,  55, 116,  72,
       105,  84,  92, 117,  51, 101,  69,  62,  47,  70, 124,  12,  31,
       107,  10,  22,  42, 114,  97,   7,  34,   1, 100,  41,  71,  52,
         8,  58,   3,  60,  95,  80,  48,  50,   0,  29,  37,  68,   2,
        79,  75, 104,  77, 113, 118,  96,  16,  64,  90,  78])

In [117]:
dweights = decoder.get_weights()

In [118]:
len(dweights)


Out[118]:
12

In [122]:
dweights[3].shape


Out[122]:
(128, 128)

In [129]:
context = encoder_f(X)
context = context[0][0]

In [130]:


In [131]:
context


Out[131]:
array([-0.71680015, -0.71960968, -0.67189914,  0.35741776, -0.74951118,
       -0.35821265,  0.11314015, -0.44468644, -0.32356325,  0.47177044,
        0.8712455 , -0.49983853,  0.2256813 ,  0.30286032, -0.7024222 ,
       -0.3009218 , -0.2085263 ,  0.48517787, -0.50062698, -0.63945681,
        0.41134304, -0.12710692,  0.35288697, -0.32511729,  0.7333858 ,
        0.63669533, -0.79113978, -0.47415727, -0.70502675, -0.45729491,
        0.94472587, -0.69170535, -0.71485847,  0.53148812, -0.42479914,
        0.79443866, -0.76739752, -0.45804244,  0.63051635, -0.32278904,
       -0.18736556, -0.5895865 , -0.07616945, -0.70557344,  0.09437619,
        0.4516294 , -0.59635127, -0.35728732, -0.60755306, -0.51159179,
        0.83871293, -0.85435426, -0.62628043,  0.24694353,  0.37690449,
       -0.56536186, -0.29809138, -0.42888081, -0.69122845, -0.32909194,
       -0.3422389 ,  0.23144025,  0.79829955, -0.03181538, -0.54385889,
        0.8051672 ,  0.22158965,  0.40830263,  0.48554987, -0.18048501,
        0.25863943,  0.41948989,  0.60638374,  0.63353252,  0.02183209,
        0.48267326, -0.83726984, -0.99353832,  0.05776672,  0.81352484,
       -0.48400256,  0.0042755 ,  0.96776056, -0.48480931,  0.70168614,
        0.70132828, -0.40280709,  0.51580983, -0.5750528 , -0.38270435,
       -0.0639038 , -0.61799997, -0.63614011,  0.81532007, -0.68753004,
        0.84851801, -0.53855199, -0.48810041,  0.82378352, -0.72564769,
       -0.7464807 , -0.83057159,  0.31444475,  0.91995108, -0.52658582,
       -0.60736173,  0.22396296, -0.22781688, -0.68356621,  0.97621125,
       -0.81962818,  0.78978717,  0.45830211,  0.52139574, -0.02888704,
        0.64774376,  0.40182143,  0.80477822,  0.65618575, -0.49759692,
       -0.11176096, -0.27976739, -0.6482625 , -0.33725443, -0.75477457,
        0.64396036, -0.4464213 ,  0.4779253 ], dtype=float32)

In [137]:
dweights[2].shape


Out[137]:
(128,)

In [148]:
questions


Out[148]:
['  1+0',
 ' 09+1',
 '  9+0',
 ' 27+8',
 '19+34',
 '35+92',
 '  4+1',
 ' 75+6',
 '  8+4',
 '  8+0',
 '  6+5',
 '  6+2',
 ' 55+6',
 '  5+3',
 '53+92',
 '  3+2',
 ' 38+1',
 ' 54+2',
 '38+93',
 ' 97+6',
 ' 47+1',
 '  7+3',
 '  5+4',
 ' 06+9',
 ' 67+8',
 ' 92+5',
 '  6+4',
 ' 22+3',
 '57+36',
 '89+03',
 '67+05',
 '57+45',
 '94+43',
 ' 52+9',
 ' 22+5',
 ' 81+2',
 '66+42',
 ' 81+6',
 '69+03',
 '  2+1',
 '78+51',
 ' 65+1',
 ' 94+8',
 '09+57',
 '  5+2',
 ' 57+2',
 ' 53+3',
 ' 74+9',
 ' 05+0',
 '69+12',
 ' 05+9',
 '98+31',
 ' 47+6',
 '86+25',
 '46+82',
 ' 26+0',
 ' 68+6',
 ' 01+5',
 '96+62',
 '  7+5',
 ' 76+7',
 ' 72+5',
 '39+12',
 '  9+8',
 '16+41',
 ' 35+0',
 '08+97',
 ' 84+7',
 ' 23+1',
 ' 59+2',
 ' 11+1',
 ' 21+0',
 '27+46',
 '  6+0',
 '35+44',
 ' 81+8',
 ' 95+4',
 ' 27+5',
 '47+05',
 '96+54',
 ' 72+7',
 '39+27',
 '  7+2',
 ' 58+7',
 ' 26+2',
 ' 64+7',
 '35+52',
 '  7+4',
 ' 77+1',
 '05+91',
 '76+43',
 ' 66+7',
 '36+93',
 ' 69+4',
 '45+53',
 ' 91+8',
 '  9+4',
 '49+67',
 '81+01',
 ' 08+2',
 ' 87+1',
 ' 83+0',
 '  9+6',
 '48+96',
 ' 13+5',
 ' 68+3',
 ' 78+4',
 '19+03',
 '53+51',
 ' 54+6',
 ' 49+6',
 '44+82',
 '  9+1',
 ' 19+0',
 ' 83+8',
 ' 12+4',
 '  7+6',
 '92+42',
 '18+82',
 ' 95+6',
 ' 66+9',
 '97+36',
 ' 61+2',
 ' 15+7',
 ' 23+7',
 '  9+9',
 ' 28+2',
 ' 84+5',
 ' 31+4',
 '38+45',
 ' 79+1',
 ' 39+8',
 ' 65+6',
 ' 59+9',
 ' 03+5',
 ' 65+2',
 '  2+0',
 ' 28+6',
 ' 75+2',
 ' 74+6',
 ' 31+0',
 '58+51',
 '  8+2',
 ' 67+5',
 '68+44',
 ' 92+6',
 '76+36',
 '  9+2',
 ' 71+6',
 '  5+0',
 ' 81+1',
 ' 04+4',
 ' 15+3',
 '  6+6',
 '  9+5',
 ' 68+2',
 ' 71+2',
 ' 99+9',
 ' 12+5',
 ' 27+9',
 '  4+2',
 ' 46+5',
 ' 16+5',
 ' 41+6',
 ' 35+7',
 '28+17',
 ' 65+7',
 ' 72+9',
 ' 69+0',
 ' 62+9',
 '47+53',
 ' 04+1',
 '  8+7',
 '09+55',
 '  6+1',
 ' 28+9',
 ' 25+1',
 '86+66',
 ' 21+2',
 ' 97+7',
 ' 99+6',
 ' 63+8',
 ' 68+4',
 '39+53',
 ' 94+6',
 '66+22',
 ' 38+5',
 '28+04',
 '  3+3',
 ' 18+8',
 '56+05',
 '  4+4',
 '55+22',
 ' 83+4',
 ' 35+9',
 ' 97+8',
 ' 52+4',
 '  9+3',
 '99+71',
 ' 34+3',
 ' 91+0',
 ' 59+8',
 '85+74',
 ' 99+1',
 ' 09+3',
 '18+53',
 '47+01',
 ' 58+5',
 ' 11+6',
 '  1+1',
 '27+04',
 ' 08+3',
 ' 09+6',
 '29+36',
 '89+72',
 '76+71',
 ' 07+1',
 ' 63+5',
 '  8+3',
 ' 41+0',
 ' 36+5',
 '69+98',
 '17+65',
 '48+71',
 ' 46+1',
 ' 28+7',
 ' 05+4',
 ' 61+8',
 ' 38+4',
 ' 95+7',
 ' 55+1',
 ' 86+7',
 ' 94+7',
 '88+97',
 ' 64+4',
 ' 96+9',
 '63+12',
 ' 73+3',
 '68+16',
 ' 21+4',
 ' 56+1',
 ' 88+0',
 '  2+2',
 '56+46',
 ' 29+9',
 ' 16+3',
 '  4+0',
 ' 44+5',
 ' 51+5',
 ' 15+8',
 ' 13+3',
 ' 56+5',
 '99+08',
 ' 56+0',
 '97+72',
 ' 29+6',
 '96+92',
 ' 13+1',
 ' 13+0',
 ' 26+1',
 ' 51+2',
 '17+62',
 ' 09+9',
 ' 35+1',
 ' 19+4',
 ' 31+6',
 ' 41+7',
 '  3+0',
 '73+22',
 ' 19+3',
 ' 98+5',
 '38+72',
 ' 63+3',
 ' 01+6',
 ' 86+2',
 ' 64+3',
 ' 37+3',
 '  7+0',
 ' 91+4',
 '  7+1',
 ' 87+0',
 '  5+5',
 ' 86+3',
 ' 41+3',
 ' 19+7',
 ' 44+7',
 ' 06+0',
 '55+52',
 '  9+7',
 ' 81+0',
 '  3+1',
 ' 46+8',
 ' 76+2',
 ' 74+2',
 '45+81',
 ' 49+2',
 '56+74',
 ' 92+9',
 ' 05+2',
 ' 12+3',
 ' 61+1',
 ' 05+1',
 '74+12',
 '64+22',
 '28+25',
 ' 94+1',
 '  7+7',
 '75+74',
 '59+67',
 '  4+3',
 ' 67+6',
 '27+83',
 ' 99+7',
 ' 71+9',
 '16+72',
 ' 32+1',
 '59+52',
 '89+48',
 ' 17+5',
 ' 07+3',
 ' 45+1',
 ' 86+5',
 '25+92',
 ' 88+9',
 '  8+1',
 '08+42',
 '37+52',
 ' 98+0',
 ' 83+9',
 ' 23+0',
 ' 55+8',
 ' 37+9',
 '  8+5',
 ' 54+7',
 ' 98+2',
 ' 01+8',
 '28+95',
 ' 53+2',
 '78+82',
 ' 89+3',
 ' 94+0',
 '17+75',
 ' 12+0',
 '  8+6',
 ' 62+6',
 '56+34',
 ' 21+9',
 '07+81',
 ' 83+1',
 '78+03',
 '  6+3',
 '79+51',
 ' 73+0',
 '09+06',
 '28+33',
 '39+88',
 ' 68+1',
 ' 55+5',
 ' 26+3',
 ' 48+6',
 ' 79+9',
 ' 02+4',
 ' 74+5',
 ' 58+6',
 ' 33+0',
 ' 88+7',
 '  0+0',
 ' 19+1',
 '38+52',
 ' 32+5',
 '49+46',
 '29+71',
 ' 15+4',
 ' 87+4',
 '  5+1',
 ' 62+7',
 ' 89+2',
 ' 12+2',
 ' 99+5',
 ' 02+8',
 ' 11+7',
 ' 16+9',
 ' 54+4',
 ' 73+7',
 ' 46+0',
 ' 18+0',
 ' 57+7',
 ' 22+7',
 ' 76+0',
 '98+32',
 '07+85',
 ' 78+5',
 '29+46',
 ' 11+3',
 ' 72+8',
 '85+01',
 '87+14',
 ' 69+5',
 '73+53',
 ' 83+6',
 '74+62',
 ' 85+8',
 ' 38+9',
 ' 13+9',
 ' 72+1',
 ' 22+2',
 '77+32',
 '42+51',
 ' 28+3',
 ' 84+6',
 '53+41',
 '96+13',
 ' 04+5',
 '21+21',
 ' 77+7',
 '95+53',
 ' 43+8',
 '48+43',
 '78+31',
 ' 13+2',
 '72+51',
 '07+92',
 ' 45+5',
 '94+82',
 '69+22',
 ' 11+5',
 ' 36+9',
 '34+14',
 ' 03+7',
 ' 78+2',
 ' 69+3',
 ' 66+0',
 ' 65+0',
 ' 44+1',
 ' 49+7',
 '69+75',
 ' 34+4',
 ' 05+8',
 '68+76',
 '37+03',
 ' 33+2',
 ' 95+9',
 ' 98+6',
 '47+92',
 '88+66',
 '97+44',
 ' 54+0',
 '87+92',
 ' 48+0',
 '73+02',
 '14+02',
 ' 89+5',
 ' 07+7',
 '63+61',
 ' 26+5',
 ' 88+5',
 ' 09+0',
 ' 13+8',
 '73+13',
 '79+61',
 ' 93+6',
 '57+63',
 ' 22+8',
 ' 82+6',
 ' 26+7',
 ' 57+8',
 ' 44+8',
 '39+28',
 '05+94',
 '87+46',
 ' 98+8',
 ' 61+0',
 '78+71',
 '69+47',
 ' 53+7',
 ' 58+8',
 ' 22+0',
 ' 45+0',
 ' 34+5',
 '12+51',
 ' 28+1',
 '34+51',
 '69+77',
 ' 85+3',
 ' 89+1',
 ' 45+3',
 ' 17+9',
 '79+66',
 ' 71+5',
 ' 27+4',
 ' 95+3',
 '35+91',
 ' 07+8',
 ' 87+5',
 '67+96',
 ' 86+8',
 '68+13',
 ' 29+5',
 ' 75+1',
 ' 76+3',
 '15+44',
 '67+14',
 '16+52',
 ' 86+4',
 ' 68+5',
 '59+44',
 '78+83',
 ' 84+2',
 ' 25+4',
 '85+33',
 ' 47+9',
 '39+77',
 '88+25',
 ' 27+3',
 '48+06',
 ' 88+8',
 '68+75',
 ' 96+0',
 ' 61+3',
 ' 18+1',
 '34+02',
 '43+71',
 ' 16+7',
 ' 33+7',
 ' 57+0',
 ' 52+1',
 '63+72',
 ' 08+4',
 ' 66+5',
 ' 14+6',
 ' 41+5',
 ' 19+5',
 ' 59+1',
 '74+71',
 '73+21',
 ' 85+0',
 '17+92',
 ' 67+1',
 '85+05',
 ' 78+6',
 ' 91+5',
 ' 56+3',
 ' 46+9',
 ' 73+9',
 '34+33',
 '27+65',
 '56+15',
 ' 31+9',
 '19+47',
 ' 45+9',
 ' 05+5',
 ' 58+4',
 ' 91+6',
 ' 11+2',
 ' 19+6',
 ' 39+1',
 ' 39+7',
 ' 07+4',
 ' 62+2',
 '55+14',
 '15+33',
 ' 48+3',
 ' 57+6',
 ' 47+4',
 ' 38+7',
 ' 17+7',
 ' 61+6',
 ' 35+6',
 '79+94',
 ' 14+7',
 '76+44',
 ' 15+1',
 '97+05',
 ' 29+7',
 ' 23+4',
 '88+67',
 ' 62+8',
 '83+63',
 ' 93+3',
 '25+34',
 ' 02+1',
 '78+87',
 '69+66',
 '26+31',
 ' 68+0',
 ' 95+1',
 ' 94+5',
 ' 41+4',
 '53+03',
 ' 36+0',
 '49+18',
 ' 61+5',
 '04+02',
 ' 92+3',
 ' 03+3',
 ' 32+2',
 '69+57',
 '16+85',
 ' 41+8',
 ' 93+4',
 '79+91',
 ' 21+1',
 ' 78+1',
 ' 53+1',
 '76+04',
 '72+01',
 '49+64',
 '67+52',
 ' 02+5',
 ' 14+3',
 ' 87+6',
 ' 42+9',
 '79+35',
 '87+32',
 '24+21',
 '19+98',
 '52+81',
 '48+17',
 '85+63',
 ' 22+4',
 '59+57',
 ' 52+3',
 '86+34',
 '33+62',
 ' 41+2',
 '75+23',
 ' 71+7',
 ' 21+5',
 '17+03',
 ' 63+2',
 '39+13',
 '98+34',
 '18+45',
 ' 53+0',
 ' 56+6',
 '72+02',
 ' 91+2',
 ' 77+4',
 ' 07+6',
 '64+14',
 ' 85+6',
 '35+25',
 ' 49+8',
 ' 43+7',
 '56+53',
 ' 44+0',
 '76+53',
 '28+11',
 '39+04',
 '16+83',
 '29+85',
 '23+92',
 '  8+8',
 '59+96',
 ' 73+2',
 '57+74',
 ' 47+8',
 '97+35',
 ' 22+6',
 ' 17+1',
 ' 24+4',
 '05+74',
 '16+61',
 '77+77',
 '38+41',
 ' 21+6',
 ' 16+8',
 '16+01',
 ' 63+6',
 '23+52',
 ' 76+5',
 ' 34+1',
 '92+61',
 ' 39+2',
 ' 16+1',
 '76+33',
 ' 58+0',
 ' 86+1',
 ' 62+4',
 ' 31+5',
 ' 02+2',
 '82+62',
 '18+16',
 ' 64+2',
 ' 88+3',
 '04+81',
 '29+13',
 ' 21+3',
 '05+92',
 '07+04',
 '79+34',
 ' 04+3',
 ' 14+5',
 ' 42+5',
 '88+85',
 '94+22',
 ' 45+8',
 ' 94+4',
 '87+42',
 ' 68+8',
 ' 29+3',
 ' 04+0',
 '54+32',
 '94+51',
 '88+22',
 '79+05',
 ' 17+4',
 '57+64',
 '36+45',
 ' 98+4',
 ' 81+7',
 '79+42',
 '96+74',
 ' 09+2',
 ' 91+9',
 ' 29+1',
 '36+14',
 '17+16',
 ' 01+1',
 '68+34',
 ' 04+8',
 '55+81',
 ' 39+0',
 ' 96+1',
 ' 54+9',
 '55+61',
 ' 22+9',
 ' 58+2',
 ' 47+7',
 ' 42+1',
 '89+79',
 '13+13',
 ' 71+8',
 ' 81+3',
 '25+74',
 ' 89+8',
 ' 88+2',
 ' 89+9',
 '05+84',
 '35+71',
 '78+57',
 ' 75+9',
 ' 01+3',
 ' 45+4',
 ' 66+6',
 ' 49+9',
 ' 38+3',
 ' 09+5',
 ' 41+1',
 ' 64+5',
 '26+04',
 ' 67+9',
 ' 36+8',
 '79+08',
 ' 65+4',
 '57+83',
 ' 81+4',
 ' 55+3',
 ' 66+8',
 ' 79+0',
 ' 55+2',
 '98+56',
 '05+73',
 ' 43+6',
 '49+23',
 ' 35+3',
 '76+05',
 '36+64',
 ' 24+1',
 ' 34+7',
 ' 55+7',
 ' 69+9',
 ' 44+3',
 ' 12+8',
 ' 96+5',
 ' 23+6',
 ' 63+1',
 ' 65+5',
 ' 85+4',
 ' 62+0',
 '68+58',
 '88+33',
 '78+08',
 '74+31',
 ' 38+8',
 ' 78+3',
 ' 43+4',
 ' 37+8',
 '55+41',
 ' 85+7',
 ' 37+1',
 '97+67',
 ' 73+4',
 ' 37+2',
 ' 75+3',
 ' 79+4',
 '94+42',
 '47+62',
 ' 52+7',
 ' 37+0',
 '06+01',
 ' 25+7',
 ' 14+4',
 '76+26',
 '94+81',
 '17+25',
 ' 13+4',
 ' 87+7',
 '86+76',
 ' 59+4',
 ' 33+6',
 ' 21+7',
 ' 92+1',
 ' 18+2',
 ' 27+7',
 ' 06+3',
 ' 43+9',
 ' 51+1',
 ' 73+1',
 '56+91',
 '78+34',
 ' 67+0',
 ' 69+2',
 ' 06+2',
 '29+14',
 '67+62',
 '38+32',
 ' 58+1',
 ' 38+6',
 '95+95',
 ' 84+1',
 ' 54+8',
 ' 84+8',
 '95+51',
 ' 23+5',
 '47+95',
 '12+31',
 ' 43+2',
 '27+01',
 '36+94',
 ' 73+8',
 ' 08+7',
 ' 99+4',
 ' 97+5',
 ' 79+6',
 '56+94',
 ' 61+7',
 ' 93+7',
 '08+74',
 ' 04+9',
 '59+14',
 '58+96',
 ' 33+3',
 '67+94',
 '68+36',
 '49+77',
 ' 47+5',
 ' 96+3',
 ' 93+8',
 ' 12+1',
 ' 53+9',
 ' 48+9',
 ' 03+6',
 ' 87+3',
 '13+51',
 '28+92',
 ' 26+8',
 '96+84',
 ' 36+7',
 ' 66+2',
 ' 97+9',
 '56+84',
 '89+34',
 ' 83+7',
 '87+55',
 '95+41',
 ' 93+1',
 ' 38+2',
 ' 67+3',
 '05+01',
 '16+42',
 '94+92',
 '37+63',
 ' 44+6',
 ' 93+0',
 '15+71',
 ' 02+7',
 '19+53',
 '84+43',
 ' 17+3',
 ' 18+5',
 '06+03',
 '28+73',
 '28+53',
 '67+24',
 ' 37+6',
 '56+65',
 '28+71',
 '75+22',
 ' 04+7',
 ' 78+0',
 '88+77',
 '74+33',
 ' 48+1',
 ' 83+5',
 ' 21+8',
 ' 64+1',
 '97+11',
 '36+55',
 '28+97',
 '99+86',
 '03+42',
 '19+95',
 ' 92+2',
 '96+96',
 '17+33',
 ' 06+6',
 ' 42+6',
 '39+48',
 '05+34',
 '75+82',
 ' 51+3',
 '18+24',
 ' 79+8',
 '27+55',
 '86+12',
 ' 34+9',
 ' 79+5',
 ' 23+9',
 ' 49+4',
 '74+54',
 ' 82+3',
 '23+81',
 ' 67+2',
 '27+12',
 ' 29+4',
 '59+48',
 ' 99+0',
 ' 63+9',
 ' 95+0',
 ' 74+0',
 ' 17+6',
 '17+07',
 '42+32',
 '69+24',
 ' 82+8',
 ' 02+0',
 '95+01',
 '44+52',
 '89+42',
 '33+11',
 ' 64+6',
 ' 74+8',
 '06+65',
 ' 04+6',
 ' 95+5',
 ' 06+7',
 '24+24',
 ' 58+9',
 '57+01',
 ' 59+7',
 '23+82',
 ' 71+1',
 '36+82',
 '33+22',
 ' 14+9',
 '87+03',
 ' 72+0',
 ' 99+3',
 '96+45',
 ' 52+0',
 '99+92',
 ' 97+4',
 '47+43',
 ' 17+8',
 '48+64',
 ' 85+2',
 ' 36+6',
 ' 95+2',
 '68+06',
 ' 33+1',
 '09+22',
 ' 84+4',
 '96+52',
 ' 27+2',
 '09+98',
 '48+31',
 '67+41',
 ' 65+3',
 ' 66+3',
 ' 44+4',
 '67+91',
 '09+84',
 '79+71',
 ' 63+7',
 '69+48',
 ' 49+1',
 ' 18+7',
 ' 56+8',
 ' 25+5',
 ...]

In [150]:
[s for s in questions if '2' in s and '4' in s]


Out[150]:
[' 54+2',
 '66+42',
 '46+82',
 '27+46',
 '44+82',
 ' 12+4',
 '92+42',
 '  4+2',
 '28+04',
 ' 52+4',
 '27+04',
 ' 21+4',
 ' 74+2',
 ' 49+2',
 '74+12',
 '64+22',
 '08+42',
 ' 02+4',
 '29+46',
 '74+62',
 '42+51',
 '94+82',
 '47+92',
 '14+02',
 ' 27+4',
 ' 84+2',
 ' 25+4',
 '34+02',
 ' 23+4',
 '25+34',
 '04+02',
 ' 42+9',
 '24+21',
 ' 22+4',
 ' 41+2',
 ' 24+4',
 ' 62+4',
 ' 64+2',
 ' 42+5',
 '94+22',
 '87+42',
 '54+32',
 '79+42',
 ' 42+1',
 '25+74',
 '26+04',
 '49+23',
 ' 24+1',
 '94+42',
 '47+62',
 '29+14',
 ' 43+2',
 '16+42',
 '94+92',
 '67+24',
 '03+42',
 ' 42+6',
 '18+24',
 ' 29+4',
 '42+32',
 '69+24',
 '44+52',
 '89+42',
 '24+24',
 '24+81',
 ' 42+0',
 '48+32',
 '36+24',
 '46+62',
 '49+52',
 ' 82+4',
 ' 24+3',
 '48+92',
 '84+52',
 '46+72',
 '64+42',
 '92+41',
 '48+12',
 '38+24',
 ' 24+8',
 '29+54',
 ' 24+9',
 '09+24',
 ' 14+2',
 ' 28+4',
 '65+42',
 '24+71',
 '14+12',
 '94+72',
 '64+92',
 '48+42',
 ' 34+2',
 ' 24+0',
 ' 48+2',
 '23+41',
 '26+94',
 '43+21',
 '24+53',
 ' 24+7',
 '47+27',
 '24+22',
 '94+52',
 '59+24',
 ' 47+2',
 '34+52',
 ' 32+4',
 '44+72',
 '84+82',
 '04+72',
 '47+82',
 '64+52',
 ' 24+6',
 ' 42+8',
 '34+42',
 ' 26+4',
 ' 42+7',
 '64+02',
 '27+74',
 '49+32',
 '24+13',
 ' 92+4',
 '24+61',
 ' 46+2',
 '24+72',
 '28+34',
 '26+24',
 ' 94+2',
 '44+23',
 ' 42+4',
 '39+42',
 '45+25',
 '29+44',
 '63+42',
 ' 45+2',
 '29+42',
 '33+42',
 ' 42+2',
 '24+01',
 '43+72',
 '06+42',
 '84+23',
 '49+28',
 '24+12',
 ' 44+2',
 '26+41',
 '24+42',
 '48+26',
 ' 42+3',
 '29+45',
 '49+24',
 '44+32',
 '49+12',
 '24+51',
 '34+32',
 '34+72',
 '14+52',
 '28+47',
 '64+12',
 ' 24+5',
 '46+22',
 '15+42',
 '88+24',
 '24+03',
 '45+23',
 '87+24',
 '74+22',
 '28+14',
 '64+62',
 '48+52',
 '94+24',
 '74+32',
 '42+91',
 '48+82',
 '16+24',
 '74+52',
 ' 72+4',
 '54+24',
 '75+24',
 '42+42',
 '43+42',
 '49+25',
 ' 04+2',
 '93+42',
 '24+32',
 '47+42',
 '55+24',
 '43+92',
 '27+34',
 '46+32',
 '46+25',
 '43+32',
 '96+42',
 '46+92',
 '24+33',
 '54+22',
 '74+23',
 '24+93',
 ' 24+2',
 '14+32',
 '34+92',
 '22+41',
 '04+92',
 '49+22',
 '02+41',
 '95+42',
 '28+94',
 '24+83',
 '19+42',
 '43+22',
 '27+41',
 '04+22',
 '25+44',
 '29+41',
 '36+42',
 '76+42',
 '44+02',
 '48+24',
 '14+72',
 '46+12',
 '26+42',
 '84+24',
 '07+24',
 '45+02',
 '24+62',
 '79+24',
 '25+54',
 '34+22',
 '54+21',
 '55+42',
 '62+41',
 '26+54',
 '42+21',
 '54+23',
 '49+92',
 '15+24',
 '57+42',
 '45+42',
 '94+62',
 '47+26',
 '54+82',
 '46+26',
 '42+11',
 '59+42',
 '64+24',
 '04+82',
 '25+94',
 '14+82',
 '54+62',
 '47+72',
 '45+12',
 '94+32',
 '94+02',
 '45+72',
 '54+52',
 '74+24',
 '34+12',
 '28+54',
 '84+02',
 '04+32',
 '96+24',
 '99+24',
 '77+24',
 '28+42',
 '06+24',
 '42+31',
 '14+62',
 '42+12',
 '29+74',
 '49+02',
 '32+41',
 '97+42',
 '26+45',
 '46+23',
 '85+24',
 '37+42',
 '69+42',
 '64+23',
 '84+32',
 '94+23',
 '27+94',
 '45+62',
 '47+25',
 '07+42',
 '28+44',
 '53+42',
 '49+27',
 '75+42',
 '88+42',
 '49+82',
 '68+42',
 '66+24',
 '14+23',
 '47+22',
 '24+14',
 '84+21',
 '08+24',
 '45+52',
 '23+42',
 '42+71',
 '49+42',
 '73+42',
 '37+24',
 '24+41',
 '24+11',
 '46+21',
 '28+45',
 '54+12',
 '44+24',
 '13+42',
 '25+04',
 '74+92',
 '62+42',
 '24+63',
 '05+42',
 '46+02',
 '29+47',
 '98+24',
 '42+22',
 '27+84',
 '14+92',
 '26+64',
 '14+22',
 '19+24',
 '47+21',
 '77+42',
 '34+23',
 '97+24',
 '24+92',
 '94+12',
 '49+21',
 '17+24',
 '44+92',
 '56+42',
 '26+44',
 '54+42',
 '44+42',
 '74+42',
 '24+23',
 '26+43',
 '34+82',
 '04+62',
 '25+64',
 '09+42',
 '44+62',
 '35+24',
 '58+24',
 '52+42',
 '39+24',
 '86+24',
 '64+21',
 '42+41',
 '64+72',
 '42+81',
 '48+27',
 '45+82',
 '99+42',
 '43+52',
 '38+42',
 '68+24',
 '89+24',
 '49+72',
 '48+62',
 '94+21',
 '54+72',
 '28+84',
 '52+41',
 '48+25',
 '24+31',
 '86+42',
 '44+22',
 '05+24',
 '25+43',
 '57+24',
 '64+32',
 '76+24',
 '41+21',
 '42+01',
 '24+04',
 '74+82',
 '49+26',
 '14+42',
 '04+21',
 '24+82',
 '28+64',
 '45+92',
 '54+92',
 '24+02',
 '49+62',
 '28+74',
 '26+14',
 '35+42',
 '29+48',
 '26+34',
 '34+62',
 '25+24',
 '25+14',
 '24+73',
 '78+42',
 '56+24',
 '72+42',
 '27+42',
 '46+42',
 '43+23',
 '48+21',
 '48+02',
 '43+82',
 '42+02',
 '18+42',
 '82+42',
 '47+12',
 '47+32',
 '04+52',
 '48+22',
 '34+21',
 '27+44',
 '42+61',
 '43+02',
 '46+52',
 '28+43',
 '27+54',
 '45+22',
 '12+41',
 '26+74',
 '84+42',
 '58+42',
 '84+92',
 '29+84',
 '27+43',
 '74+72',
 '26+84',
 '64+82',
 '04+12',
 '25+42',
 '44+12',
 '85+42',
 '82+41',
 '27+24',
 '46+24',
 '24+43',
 '45+32',
 '54+02',
 '27+45',
 '48+23',
 '49+29',
 '34+24',
 '28+46',
 '65+24',
 '04+42',
 '98+42',
 '28+41',
 '25+84',
 '29+24',
 '29+34',
 '48+28',
 '84+12',
 '29+04',
 '24+91',
 '29+64',
 '47+52',
 '67+42',
 '84+22',
 '43+12',
 '48+72',
 '25+41',
 '45+21',
 '27+64',
 '44+21',
 '29+94',
 '47+02',
 '27+14',
 '84+72',
 '43+62',
 '28+24',
 '78+24',
 '47+23',
 '14+21',
 '24+52',
 '17+42',
 '29+43',
 '74+21',
 '04+23',
 '74+02',
 '45+24']

In [ ]: