<jupyter_start><jupyter_text>Global Variables<jupyter_code>_BATCH_SIZE = 64
_VOCAB_SIZE = 6000
_WORD_DIM = 128
_MODEL_DEPTH = 4

_INPUT_LENGTH = 25
_OUTPUT_LENGTH = 10<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>model = AttentionSeq2Seq(input_length=_INPUT_LENGTH, 
                         input_dim=_WORD_DIM, 
                         hidden_dim=_WORD_DIM, 
                         output_length=_OUTPUT_LENGTH, 
                         output_dim=_WORD_DIM, 
                         depth=_MODEL_DEPTH)
model.compile(loss='mse', optimizer='rmsprop')<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>embedding = get_word_embedding(_WORD_DIM)
train_data = get_train_data()
_, ch2int = get_vocab()
len(train_data)
def pad_to(lst, length, value):
    for i in range(len(lst), length):
        lst.append(value)
    
    return lst

def clean_train_data(train_data):
    X_train = []
    Y_train = []
    for idx in xrange(len(train_data)):
        line_number = idx % 4
        
        keyword = train_data[idx]['keyword']
        current_sentence = train_data[idx]['sentence']
        previous_sentences = ''.join([train_data[idx - i]['sentence'] for i in range(line_number, 0, -1)])
        
        X_entry = pad_to([[ch2int[ch]] for ch in (keyword + previous_sentences)], 25, [_VOCAB_SIZE - 1])
        Y_entry = pad_to([[ch2int[ch]] for ch in current_sentence], 10, [_VOCAB_SIZE - 1])
        
        X_train.append(X_entry)
        Y_train.append(Y_entry)
        
    return X_train, Y_train
X_train, Y_train = clean_train_data(train_data)
X_train_embedded = [map(lambda x: embedding[x[0]], sample) for sample in X_train]
Y_train_embedded = [map(lambda x: embedding[x[0]], sample) for sample in Y_train]<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>model.fit(X_train_embedded, Y_train_embedded, epochs=1, verbose=1)<jupyter_output>Epoch 1/1
39956/39956 [==============================] - 421s - loss: 0.4278<jupyter_text>Generation<jupyter_code>kw = u'山水'
kw_pad = [pad_to([[ch2int[ch]] for ch in kw], 25, [_VOCAB_SIZE - 1])]
kw_embed = [map(lambda x: embedding[x[0]], sample) for sample in kw_pad]
kw_embed_array = np.array(kw_embed)
pred = model.predict(kw_embed_array)
pred
w2v_model = Word2Vec.load('data/word2vec.model')
result = []
for i in range(len(pred[0])):
    result.append(w2v_model.most_similar(positive=[pred[0][i]], topn=1))
for r in result:
    print r[0][0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1345764085934333, 'fraction_numerical': 0.0263477908390758, 'mean_word_length': 3.975806451612903, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '142439', 'license': 'MIT', 'n_tokens_mistral': 965, 'n_tokens_neox': 900, 'n_words': 155, 'path': 'notebooks/Keras Seq2Seq V1.ipynb', 'repo_name': 'qkhy/poetry-seq2seq', 'size': '2467'}