pythonLSTM神经网络损失函数曲线代码
时间: 2023-10-17 21:37:20 浏览: 123
以下是一个简单的Python代码示例,展示了如何使用LSTM神经网络训练文本数据集,并绘制损失函数曲线:
```python
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
import matplotlib.pyplot as plt
# 加载文本数据集
path = tf.keras.utils.get_file('nietzsche.txt', origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')
text = open(path, 'rb').read().decode(encoding='utf-8')
vocab = sorted(set(text))
# 构建字符映射表
char2idx = {u: i for i, u in enumerate(vocab)}
idx2char = np.array(vocab)
# 将文本数据集转换为数字序列
text_as_int = np.array([char2idx[c] for c in text])
# 定义输入和目标序列
seq_length = 100
examples_per_epoch = len(text) // seq_length
char_dataset = tf.data.Dataset.from_tensor_slices(text_as_int)
sequences = char_dataset.batch(seq_length+1, drop_remainder=True)
def split_input_target(chunk):
input_text = chunk[:-1]
target_text = chunk[1:]
return input_text, target_text
dataset = sequences.map(split_input_target)
# 批量处理数据集
BATCH_SIZE = 64
VOCAB_SIZE = len(vocab)
EMBEDDING_DIM = 256
RNN_UNITS = 1024
BUFFER_SIZE = 10000
data = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)
# 定义LSTM模型
def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
model = tf.keras.Sequential([
layers.Embedding(vocab_size, embedding_dim,
batch_input_shape=[batch_size, None]),
layers.LSTM(rnn_units,
return_sequences=True,
stateful=True,
recurrent_initializer='glorot_uniform'),
layers.Dense(vocab_size)
])
return model
# 构建模型
model = build_model(VOCAB_SIZE, EMBEDDING_DIM, RNN_UNITS, BATCH_SIZE)
# 定义损失函数
def loss(labels, logits):
return tf.keras.losses.sparse_categorical_crossentropy(labels, logits, from_logits=True)
# 编译模型
model.compile(optimizer='adam', loss=loss)
# 训练模型
history = model.fit(data, epochs=50)
# 绘制损失函数曲线
plt.plot(history.history['loss'], label='training loss')
plt.title('Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
```
这个示例使用了TensorFlow和Keras库来构建LSTM模型并训练文本数据集。在训练过程中,我们记录了每个epoch的训练损失,并使用Matplotlib库绘制了损失函数曲线。
阅读全文