python seq2seq摘要项目片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python seq2seq摘要项目片段相关的知识,希望对你有一定的参考价值。
import numpy as np
for i in range(5):
print("nothing")
print(i)
a = [[1, 2, 3],
[2, 3, 4],
[5, 4, 3]]
print(np.argmax(a, 0))
seq_length = [60 for i in range(20)]
print(seq_length)
a = np.zeros(5)
a[0] = 0
for i in range(1, 5):
a[i] = i
print(a)
print(len(a))
import tensorflow as tf
batch_size = 2
articles = tf.constant([[1, 2, 4], [2, 3, 9]])
encoder_inputs = tf.unstack(tf.transpose(articles))
with tf.Session() as sess:
encoder_inputs = (sess.run(encoder_inputs))
for i in encoder_inputs:
print(i)
import tensorflow as tf
from tensorflow.contrib.seq2seq import *
from tensorflow.python.layers.core import Dense
class Seq2SeqModel(object):
def __init__(self, rnn_size, layer_size, encoder_vocab_size,
decoder_vocab_size, embedding_dim, grad_clip, is_inference=False):
# define inputs
self.input_x = tf.placeholder(tf.int32, shape=[None, None], name='input_ids')
# define embedding layer
with tf.variable_scope('embedding'):
encoder_embedding = tf.Variable(tf.truncated_normal(shape=[encoder_vocab_size, embedding_dim], stddev=0.1),
name='encoder_embedding')
decoder_embedding = tf.Variable(tf.truncated_normal(shape=[decoder_vocab_size, embedding_dim], stddev=0.1),
name='decoder_embedding')
# define encoder
with tf.variable_scope('encoder'):
encoder = self._get_simple_lstm(rnn_size, layer_size)
with tf.device('/cpu:0'):
input_x_embedded = tf.nn.embedding_lookup(encoder_embedding, self.input_x)
encoder_outputs, encoder_state = tf.nn.dynamic_rnn(encoder, input_x_embedded, dtype=tf.float32)
# define helper for decoder
if is_inference:
self.start_tokens = tf.placeholder(tf.int32, shape=[None], name='start_tokens')
self.end_token = tf.placeholder(tf.int32, name='end_token')
helper = GreedyEmbeddingHelper(decoder_embedding, self.start_tokens, self.end_token)
else:
self.target_ids = tf.placeholder(tf.int32, shape=[None, None], name='target_ids')
self.decoder_seq_length = tf.placeholder(tf.int32, shape=[None], name='batch_seq_length')
with tf.device('/cpu:0'):
target_embeddeds = tf.nn.embedding_lookup(decoder_embedding, self.target_ids)
helper = TrainingHelper(target_embeddeds, self.decoder_seq_length)
with tf.variable_scope('decoder'):
fc_layer = Dense(decoder_vocab_size)
decoder_cell = self._get_simple_lstm(rnn_size, layer_size)
decoder = BasicDecoder(decoder_cell, helper, encoder_state, fc_layer)
logits, final_state, final_sequence_lengths = dynamic_decode(decoder)
if not is_inference:
targets = tf.reshape(self.target_ids, [-1])
logits_flat = tf.reshape(final_outputs.rnn_output, [-1, decoder_vocab_size])
print
'shape logits_flat:{}'.format(logits_flat.shape)
print
'shape logits:{}'.format(final_outputs.rnn_output.shape)
self.cost = tf.losses.sparse_softmax_cross_entropy(targets, logits_flat)
# define train op
tvars = tf.trainable_variables()
grads, _ = tf.clip_by_global_norm(tf.gradients(self.cost, tvars), grad_clip)
optimizer = tf.train.AdamOptimizer(1e-3)
self.train_op = optimizer.apply_gradients(zip(grads, tvars))
else:
self.prob = tf.nn.softmax(logits)
def _get_simple_lstm(self, rnn_size, layer_size):
lstm_layers = [tf.contrib.rnn.LSTMCell(rnn_size) for _ in xrange(layer_size)]
import numpy as np
import tensorflow as tf
CLASS = 10
label1 = tf.constant([5, 2, 1])
sess1 = tf.Session()
print('label1:', sess1.run(label1))
b = tf.one_hot(label1, depth=CLASS, dtype=tf.float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(b)
print('after one_hot', sess.run(b))
with tf.variable_scope('encoder', reuse=tf.AUTO_REUSE):
encoder_cell = tf.nn.rnn_cell.LSTMCell(num_units=self.encoder_hidden_units, name='encoder_cell')
self.encoder_output, self.encoder_final_state = tf.nn.dynamic_rnn(
cell=encoder_cell,
inputs=self.encoder_inputs_embedded,
dtype=tf.float32,
time_major=False
)
print(self.encoder_final_state)
self.decoder_logits_test = tf.contrib.layers.linear(self.decoder_outputs_test, self.vocab_size)
self.decoder_prediction = tf.argmax(self.decoder_logits_test, 2)
self.targets = tf.reshape(self.decoder_targets, [-1])
self.logits_flat = tf.reshape(self.decoder_train_logits.rnn_output, [-1, self.vocab_size])
self.logits_flat = tf.argmax(self.logits_flat, 0)
self.stepwise_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
labels=tf.one_hot(self.decoder_targets,
depth=self.vocab_size,
dtype=tf.float32),
logits=self.model_outputs,
)
self.loss = tf.reduce_mean(self.stepwise_cross_entropy)
self.loss = tf.losses.sparse_softmax_cross_entropy(self.targets, self.logits_flat)
self.decoder_outputs_train, _ = tf.nn.dynamic_rnn(
cell=decoder_cell,
inputs=self.decoder_inputs_embedded,
initial_state=self.encoder_final_state,
dtype=tf.float32,
sequence_length=self.decoder_length,
time_major=False)
start_tokens = 0
end_tokens = 0
# Helper
helper = s2s.GreedyEmbeddingHelper(
self.embeddings_trainable,
tf.fill([self.batch_size], start_tokens), end_tokens)
# Decoder
decoder = s2s.BasicDecoder(
decoder_cell, helper, self.encoder_final_state)
# Dynamic decoding
decoder_infer_outputs, _, _ = s2s.dynamic_decode(
decoder, maximum_iterations=25)
self.decoder_prediction = decoder_infer_outputs.sample_id
# single layer blstm encoder
for layer_i in range(self.encoder_layers):
with tf.variable_scope('encoder%i' % layer_i, reuse=tf.AUTO_REUSE):
cell_fw = rnn.LSTMCell(
num_units=self.encoder_hidden_units,
initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=114),
state_is_tuple=True)
cell_bw = rnn.LSTMCell(
num_units=self.encoder_hidden_units,
initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=133),
state_is_tuple=True)
(self.encoder_inputs_embedded, self.encoder_final_state) = tf.nn.bidirectional_dynamic_rnn(
cell_fw=cell_fw,
cell_bw=cell_bw,
inputs=self.encoder_inputs_embedded,
dtype=tf.float32)
self.encoder_final_state_c = tf.concat(
(self.encoder_final_state[0].c, self.encoder_final_state[1].c), 1)
self.encoder_final_state_h = tf.concat(
(self.encoder_final_state[0].h, self.encoder_final_state[1].h), 1)
self.encoder_final_state = contrib.rnn.LSTMStateTuple(
c=self.encoder_final_state_c,
h=self.encoder_final_state_h)
# multi layer bgru encoder with dropout wrapper
with tf.variable_scope('encoder', reuse=tf.AUTO_REUSE):
inputs = self.encoder_inputs_embedded
for layer_i in range(self.encoder_layers):
with tf.variable_scope(None, default_name="bidirectional-rnn-%i" % layer_i):
cell_fw = tf.nn.rnn_cell.GRUCell(
num_units=self.encoder_hidden_units,
kernel_initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=114))
cell_bw = tf.nn.rnn_cell.GRUCell(
num_units=self.encoder_hidden_units,
kernel_initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=114))
if self.is_train:
cell_fw = tf.nn.rnn_cell.DropoutWrapper(cell_fw, output_keep_prob=self.keep_prob)
cell_bw = tf.nn.rnn_cell.DropoutWrapper(cell_bw, output_keep_prob=self.keep_prob)
(output, self.encoder_final_state) = tf.nn.bidirectional_dynamic_rnn(
cell_fw=cell_fw,
cell_bw=cell_bw,
inputs=inputs,
dtype=tf.float32)
inputs = tf.concat(output, 2)
self.encoder_final_state = tf.concat(self.encoder_final_state, 1)
以上是关于python seq2seq摘要项目片段的主要内容,如果未能解决你的问题,请参考以下文章
DL4NLP —— seq2seq+attention机制的应用:文档自动摘要(Automatic Text Summarization)