给一个基于LSTM实现英文翻译中文的代码
时间: 2024-05-09 16:14:23 浏览: 105
下面是一个基于LSTM实现英文翻译中文的代码示例:
```python
import numpy as np
import tensorflow as tf
# 读取数据
with open('english.txt', 'r') as f:
english_data = f.read().splitlines()
with open('chinese.txt', 'r') as f:
chinese_data = f.read().splitlines()
# 构建词典
english_words = sorted(list(set(' '.join(english_data).split())))
english_to_int = {word: idx for idx, word in enumerate(english_words)}
int_to_english = {idx: word for word, idx in english_to_int.items()}
english_vocab_size = len(english_words)
chinese_words = sorted(list(set(' '.join(chinese_data).split())))
chinese_to_int = {word: idx for idx, word in enumerate(chinese_words)}
int_to_chinese = {idx: word for word, idx in chinese_to_int.items()}
chinese_vocab_size = len(chinese_words)
# 将文本转换为数字序列
def text_to_seq(text, word_to_int):
return [word_to_int[word] for word in text.split()]
english_seq = [text_to_seq(text, english_to_int) for text in english_data]
chinese_seq = [text_to_seq(text, chinese_to_int) for text in chinese_data]
# 定义模型参数
embedding_size = 128
lstm_size = 128
batch_size = 50
epochs = 100
# 构建模型
inputs = tf.placeholder(tf.int32, [None, None], name='inputs')
targets = tf.placeholder(tf.int32, [None, None], name='targets')
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
source_vocab_size = english_vocab_size
target_vocab_size = chinese_vocab_size
# encoder
enc_embeddings = tf.Variable(tf.random_uniform([source_vocab_size, embedding_size], -1.0, 1.0), name='enc_embeddings')
enc_embed_input = tf.nn.embedding_lookup(enc_embeddings, inputs)
enc_cell = tf.contrib.rnn.LSTMCell(lstm_size)
enc_cell = tf.contrib.rnn.DropoutWrapper(enc_cell, output_keep_prob=keep_prob)
_, enc_state = tf.nn.dynamic_rnn(enc_cell, enc_embed_input, dtype=tf.float32)
# decoder
dec_embeddings = tf.Variable(tf.random_uniform([target_vocab_size, embedding_size], -1.0, 1.0), name='dec_embeddings')
dec_inputs = tf.nn.embedding_lookup(dec_embeddings, targets)
dec_cell = tf.contrib.rnn.LSTMCell(lstm_size)
dec_cell = tf.contrib.rnn.DropoutWrapper(dec_cell, output_keep_prob=keep_prob)
dec_output, _ = tf.nn.dynamic_rnn(dec_cell, dec_inputs, initial_state=enc_state, dtype=tf.float32)
# 输出层
logits = tf.contrib.layers.fully_connected(dec_output, target_vocab_size, activation_fn=None)
probs = tf.nn.softmax(logits, name='probs')
# 损失函数和优化器
cost = tf.contrib.seq2seq.sequence_loss(logits, targets, tf.ones([batch_size, tf.reduce_max(tf.shape(targets))]))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
# 训练模型
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(epochs):
for batch_idx in range(len(english_data) // batch_size):
start_idx = batch_idx * batch_size
end_idx = start_idx + batch_size
batch_inputs = np.array(english_seq[start_idx:end_idx])
batch_targets = np.array(chinese_seq[start_idx:end_idx])
_, loss = sess.run([optimizer, cost], feed_dict={inputs: batch_inputs, targets: batch_targets, keep_prob: 0.5})
print('Epoch: {}, Batch: {}, Loss: {:.3f}'.format(epoch + 1, batch_idx + 1, loss))
# 保存模型
saver = tf.train.Saver()
saver.save(sess, 'model.ckpt')
```
请注意,此代码仅用于示例目的。实际使用中,您需要根据自己的数据进行修改和调整。
阅读全文