import pandas as pd import time import numpy as np import… · The Chat Archive
Explore / Programming import pandas as pd import time import numpy as np import matplotlib.pyplot as plt… Save Share Download ▾ Reportimport pandas as pd
import time
import numpy as np
import matplotlib.pyplot as plt
import os
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
from collections import Counter
import torch.nn.functional
import pennylane as qml
from pennylane import numpy as np
from datasets import load_dataset
from tqdm import tqdm # 添加进度条
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import re
from sklearn.model_selection import train_test_split
from qiskit import QuantumCircuit
from qiskit.visualization import circuit_drawer
from sklearn.metrics import f1_score, recall_score
import random
seed = 42
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
min_freq = 1
sequence_length = 8
embedding_dim = 100 # 词嵌入维度 与mat1有关
batch_size = 32
hidden_size = 64
dropout_rate = 0.4
lr = 0.001
n_qlayers = 2
heads = 4
epoch = 40
n_qubits = 8
#weight_decay = 0.00001
读取数据文件
def load_data(file_path):
data = []
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
text, label = line.strip().split('\t')
data.append((text, int(label)))
return pd.DataFrame(data, columns=['text', 'label'])
文本清洗函数
def clean_text(text):
# 转换为小写
text = text.lower()
# 移除 HTML 标签
text = re.sub(r'<.*?>', '', text)
# 移除 URL
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
# 移除标点符号和数字
text = re.sub(r'\d+', '', text)
text = re.sub(r'[^\w\s]', '', text)
# 分词
words = word_tokenize(text)
# 移除停用词
stop_words = set(stopwords.words('english'))
words = [word for word in words if word not in stop_words]
return ' '.join(words)
加载数据
file_path = 'amazon_cells_labelled.txt'
dataset = load_data(file_path)
随机抽取一个新的训练集(800条)、验证集(100条)和测试集(100条)
train_val_set, test_set = train_test_split(dataset, train_size=900, test_size=100, random_state=seed, stratify=dataset['label'])
train_set, val_set = train_test_split(train_val_set, train_size=800, test_size=100, random_state=seed, stratify=train_val_set['label'])
清洗所有数据集的文本
train_set['text'] = train_set['text'].apply(clean_text)
val_set['text'] = val_set['text'].apply(clean_text)
test_set['text'] = test_set['text'].apply(clean_text)
定义一个函数来标记和编码句子
def tokenize_and_encode(sentences, vocab, seq_length):
token_ids = [
[vocab.get(token, vocab['']) for token in sentence.split()]
for sentence in sentences
]
# 填充序列
token_ids = [
t_ids[:seq_length] if len(t_ids) > seq_length else t_ids + [vocab['']] * (seq_length - len(t_ids))
for t_ids in token_ids
]
return token_ids
从训练数据集创建词汇表
all_train_tokens = [token for sentence in train_set['text'] for token in sentence.split()]
计算所有训练数据中单词的频率
Assistant2024-10-09 01:23 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Oct 9, 2024
Added to archive Nov 16, 2026
ID 01m7h41x6f3xh8mdhdzk76a8t3Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 859b1bc801e626c625f742df3db131cf
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
word_counts = Counter(all_train_tokens)
打印词汇表中每个单词的频率,按照频率从高到低排序 for word, count in word_counts.most_common():
print(f"{word}: {count}")
只保留出现次数大于等于阈值的单词 filtered_words = [word for word, count in word_counts.items() if count >= min_freq]
vocabulary = {word: i + 2 for i, word in enumerate(filtered_words)}
vocabulary[''] = 1
vocabulary[''] = 0
打印词汇表大小 print(f"真实Vocabulary size: {len(vocabulary)}")
对数据集进行标记和编码 train_token_ids = tokenize_and_encode(train_set['text'], vocabulary, sequence_length)
val_token_ids = tokenize_and_encode(val_set['text'], vocabulary, sequence_length)
#test_token_ids = tokenize_and_encode(test_set['text'], vocabulary, sequence_length)
创建自定义 PyTorch 数据集 class SentimentDataset(Dataset):
def init (self, token_ids, labels):
self.token_ids = token_ids
self.labels = labels
def __len__(self):
return len(self.token_ids)
def __getitem__(self, idx):
return torch.tensor(self.token_ids[idx], dtype=torch.long), torch.tensor(self.labels[idx], dtype=torch.long)
创建自定义 Dataset 实例 train_dataset = SentimentDataset(train_token_ids, train_set['label'].tolist())
val_dataset = SentimentDataset(val_token_ids, val_set['label'].tolist())
#test_dataset = SentimentDataset(test_token_ids, test_set['label'].tolist())
打印标记和编码后的数据样本 print("\n标记和编码后的数据样本:")
for sample in train_token_ids[:100]:
print(" ".join(map(str, sample)))
创建 DataLoader train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
#test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
打印训练集、验证集和测试集的标签统计信息 train_positive_count = train_set[train_set['label'] == 1].shape[0]
train_negative_count = train_set[train_set['label'] == 0].shape[0]
print(f"Train Positive count: {train_positive_count}")
print(f"Train Negative count: {train_negative_count}")
val_positive_count = val_set[val_set['label'] == 1].shape[0]
val_negative_count = val_set[val_set['label'] == 0].shape[0]
print(f"Validation Positive count: {val_positive_count}")
print(f"Validation Negative count: {val_negative_count}")
test_positive_count = test_set[test_set['label'] == 1].shape[0]
test_negative_count = test_set[test_set['label'] == 0].shape[0]
print(f"Test Positive count: {test_positive_count}")
print(f"Test Negative count: {test_negative_count}")
#自注意力类
class MultiHeadSelfAttention(nn.Module):
def init (self, embed_dim, heads):
super().init ()
self.embed_dim = embed_dim
self.heads = heads
self.head_dim = embed_dim // heads
assert self.head_dim * heads == embed_dim, "Embed size needs to be divisible by heads"
self.values = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.keys = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.queries = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.fc_out = nn.Linear(heads * self.head_dim, embed_dim)
def forward(self, values, keys, query, mask):
N = query.shape[0]
value_len, key_len, query_len = values.shape[1], keys.shape[1], query.shape[1]
# Split the embedding into self.heads different pieces
values = values.reshape(N, value_len, self.heads, self.head_dim)
keys = keys.reshape(N, key_len, self.heads, self.head_dim)
queries = query.reshape(N, query_len, self.heads, self.head_dim)
values = self.values(values)
keys = self.keys(keys)
queries = self.queries(queries)
energy = torch.einsum("nqhd,nkhd->nhqdk", [queries, keys])
if mask is not None:
energy = energy.masked_fill(mask == 0, float("-1e20"))
attention = torch.softmax(energy / (self.embed_dim ** (1 / 2)), dim=3)
out = torch.einsum("nhqdk,nqhd->nhqd", [attention, values]).reshape(N, query_len, self.heads * self.head_dim)
out = self.fc_out(out)
return out
量子LSTM class QLSTM(nn.Module):
def init (self,
input_size,
hidden_size,
n_qubits=n_qubits,
n_qlayers=n_qlayers,
n_vrotations=3,
batch_first=True,
return_sequences=False,
return_state=False,
backend="default.qubit",
dropout_rate=dropout_rate):
super(QLSTM, self).init ()
self.n_inputs = input_size
self.hidden_size = hidden_size
self.concat_size = self.n_inputs + self.hidden_size
self.n_qubits = n_qubits
self.n_qlayers = n_qlayers
self.n_vrotations = n_vrotations
self.backend = backend
self.batch_first = batch_first
self.return_sequences = return_sequences
self.return_state = return_state
self.wires_forget = [f"wire_forget_{i}" for i in range(self.n_qubits)]
self.wires_input = [f"wire_input_{i}" for i in range(self.n_qubits)]
self.wires_update = [f"wire_update_{i}" for i in range(self.n_qubits)]
self.wires_output = [f"wire_output_{i}" for i in range(self.n_qubits)]
self.wires_output_h = [f"wire_output_h_{i}" for i in range(self.n_qubits)]
# 定义线性层
self.fc = nn.Linear(self.hidden_size, 2)
# 初始化权重形状
self.weight_shapes = {"weights": (self.n_qlayers, self.n_vrotations, self.n_qubits)}
# 定义量子设备
# self.dev = qml.device('default.qubit', wires=4)
self.dev_forget = qml.device(self.backend, wires=self.wires_forget)
self.dev_input = qml.device(self.backend, wires=self.wires_input)
self.dev_update = qml.device(self.backend, wires=self.wires_update)
self.dev_output = qml.device(self.backend, wires=self.wires_output)
self.dev_output_h = qml.device(self.backend, wires=self.wires_output_h)
# 定义 ansatz 方法
self.ansatz = self.ansatz
# 初始化dropout层
self.dropout = nn.Dropout(dropout_rate)
# QNode实例化
self.qlayer_forget = qml.QNode(self._circuit_forget, self.dev_forget, interface="torch")
self.qlayer_input = qml.QNode(self._circuit_input, self.dev_input, interface="torch")
self.qlayer_update = qml.QNode(self._circuit_update, self.dev_update, interface="torch")
self.qlayer_output = qml.QNode(self._circuit_output, self.dev_output, interface="torch")
self.qlayer_output_h = qml.QNode(self._circuit_output_h, self.dev_output_h, interface="torch")
# 定义权重形状
weight_shapes = {"weights": (self.n_qlayers, self.n_vrotations, self.n_qubits)}
# print(f"weight_shapes = (n_qlayers, n_vrotations, n_qubits) = ({self.n_qlayers}, {self.n_vrotations}, {self.n_qubits})")
# 定义线性层
self.clayer_in = nn.Linear(self.concat_size, self.n_qubits)
self.clayer_out = nn.Linear(self.n_qubits, self.hidden_size)
self.clayer_out_f = nn.Linear(self.n_qubits, self.hidden_size)
self.clayer_out_i = nn.Linear(self.n_qubits, self.hidden_size)
self.clayer_out_g = nn.Linear(self.n_qubits, self.hidden_size)
self.clayer_out_o = nn.Linear(self.n_qubits, self.hidden_size)
self.TorchLayers = {
'forget': qml.qnn.TorchLayer(self.qlayer_forget, self.weight_shapes),
'input': qml.qnn.TorchLayer(self.qlayer_input, self.weight_shapes),
'update': qml.qnn.TorchLayer(self.qlayer_update, self.weight_shapes),
'output': qml.qnn.TorchLayer(self.qlayer_output, self.weight_shapes),
'output_h': qml.qnn.TorchLayer(self.qlayer_output_h, self.weight_shapes)
}
def ansatz(self, params, wires_type):
# Entangling layer.
for i in range(1, 3):
for j in range(self.n_qubits):
if j + i < self.n_qubits:
qml.CNOT(wires=[wires_type[j], wires_type[j + i]])
else:
qml.CNOT(wires=[wires_type[j], wires_type[j + i - self.n_qubits]])
# Variational layer.
for i in range(self.n_qubits):
qml.RX(params[0][i], wires=wires_type[i])
qml.RY(params[1][i], wires=wires_type[i])
qml.RZ(params[2][i], wires=wires_type[i])
def vqc(self, features, weights, wires_type):
# 将features转换为PyTorch张量(如果它是一个NumPy数组)
if isinstance(features, np.ndarray):
features = torch.tensor(features, dtype=torch.float32)
# 检查features是否为一维张量,如果是,则添加一个新的批次维度
if features.dim() == 1:
features = features.unsqueeze(0) # 将其变为二维张量,形状为[1, 特征数]
if not isinstance(features, torch.Tensor):
features = torch.tensor(features, dtype=torch.float32)
# print("features形状:", features.shape)
# print("Weights shape:", weights.shape)
# 确保 features 的长度等于量子位的数量
features = features[:, :self.n_qubits]
ry_params = torch.atan2(features, torch.ones_like(features))
rz_params = torch.atan2(features ** 2, torch.ones_like(features))
# ry_params = [torch.arctan(feature) for feature in features]
# rz_params = [torch.arctan(feature ** 2) for feature in features]
# 应用Hadamard门到每个量子位上
for i in range(self.n_qubits):
qml.Hadamard(wires=wires_type[i])
# 对量子位进行RY和RZ旋转
for i in range(self.n_qubits):
qml.RY(ry_params[:, i], wires=wires_type[i])
qml.RZ(rz_params[:, i], wires=wires_type[i])
# 创建量子位之间的纠缠
for i in range(1, 3):
for j in range(self.n_qubits):
if j + i < self.n_qubits:
qml.CNOT(wires=[wires_type[j], wires_type[j + i]])
else:
qml.CNOT(wires=[wires_type[j], wires_type[j + i - self.n_qubits]])
# 再次对量子位进行RX、RY、RZ旋转
for layer in range(self.n_qlayers):
for i in range(self.n_qubits):
qml.RX(weights[layer, 0, i], wires=wires_type[i])
qml.RY(weights[layer, 1, i], wires=wires_type[i])
qml.RZ(weights[layer, 2, i], wires=wires_type[i])
# 定义量子电路,模拟LSTM中的门控机制
def _circuit_forget(self, inputs, weights):
self.vqc(inputs, weights, self.wires_forget)
return [qml.expval(qml.PauliZ(wires=i)) for i in self.wires_forget]
def _circuit_input(self, inputs, weights):
self.vqc(inputs, weights, self.wires_input)
return [qml.expval(qml.PauliZ(wires=i)) for i in self.wires_input]
def _circuit_update(self, inputs, weights):
self.vqc(inputs, weights, self.wires_update)
return [qml.expval(qml.PauliZ(wires=i)) for i in self.wires_update]
def _circuit_output(self, inputs, weights):
self.vqc(inputs, weights, self.wires_output)
return [qml.expval(qml.PauliZ(wires=i)) for i in self.wires_output]
# 计算隐藏状态输出
def _circuit_output_h(self, inputs, weights):
self.vqc(inputs, weights, self.wires_output_h)
return [qml.expval(qml.PauliZ(wires=i)) for i in self.wires_output_h]
# 利用qiskit可视化电路图
def to_qiskit_circuit(self, inputs, weights, wires_type):
# 添加经典位用于存储测量结果
circuit = QuantumCircuit(self.n_qubits, self.n_qubits)
features = torch.tensor(inputs[:, :self.n_qubits], dtype=torch.float32)
ry_params = torch.atan2(features, torch.ones_like(features))
rz_params = torch.atan2(features ** 2, torch.ones_like(features))
# 应用Hadamard门到每个量子位上
for i in range(self.n_qubits):
circuit.h(i)
# 对量子位进行RY和RZ旋转
for i in range(self.n_qubits):
circuit.ry(ry_params[:, i].item(), i)
circuit.rz(rz_params[:, i].item(), i)
# 创建量子位之间的纠缠
for i in range(1, 3):
for j in range(self.n_qubits):
if j + i < self.n_qubits:
circuit.cx(j, j + i)
else:
circuit.cx(j, (j + i) % self.n_qubits)
# 再次对量子位进行RX、RY、RZ旋转
for layer in range(self.n_qlayers):
for i in range(self.n_qubits):
circuit.rx(weights[layer, 0, i].item(), i)
circuit.ry(weights[layer, 1, i].item(), i)
circuit.rz(weights[layer, 2, i].item(), i)
# 对每个量子位进行测量,并将结果存储到对应的经典位中
for i in range(self.n_qubits):
circuit.measure(i, i)
return circuit
def visualize_qiskit_circuit(self):
example_inputs = np.random.random((1, self.n_qubits))
example_weights = np.random.random((self.n_qlayers, self.n_vrotations, self.n_qubits))
qiskit_circuit = self.to_qiskit_circuit(example_inputs, example_weights, self.wires_forget)
circuit_drawer(qiskit_circuit, output='mpl').show()
def forward(self, x, init_states=None):
batch_size, seq_length, _ = x.size()
if init_states is None:
h_t = torch.zeros(batch_size, self.hidden_size, dtype=torch.float32) # hidden state (output)
c_t = torch.zeros(batch_size, self.hidden_size, dtype=torch.float32) # cell state
else:
h_t, c_t = init_states
# 批处理整个序列
for t in range(seq_length):
x_t = x[:, t, :] # 获取当前时间步的特征
# 选择 h_t 的最后一层,以匹配 x_t 的维度
h_t_last_layer = h_t[-1, :, :] # 假设 h_t 的形状是 [num_layers, batch_size, hidden_size]
# 现在 h_t_last_layer 的形状是 [batch_size, hidden_size],可以和 x_t 连接
combined = torch.cat((h_t_last_layer, x_t), dim=1)
# 将拼接后的数据通过一个线性层以匹配量子位的维度
combined = self.clayer_in(combined)
# 用量子层处理整个批次的数据
f_t_intermediate = self.TorchLayers['forget'](combined)
i_t_intermediate = self.TorchLayers['input'](combined)
g_t_intermediate = self.TorchLayers['update'](combined)
o_t_intermediate = self.TorchLayers['output'](combined)
# 将中间结果通过另一个线性层转换回隐藏状态的维度
f_t = torch.sigmoid(self.clayer_out_f(f_t_intermediate))
i_t = torch.sigmoid(self.clayer_out_i(i_t_intermediate))
g_t = torch.tanh(self.clayer_out_g(g_t_intermediate))
o_t = torch.sigmoid(self.clayer_out_o(o_t_intermediate))
# 根据 LSTM 的更新规则更新隐藏状态和细胞状态
c_t = f_t * c_t + i_t * g_t
h_t = o_t * torch.tanh(c_t)
# print("h_t shape:", h_t.shape)
# 使用最后的隐藏状态通过一个线性层以得到最终输出
h_t = self.dropout(h_t) # 将dropout应用到隐藏状态
output = self.fc(h_t)
# print(f"Output shape: {output.shape}")
return output
电路可视化实现 qlstm_instance = QLSTM(input_size=10, hidden_size=64, n_qubits=4, n_qlayers=1, n_vrotations=3, backend="default.qubit")
qlstm_instance.visualize_qiskit_circuit()
class QShallowClassificationLSTM(nn.Module):
def init (self, num_sensors, embedding_dim, hidden_units, n_qubits=n_qubits, n_qlayers=n_qlayers, num_classes=2, dropout_rate=dropout_rate, heads=heads):
super().init ()
self.num_sensors = num_sensors
self.hidden_units = hidden_units
self.num_layers = 1
self.num_classes = num_classes
self.embedding = nn.Embedding(vocab_size, embedding_dim)
# 添加自注意力层
self.attention = MultiHeadSelfAttention(embedding_dim, heads)
# 添加 Dropout 层
self.dropout = nn.Dropout(dropout_rate)
self.lstm = QLSTM(
# input_size=num_sensors,
# 使用embedding_dim 作为 LSTM 的输入维度
input_size=embedding_dim,
hidden_size=hidden_units,
batch_first=True,
n_qubits=n_qubits,
n_qlayers=n_qlayers,
)
self.linear = nn.Linear(in_features=2, out_features=self.num_classes)
def forward(self, x):
batch_size = x.shape[0]
x = self.embedding(x)
# print(f"Embedding layer output shape: {x.shape}")
# 应用自注意力层
attended_x = self.attention(x, x, x, None) # 注意力层没有遮罩,所以传递None
# 应用 Dropout
x = self.dropout(x)
h0 = torch.zeros(self.num_layers, batch_size, self.hidden_units)
c0 = torch.zeros(self.num_layers, batch_size, self.hidden_units)
# QLSTM 返回所有时间步的输出
outputs = self.lstm(x, (h0, c0))
# 选择最后一个时间步的隐藏状态,它的形状为 [batch_size, hidden_units]
last_hidden_state = outputs.squeeze(0)
# 将最后一个隐藏状态传递给线性层
out = self.linear(last_hidden_state)
return out
计算模型分类准确率 def calculate_accuracy_and_scores(data_loader, Qmodel):
correct = 0
total = 0
true_positives = 0
predicted_positives = 0
true_negatives = 0
predicted_negatives = 0
all_labels = []
all_predictions = []
Qmodel.eval()
with torch.no_grad():
for X, y in data_loader:
outputs = Qmodel(X)
_, predicted = torch.max(outputs.data, 1)
total += y.size(0)
# 累积条件统计数据
true_positives += (y == 1).sum().item() # 真实正类
predicted_positives += (predicted == 1).sum().item() # 预测正类
true_negatives += (y == 0).sum().item() # 真实负类
predicted_negatives += (predicted == 0).sum().item() # 预测负类
correct += (predicted == y).sum().item()
# 收集所有标签和预测值
all_labels.extend(y.cpu().numpy())
all_predictions.extend(predicted.cpu().numpy())
# 计算准确率
accuracy = 100 * correct / total
# 计算召回率
recall = recall_score(y, predicted, pos_label=1)
# 计算F1分数
f1 = f1_score(y, predicted, pos_label=1)
return accuracy, recall, f1
词汇表大小 vocab_size = len(vocabulary)
实例化分类模型和量子分类模型 Qmodel = QShallowClassificationLSTM(num_sensors=len(vocabulary),
embedding_dim=embedding_dim,
hidden_units=hidden_size,
n_qubits=n_qubits,
n_qlayers=n_qlayers,
num_classes=2)
定义损失函数和优化器 loss_function = nn.CrossEntropyLoss() # 更新为分类任务的损失函数
Qoptimizer = torch.optim.Adam(Qmodel.parameters(), lr=lr)
定义学习率调度器
scheduler = StepLR(Qoptimizer, step_size=34, gamma=0.2)
训练函数 def train_model(data_loader, Qmodel, loss_function, optimizer, progress_bar):
total_loss = 0
Qmodel.train()
for X, y in data_loader:
output = Qmodel(X)
loss = loss_function(output, y)
# 添加L2正则化
# reg_loss = weight_decay * sum(p.sum() for p in Qmodel.parameters() if p.requires_grad)
# loss += reg_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
progress_bar.update(1) # 更新进度条
avg_loss = total_loss / len(data_loader)
return avg_loss
测试函数 def test_model(data_loader, Qmodel, loss_function, progress_bar):
total_loss = 0
Qmodel.eval()
with torch.no_grad():
for X, y in data_loader:
output = Qmodel(X)
total_loss += loss_function(output, y).item()
progress_bar.update(1) # 更新进度条
avg_loss = total_loss / len(data_loader)
return avg_loss
初始化训练、测试、验证损失,验证集准确率 quantum_loss_train = []
quantum_loss_test = []
quantum_loss_val = []
quantum_accuracy_val = []
print("Quantum Untrained test\n--------")
初始化存储准确率的列表 train_accuracy_list = []
train_recall_list = []
train_f1_list = []
val_accuracy_list = []
val_recall_list = []
val_f1_list = []
test_accuracy_list = []
test_recall_list = []
test_f1_list = [] for ix_epoch in range(epoch):
# print(f"Epoch {ix_epoch+1}\n---------")
total_steps = len(train_loader) + len(val_loader)
progress_bar = tqdm(total=total_steps, desc=f"Epoch {ix_epoch + 1}")
# 训练损失
train_loss = train_model(train_loader, Qmodel, loss_function, Qoptimizer, progress_bar)
# 测试损失
# test_loss = test_model(test_loader, Qmodel, loss_function, progress_bar)
# 验证损失
val_loss = test_model(val_loader, Qmodel, loss_function, progress_bar)
# 计算量子模型的准确率、召回率和F1分数
quantum_train_accuracy, train_recall, train_f1 = calculate_accuracy_and_scores(train_loader, Qmodel)
# quantum_test_accuracy, test_recall, test_f1 = calculate_accuracy_and_scores(test_loader, Qmodel)
# 计算验证集准确率、召回率和F1分数
val_accuracy, val_recall, val_f1 = calculate_accuracy_and_scores(val_loader, Qmodel)
# 关闭进度条
progress_bar.close()
train_accuracy, train_recall, train_f1 = calculate_accuracy_and_scores(train_loader, Qmodel)
val_accuracy, val_recall, val_f1 = calculate_accuracy_and_scores(val_loader, Qmodel)
# test_accuracy = calculate_accuracy(test_loader, Qmodel)
# 将准确率添加到相应的列表中
train_accuracy_list.append(train_accuracy)
train_recall_list.append(train_recall)
train_f1_list.append(train_f1)
val_accuracy_list.append(val_accuracy)
val_recall_list.append(val_recall)
val_f1_list.append(val_f1)
# test_accuracy_list.append(test_accuracy)
# test_recall_list.append(test_recall)
# test_f1_list.append(test_f1)
# 打印损失和评估指标信息
print(f"\nTrain Loss: {train_loss:.4f}")
# print(f"Test Loss: {test_loss:.4f}")
print(f"Validation Loss: {val_loss:.4f}")
print(f"Train Accuracy: {quantum_train_accuracy:.2f}%")
# print(f"Test Accuracy: {quantum_test_accuracy:.2f}%")
print(f"Validation Accuracy: {val_accuracy:.2f}%\n")
print(f"Train Recall: {train_recall:.2f}")
# print(f"Test Recall: {test_recall:.2f}")
print(f"Train F1 Score: {train_f1:.2f}")
print(f"Validation Recall: {val_recall:.2f}")
# print(f"Test F1 Score: {test_f1:.2f}")
print(f"Validation F1 Score: {val_f1:.2f}\n")
# 记录训练、测试、验证损失
quantum_loss_train.append(train_loss)
# quantum_loss_test.append(test_loss)
quantum_loss_val.append(val_loss)
quantum_accuracy_val.append(val_accuracy)
train_eval_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=False)
绘制量子 QLSTM 模型的损失图表 plt.figure(figsize=(12, 6))
plt.plot(quantum_loss_train, label='Quantum Train Loss')
plt.plot(quantum_loss_test, label='Quantum Test Loss')
plt.plot(quantum_loss_val, label='Validation Loss')
plt.title('Quantum LSTM Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Validation Loss across Folds')
plt.xlabel('Epoch')
plt.ylabel('Validation Loss')
plt.legend()
plt.show()
绘制准确率图表 plt.figure(figsize=(12, 6))
plt.plot(train_accuracy_list, label='Train Accuracy')
plt.plot(val_accuracy_list, label='Validation Accuracy')
plt.plot(test_accuracy_list, label='Test Accuracy') plt.plot(train_recall_list, label='Train Recall')
plt.plot(val_recall_list, label='Validation Recall')
plt.plot(test_recall_list, label='Test Recall') plt.plot(train_f1_list, label='Train F1 Score')
plt.plot(val_f1_list, label='Validation F1 Score')
plt.plot(test_f1_list, label='Test F1 Score')
模型性能指标 plt.title('Model Evaluation Metrics Across Epochs')
plt.xlabel('Epoch')
plt.ylabel('Metrics')
plt.legend()
plt.show()
这是我的整体模型,其中我所使用的数据集总数为1000条,其中训练集验证集测试集按照8:1:1的比例划分。并且每个句子在经过数据预处理步骤后我设置的平均长度为8,请你结合我的数据集信息来给出一个更合理的参数组合以达到更好的效果
好的,根据您提供的信息,我们可以从多个方面入手,调整模型的超参数以提高性能。以下是对模型参数的详细建议:
1. 调整序列长度(sequence_length)
建议: 增加 sequence_length,例如设置为 20 或 50。
原因: 您目前将序列长度设置为 8,这可能过短,可能导致模型无法捕获句子中的完整信息,尤其是在处理情感分析任务时。尽管您说经过预处理后句子的平均长度为 8,但仍建议增加 sequence_length,因为这样可以捕获更多的上下文信息,从而提高模型的性能。
实现方法:
sequence_length = 20 # 或者更高,例如50
在数据预处理的填充和截断部分,确保相应地调整序列长度。
2. 调整批次大小(batch_size)
建议: 减小 batch_size,例如设置为 16 或 8。
原因: 对于小型数据集,使用较小的批次大小可以使模型更好地学习数据的细微差别。较小的批次大小可以增加参数更新的频率,可能有助于更快地收敛。
实现方法:
batch_size = 16 # 或者更小,如8
确保在创建 DataLoader 时使用新的 batch_size。
3. 调整学习率(lr)
建议: 增大学习率,例如设置为 0.01。
原因: 增大学习率可以使模型更快地找到优化方向,特别是在数据量较小的情况下。不过,需要注意的是,学习率过大会导致训练不稳定,因此需要在实验中找到合适的平衡点。
实现方法:
lr = 0.01
4. 调整隐藏层大小(hidden_size)
建议: 增大 hidden_size,例如设置为 128 或 256。
原因: 增加隐藏层的神经元数量可以提高模型的表示能力,使其能够学习更复杂的模式。这对于小型数据集可能特别有用,因为模型需要充分利用有限的数据来学习。
实现方法:
hidden_size = 128 # 或者256
5. 调整 Dropout 率(dropout_rate)
建议: 减小 dropout_rate,例如设置为 0.2 或 0.1。
原因: 在小型数据集上,过高的 Dropout 率可能会导致模型欠拟合,因为过多的神经元被随机丢弃。适当降低 Dropout 率可以让模型更充分地学习数据。
实现方法:
dropout_rate = 0.2 # 或者0.1
6. 调整量子层数(n_qlayers)
建议: 减小 n_qlayers,例如设置为 1。
原因: 量子电路越深,参数越多,可能导致模型过拟合或训练不稳定。对于小型数据集,适当减少量子层数可以简化模型,减少过拟合的风险。
实现方法:
n_qlayers = 1
7. 调整量子比特数(n_qubits)
建议: 适当减小 n_qubits,例如设置为 4。
原因: 减少量子比特数可以降低量子电路的复杂度,加快训练速度,同时防止模型过拟合。
实现方法:
n_qubits = 4
8. 使用预训练词嵌入
建议: 使用预训练的词向量,如 GloVe 或 word2vec。
原因: 预训练的词嵌入能够捕获词语之间丰富的语义关系,对于小型数据集,使用预训练的词向量可以显著提高模型的性能。
实现方法:
# 下载预训练的 GloVe 词向量
!wget http://nlp.stanford.edu/data/glove.6B.zip
!unzip glove.6B.zip
# 加载 GloVe 词向量
def load_glove_embeddings(glove_file):
embeddings_index = {}
with open(glove_file, encoding='utf-8') as f:
for line in f:
values = line.split()
word = ''.join(values[:-embedding_dim])
coefs = np.asarray(values[-embedding_dim:], dtype='float32')
embeddings_index[word] = coefs
return embeddings_index
glove_file = 'glove.6B.100d.txt' # 选择适当的维度
embeddings_index = load_glove_embeddings(glove_file)
# 创建嵌入矩阵
embedding_matrix = np.zeros((vocab_size, embedding_dim))
for word, i in vocabulary.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
# 在模型中使用预训练的嵌入矩阵
class QShallowClassificationLSTM(nn.Module):
def __init__(self, ...):
...
self.embedding = nn.Embedding.from_pretrained(torch.FloatTensor(embedding_matrix), freeze=False)
...
9. 增加训练轮数(epoch)
建议: 增加训练轮数,例如设置为 100。
原因: 对于小型数据集,多训练几轮可以使模型更充分地学习数据中的模式。
实现方法:
epoch = 100
10. 使用早停法(Early Stopping)
建议: 在训练过程中监控验证集的损失或准确率,若在若干轮内没有提升,则停止训练。
原因: 防止模型过拟合,同时节省训练时间。
实现方法:
# 在训练循环中添加早停逻辑
patience = 10 # 可以根据需要调整
best_val_loss = float('inf')
epochs_no_improve = 0
for ix_epoch in range(epoch):
...
if val_loss < best_val_loss:
best_val_loss = val_loss
epochs_no_improve = 0
# 保存模型
torch.save(Qmodel.state_dict(), 'best_model.pth')
else:
epochs_no_improve += 1
if epochs_no_improve == patience:
print('Early stopping!')
break
11. 调整优化器和学习率调度器
建议: 尝试使用不同的优化器和学习率调度器,例如使用 AdamW 优化器和余弦退火学习率调度器。
原因: 不同的优化器和学习率调度策略可能会对模型的收敛速度和性能产生影响。
实现方法:
from torch.optim import AdamW
from torch.optim.lr_scheduler import CosineAnnealingLR
Qoptimizer = AdamW(Qmodel.parameters(), lr=lr)
scheduler = CosineAnnealingLR(Qoptimizer, T_max=10)
在训练循环中,添加 scheduler.step()。
12. 增加正则化项
建议: 添加权重衰减(L2 正则化)或其他正则化方法。
原因: 正则化可以防止模型过拟合,提高泛化能力。
实现方法:
weight_decay = 1e-5
Qoptimizer = torch.optim.Adam(Qmodel.parameters(), lr=lr, weight_decay=weight_decay)
13. 数据增强
建议: 对文本数据进行数据增强,例如同义词替换、随机删除等。
原因: 数据增强可以扩大数据集规模,使模型能够学习到更多的模式。
实现方法: 可以使用 nlpaug 库进行文本数据增强。
# 安装 nlpaug
!pip install nlpaug
import nlpaug.augmenter.word as naw
# 创建数据增强器
aug = naw.SynonymAug(aug_src='wordnet')
# 对训练集进行数据增强
augmented_texts = []
augmented_labels = []
for text, label in zip(train_set['text'], train_set['label']):
augmented_text = aug.augment(text)
augmented_texts.append(augmented_text)
augmented_labels.append(label)
# 将增强后的数据添加到训练集中
train_set_aug = pd.DataFrame({'text': augmented_texts, 'label': augmented_labels})
train_set = pd.concat([train_set, train_set_aug])
14. 交叉验证
建议: 使用交叉验证来评估模型的性能,并根据结果调整超参数。
原因: 交叉验证可以更稳定地评估模型性能,减少偶然因素的影响。
实现方法:
from sklearn.model_selection import KFold
kf = KFold(n_splits=5, shuffle=True, random_state=seed)
for train_index, val_index in kf.split(dataset):
train_set = dataset.iloc[train_index]
val_set = dataset.iloc[val_index]
# 继续数据预处理和模型训练
15. 调整自注意力头数(heads)
建议: 减少多头注意力的头数,例如设置为 2。
原因: 头数过多可能导致模型复杂度过高,降低头数可以简化模型,减少过拟合的风险。
实现方法:
heads = 2
16. 简化模型架构
建议: 在模型中暂时移除或简化一些组件,例如自注意力层,专注于核心的 LSTM 模块,以减少模型复杂度。
原因: 过于复杂的模型在小数据集上容易过拟合,先使模型简化,找到基本的有效架构,再逐步添加组件。
实现方法:
# 可以暂时注释掉自注意力层相关的代码
# 在 QShallowClassificationLSTM 类中
def forward(self, x):
batch_size = x.shape[0]
x = self.embedding(x)
# 暂时移除自注意力层
# attended_x = self.attention(x, x, x, None)
# x = self.dropout(x)
h0 = torch.zeros(self.num_layers, batch_size, self.hidden_units)
c0 = torch.zeros(self.num_layers, batch_size, self.hidden_units)
outputs = self.lstm(x, (h0, c0))
last_hidden_state = outputs.squeeze(0)
out = self.linear(last_hidden_state)
return out
17. 检查数据预处理
建议: 确保数据预处理步骤,如标记化、去停用词、词汇表建立等都正确无误。还可以考虑保留停用词,因为它们有时对情感分析有用。
原因: 数据预处理的质量直接影响模型的性能,错误或不当的预处理可能导致信息丢失。
实现方法:
# 在 clean_text 函数中,考虑是否需要移除停用词
def clean_text(text):
text = text.lower()
text = re.sub(r'<.*?>', '', text)
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
text = re.sub(r'\d+', '', text)
text = re.sub(r'[^\w\s]', '', text)
words = word_tokenize(text)
# 选择性地保留停用词
# stop_words = set(stopwords.words('english'))
# words = [word for word in words if word not in stop_words]
return ' '.join(words)
18. 更新词汇表频次阈值(min_freq)
建议: 增大 min_freq 的值,例如设置为 2 或 3。
原因: 过滤掉出现频次过低的词,可以减少噪音,提高模型的学习效率。
实现方法:
min_freq = 2 # 或者3
19. 增加模型的随机性
建议: 多次运行实验,改变随机种子,以确保结果的稳定性。
原因: 小数据集上的模型训练结果可能受随机性影响,多个随机种子下的实验可以帮助验证结果的可靠性。
实现方法:
# 更改随机种子
seed = 123 # 或其他值
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
20. 日志记录与可视化
建议: 使用 TensorBoard 或其他工具记录训练过程中的指标,以便更直观地观察模型的性能变化。
原因: 更好地监控训练过程,发现可能的问题。
实现方法:
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
# 在训练循环中
writer.add_scalar('Loss/train', train_loss, ix_epoch)
writer.add_scalar('Loss/val', val_loss, ix_epoch)
writer.add_scalar('Accuracy/val', val_accuracy, ix_epoch)
# 训练结束后
writer.close()
以上建议可以结合起来,通过实验逐步调整超参数,找到最佳的参数组合。需要注意的是,由于量子计算模拟的计算量较大,训练时间可能会较长。可以先在经典模型上尝试调整参数,取得较好的基线结果后,再在量子模型上进行微调。
希望这些建议能帮助您提高模型的性能!
Copy first response
Not yet included in a published release.