Пример #1
0
        tf.placeholder_with_default(data.dparams['max_line_length'],
                                    None,
                                    name='sequence_length')

    # Find the shape of the input data for sequence_loss
    input_shape = tf.shape(input_data)

    print('Create logits using the model...')
    # Create training and inference logits
    train_logits, inference_logits = \
        seq2seq_model(tf.reverse(input_data, [-1]),
                      targets,
                      keep_prob,
                      data.tparams['batch_size'],
                      sequence_length,
                      len(vocab_to_int),
                      data.hparams['encoding_embedding_size'],
                      data.hparams['decoding_embedding_size'],
                      data.hparams['rnn_size'],
                      data.hparams['num_layers'],
                      vocab_to_int,
                      data.hparams['attn_length'])

    # Create a tensor to be used for making predictions
    tf.identity(inference_logits, 'logits')

    print('Optimize training operation...')
    with tf.name_scope('optimization'):

        # Loss function
        cost = \
            sequence_loss(train_logits,
Пример #2
0
test_data = answers_int[BATCH_SIZE:]
val_train_data = questions_int[:BATCH_SIZE]
val_test_data = answers_int[:BATCH_SIZE]

pad_int = vocabs_to_index['<PAD>']

val_batch_x, val_batch_len = pad_sentence(val_train_data, pad_int)
val_batch_y, val_batch_len_y = pad_sentence(val_test_data, pad_int)
val_batch_x = np.array(val_batch_x)
val_batch_y = np.array(val_batch_y)

no_of_batches = math.floor(len(train_data) // BATCH_SIZE)
round_no = no_of_batches * BATCH_SIZE

input_data, target_data, input_data_len, target_data_len, lr_rate, keep_probs, inference_logits, cost, train_op = seq2seq_model(
    question_vocab_size, EMBED_SIZE, RNN_SIZE, KEEP_PROB, answer_vocab_size,
    BATCH_SIZE, vocabs_to_index)

translate_sentence = 'how are you'
translate_sentence = sentence_to_seq(translate_sentence, vocabs_to_index)

acc_plt = []
loss_plt = []
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(EPOCHS):
        total_accuracy = 0.0
        total_loss = 0.0
        for bs in tqdm(range(0, round_no, BATCH_SIZE)):
            index = min(bs + BATCH_SIZE, round_no)
Пример #3
0
    sequence_length = \
        tf.placeholder_with_default(max_length,
                                    None,
                                    name='sequence_length')

    # Find the shape of the input data for sequence_loss
    input_shape = tf.shape(input_data)

    # Create training and inference logits
    train_logits, inference_logits = \
        seq2seq_model(tf.reverse(input_data, [-1]),
                      targets,
                      keep_prob,
                      batch_size,
                      sequence_length,
                      len(vocab_to_int),
                      enc_embed_size,
                      dec_embed_size,
                      rnn_size,
                      num_layers,
                      vocab_to_int,
                      attn_length)

    # Create a tensor to be used for making predictions
    tf.identity(inference_logits, 'logits')

    print('Optimize RNN...')
    with tf.name_scope('optimization'):

        # Loss function
        cost = \
            sequence_loss(train_logits,
Пример #4
0
#Defining a session
tf.reset_default_graph()
session = tf.InteractiveSession()

#Loading the model inputs
inputs, targets, lr, keep_prob = model.model_inputs()

#Setting the sequence length
sequence_length = tf.placeholder_with_default(25, None, name='sequence_length')

#Getting the shape of input tensor
input_shape = tf.shape(inputs)

#Getting the training and test predictions
training_predictions, test_predictions = model.seq2seq_model(
    tf.reverse(inputs, [-1]), targets, keep_prob, batch_size, sequence_length,
    len(answerwords2int), len(questionwords2int), encoding_embedding_size,
    decoding_embedding_size, rnn_size, num_layers, questionwords2int)
#Setting up the loss error,the optimizer gradient clipping
with tf.name_scope("optimization"):
    loss_error = tf.contrib.seq2seq.sequence_loss(
        training_predictions, targets,
        tf.ones([input_shape[0], sequence_length]))
    optimizer = tf.train.AdamOptimizer(learning_rate)
    gradients = optimizer.compute_gradients(loss_error)
    clipped_gradients = [(tf.clip_by_value(grad_tensor, -5.,
                                           5.), grad_variable)
                         for grad_tensor, grad_variable in gradients
                         if grad_tensor is not None]
    optimizer_gradient_clipping = optimizer.apply_gradients(clipped_gradients)

Пример #5
0
# 构造graph
train_graph = tf.Graph()

with train_graph.as_default():
    # 获得模型输入
    input_data, targets, lr, target_sequence_length, max_target_sequence_length, source_sequence_length = get_inputs(
    )

    training_decoder_output, predicting_decoder_output = seq2seq_model(
        input_data,
        targets,
        lr,
        target_sequence_length,
        max_target_sequence_length,
        source_sequence_length,
        len(data_process.source_letter_to_int),
        len(data_process.target_letter_to_int),
        encoding_embedding_size,  # 15
        decoding_embedding_size,  # 15
        rnn_size,  # 50
        num_layers)  # 2
    # tf.identity 在计算图中建立建立复制计算节点
    training_logits = tf.identity(training_decoder_output.rnn_output, 'logits')
    predicting_logits = tf.identity(predicting_decoder_output.sample_id,
                                    name='predictions')

    masks = tf.sequence_mask(target_sequence_length,
                             max_target_sequence_length,
                             dtype=tf.float32,
                             name='masks')
Пример #6
0
k = 0
while file:
    line = file.readline().decode('utf-8')
    k += 1
    if k % 1000 == 0:
        print k
    split = re.split(' ', line)
    voc.append(split[0])
    vec = split[1:101]
    for i in range(len(vec)):
        vec[i] = eval(vec[i])
    embed.append(vec)
    if k > 10000:
        break

model = seq2seq_model(voc, embed, 100, 4)
batch_size = 20
maxi_length = 40

sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())

try:
    saver = tf.train.import_meta_graph('model.ckpt.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./'))
    print 'load finished'
except:
    print 'load failed'

total_parameters = 0
Пример #7
0
keep_probability = 0.8
beam_width = 20

print('Building graph')
# Build the graph
train_graph = tf.Graph()
# Set the graph to default to ensure that it is ready for training
with train_graph.as_default():
    # Load the model inputs
    input_data, targets, lr, keep_prob, target_length, max_target_length, input_length = model.model_inputs(
    )

    # Create the training and inference logits
    training_logits, inference_logits = model.seq2seq_model(
        tf.reverse(input_data, [-1]), targets, keep_prob, input_length,
        target_length, max_target_length,
        len(vocab2int) + 1, rnn_size, num_layers, vocab2int,
        word_embedding_matrix, batch_size, beam_width)

    # Create tensors for the training logits and inference logits
    training_logits = tf.identity(training_logits.rnn_output, 'logits')
    inference_logits = tf.identity(inference_logits.predicted_ids,
                                   name='predictions')

    # Create the weights for sequence_loss
    masks = tf.sequence_mask(target_length,
                             max_target_length,
                             dtype=tf.float32,
                             name='masks')

    with tf.name_scope("optimization"):
sess = tf.InteractiveSession()

input_data = tf.placeholder(tf.int32, [None, None], name='input')
targets = tf.placeholder(tf.int32, [None, None], name='targets')
lr = tf.placeholder(tf.float32, name='learning_rate')
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
sequence_length = tf.placeholder_with_default(20, None, name='sequence_length')

# Find the shape of the input data for sequence_loss
input_shape = tf.shape(input_data)

# Create the training and inference logits
train_logits, inference_logits = model.seq2seq_model(
    tf.reverse(input_data, [-1]), targets, keep_prob,
    batch_size, sequence_length, len(answers_vocab_to_int),
    len(questions_vocab_to_int), encoding_embedding_size,
    decoding_embedding_size, rnn_size, num_layers, questions_vocab_to_int)

tf.identity(inference_logits, 'logits')

with tf.name_scope("optimization"):
    # Loss function
    cost = tf.contrib.seq2seq.sequence_loss(
        train_logits, targets, tf.ones([input_shape[0], sequence_length]))

    # Optimizer
    optimizer = tf.train.AdamOptimizer(learning_rate)

    # Gradient Clipping
    gradients = optimizer.compute_gradients(cost)