diff --git a/Qfunctions/saveToxlsx.py b/Qfunctions/saveToxlsx.py index 9f8e5b4..efacddb 100644 --- a/Qfunctions/saveToxlsx.py +++ b/Qfunctions/saveToxlsx.py @@ -5,9 +5,4 @@ def save_to_xlsx(project_name, file_name, data): data.to_excel(f'Result/{project_name}/{file_name}.xlsx', index=True) print("Save successed to " + f'Result/{project_name}/{file_name}.xlsx') - - # for filename,data in save_maps.items(): - # data.to_excel(f'Result/{project_name}/{filename}.xlsx', index=True) - # print("Save successed to " + f'Result/{project_name}/{filename}.xlsx') - # print('Save to xlsx done!') return diff --git a/Qfunctions/test.py b/Qfunctions/test.py deleted file mode 100644 index d4c26dd..0000000 --- a/Qfunctions/test.py +++ /dev/null @@ -1,17 +0,0 @@ -import time -from sklearn.neural_network import MLPClassifier -from sklearn.metrics import classification_report, accuracy_score - -def MLP(X_train, X_test, y_train, y_test): - start_time = time.time() - # 训练 MLP 分类器 - mlp = MLPClassifier(hidden_layer_sizes=(100,), max_iter=300, random_state=42) - mlp.fit(X_train, y_train) - y_pred = mlp.predict(X_test) - end_time = time.time() - # 打印训练时间 - print("Training Time:", end_time - start_time, "seconds") - # 评估模型 - print("Accuracy:", accuracy_score(y_test, y_pred)) - print("Classification Report:") - print(classification_report(y_test, y_pred)) \ No newline at end of file diff --git a/Qtorch/Models/QSVM.py b/Qtorch/Models/QSVM.py deleted file mode 100644 index b0c5b35..0000000 --- a/Qtorch/Models/QSVM.py +++ /dev/null @@ -1,14 +0,0 @@ -from Qtorch.Models.Qnn import Qnn -from abc import ABC, abstractmethod - -class QSVM(Qnn, ABC): - def __init__(self, data, labels=None, test_size=0.2, random_state=None): - super().__init__(data, labels, test_size, random_state) - self.result.update({ - "pca_2d" : None, - "pca_3d" : None - }) - - @abstractmethod - def train_model(self, train_loader, test_loader, epochs): - return super().train_model(train_loader, test_loader, epochs) diff --git a/Qtorch/Models/QSVM_BRF.py b/Qtorch/Models/QSVM_BRF.py deleted file mode 100644 index 9e42baf..0000000 --- a/Qtorch/Models/QSVM_BRF.py +++ /dev/null @@ -1,74 +0,0 @@ -import torch -import torch.nn as nn -import torch.optim as optim - -from Qtorch.Models.QSVM import QSVM as svm - -class QSVM_BRF(svm): - def __init__(self, data, labels=None, test_size=0.2, random_state=None, - gamma=1.0, C=100, batch_size = 64, learning_rate=0.01): - super().__init__(data, labels, test_size, random_state) - self.gamma, self.C, self.n_features = gamma, C, data.shape[0] - 1 - self.support_vectors = torch.cat([batch[0] for batch in self.train_loader]) - self.alpha = nn.Parameter(torch.zeros(self.support_vectors.shape[0])) - self.b = nn.Parameter(torch.zeros(1)) - self.batch_size = batch_size - self.learning_rate = learning_rate - self.optimizer = optim.SGD(self.parameters(), lr=self.learning_rate) - - def rbf_kernel(self, X, Y): - X_norm = (X**2).sum(1).view(-1, 1) - Y_norm = (Y**2).sum(1).view(1, -1) - dist = X_norm + Y_norm - 2.0 * torch.mm(X, Y.t()) - return torch.exp(-self.gamma * dist) - - def forward(self, X): - K = self.rbf_kernel(X, self.support_vectors) - return torch.mm(K, self.alpha.unsqueeze(1)).squeeze() + self.b - - def hinge_loss(self, outputs, targets): - return torch.mean(torch.clamp(1 - outputs * targets, min=0)) - - def regularization(self): - return 0.5 * (self.alpha ** 2).sum() - - def train_model(self, epoch_times=100, learning_rate=0.01): - - losses, train_accs, test_accs = [], [], [] - - for epoch in range(epoch_times): - self.train() - epoch_loss, correct_train, total_train = 0, 0, 0 - - for batch_X, batch_y in self.train_loader: - - self.optimizer.zero_grad() - outputs = self(batch_X) - loss = self.hinge_loss(outputs, batch_y) + self.C * self.regularization() - loss.backward() - self.optimizer.step() - - epoch_loss += loss.item() - predicted = torch.sign(outputs) - correct_train += (predicted == batch_y).sum().item() - total_train += batch_y.size(0) - - train_acc = correct_train / total_train - test_acc = self.evaluate() - - losses.append(epoch_loss / len(self.train_loader)) - train_accs.append(train_acc) - test_accs.append(test_acc) - print(f'Epoch [{epoch+1}/{epoch_times}], Loss: {losses[-1]:.4f}, Train Acc: {train_acc:.4f}, Test Acc: {test_acc:.4f}') - - def evaluate(self): - self.eval() - correct = 0 - total = 0 - with torch.no_grad(): - for batch_X, batch_y in self.test_loader: - outputs = self(batch_X) - predicted = torch.sign(outputs) - correct += (predicted == batch_y).sum().item() - total += batch_y.size(0) - return correct / total diff --git a/SVM b/SVM deleted file mode 160000 index 3aec5f2..0000000 --- a/SVM +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3aec5f294e817679e61b0833d4f750b9f118cfbc diff --git a/catplus b/Scripts/catplus similarity index 100% rename from catplus rename to Scripts/catplus diff --git a/test.py b/Scripts/test.py similarity index 100% rename from test.py rename to Scripts/test.py diff --git a/main.py b/main.py index c174de5..6d62518 100644 --- a/main.py +++ b/main.py @@ -33,100 +33,6 @@ def main(): df_pca2d.to_excel(os.path.join(folder, 'pca_2d_points_with_labels.xlsx'), index=False) df_pca3d.to_excel(os.path.join(folder, 'pca_3d_points_with_labels.xlsx'), index=False) - """ clf = SVC(kernel='rbf', C=1.0) - from sklearn.model_selection import RandomizedSearchCV - from scipy.stats import uniform - - # 定义参数分布 - param_dist = {'C': uniform(0.1, 100)} - - # 随机搜索 - random_search = RandomizedSearchCV(SVC(kernel='rbf'), param_distributions=param_dist, - n_iter=20, cv=5, scoring='accuracy') - random_search.fit(X_train, y_train) - - # 获取最佳参数和模型 - best_C = random_search.best_params_['C'] - best_model = random_search.best_estimator_ - - # 评估 - y_pred = best_model.predict(X_test) - from sklearn.metrics import accuracy_score, confusion_matrix - accuracy = accuracy_score(y_test, y_pred) - - print(f"Best C: {best_C}") - print(f"Best accuracy: {accuracy}" )""" - - """ - from sklearn import svm - from sklearn.model_selection import train_test_split, learning_curve, cross_val_score - import numpy as np - import matplotlib.pyplot as plt - import seaborn as sns - - # 进行交叉验证 - cv_scores = cross_val_score(clf, X_train, y_train, cv=5) - print(f"Cross-validation scores: {cv_scores}") - print(f"Mean CV score: {cv_scores.mean():.3f} (+/- {cv_scores.std() * 2:.3f})") - - - # 计算学习曲线 - train_sizes, train_scores, test_scores = learning_curve( - clf, X_train, y_train, cv=5, n_jobs=-1, - train_sizes=np.linspace(.1, 1.0, 5)) - - train_scores_mean = np.mean(train_scores, axis=1) - train_scores_std = np.std(train_scores, axis=1) - test_scores_mean = np.mean(test_scores, axis=1) - test_scores_std = np.std(test_scores, axis=1) - - # 绘制学习曲线 - plt.figure(figsize=(10, 6)) - plt.title("Learning Curve") - plt.xlabel("Training examples") - plt.ylabel("Score") - plt.grid() - plt.fill_between(train_sizes, train_scores_mean - train_scores_std, - train_scores_mean + train_scores_std, alpha=0.1, color="r") - plt.fill_between(train_sizes, test_scores_mean - test_scores_std, - test_scores_mean + test_scores_std, alpha=0.1, color="g") - plt.plot(train_sizes, train_scores_mean, 'o-', color="r", label="Training score") - plt.plot(train_sizes, test_scores_mean, 'o-', color="g", label="Cross-validation score") - plt.legend(loc="best") - plt.show() - - # 在全部训练数据上训练模型 - clf.fit(X_train, y_train) - - # 预测 - y_pred = clf.predict(X_test) - - # 计算准确率 - accuracy = accuracy_score(y_test, y_pred) - print(f"Test Accuracy: {accuracy:.3f}") - - # 计算归一化的混淆矩阵 - cm = confusion_matrix(y_test, y_pred, normalize='true') - - # 绘制混淆矩阵 - plt.figure(figsize=(10,7)) - sns.heatmap(cm, annot=True, fmt='.2f', cmap='Blues') - plt.title('Normalized Confusion Matrix') - plt.ylabel('True label') - plt.xlabel('Predicted label') - plt.show() """ - - """ - model.fit(X_train, y_train) - y_pred = model.predict(X_test) - from sklearn.metrics import accuracy_score, confusion_matrix - accuracy = accuracy_score(y_test, y_pred) - print(f"Accuracy: {accuracy}") - - # 计算归一化的混淆矩阵 - cm = confusion_matrix(y_test, y_pred, normalize='true') - print(cm) """ - model = Qmlp( X_train=X_train, X_test=X_test, y_train=y_train, y_test= y_test, @@ -146,138 +52,3 @@ def main(): if __name__ == '__main__': main() - - - - -# from sklearn.model_selection import train_test_split -# from sklearn.preprocessing import StandardScaler -# from sklearn.svm import SVC -# from sklearn.model_selection import GridSearchCV -# from sklearn.metrics import accuracy_score, classification_report, confusion_matrix -# import pandas as pd - -# if __name__ == '__main__': - -# project_name = '20240829Letters' -# labels = None - -# data = ld(project_name, labels) - - - - - # svm = SVM( - # data=data, - # labels=labels - # ) - - # svm.fit() - - # X, y = data.iloc[:, :-1], data.iloc[:, -1] - - # # 分割数据 - # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=None) - - # # 标准化数据 - # scaler = StandardScaler() - # X_train_scaled = scaler.fit_transform(X_train) - # X_test_scaled = scaler.transform(X_test) - - # # 创建 SVM 分类器 - # svm = SVC(kernel='rbf', random_state=42) - - # # 定义参数网格 - # param_grid = { - # 'C': [0.1, 1, 10, 100], - # 'gamma': ['scale', 'auto', 0.1, 1, 10] - # } - - # # 使用网格搜索进行超参数调优 - # grid_search = GridSearchCV(svm, param_grid, cv=5, n_jobs=-1, verbose=2) - # grid_search.fit(X_train_scaled, y_train) - - # # 打印最佳参数 - # print("Best parameters:", grid_search.best_params_) - - # # 使用最佳参数的模型 - # best_svm = grid_search.best_estimator_ - - # # 计算训练集和测试集准确率 - # y_train_pred = best_svm.predict(X_train_scaled) - # train_acc = accuracy_score(y_train, y_train_pred) - - # y_test_pred = best_svm.predict(X_test_scaled) - # test_acc = accuracy_score(y_test, y_test_pred) - - # # 在测试集上进行预测 - # y_pred = best_svm.predict(X_test_scaled) - - # # 计算准确率 - # accuracy = accuracy_score(y_test, y_pred) - # print(f"Accuracy: {accuracy}") - - # # 打印详细的分类报告 - # print(classification_report(y_test, y_pred)) - # # 计算并可视化混淆矩阵 - # cm = confusion_matrix(y_test, y_test_pred, normalize='true') - - # print(cm) - # # model = QSVM( - # # data=data, - # # labels=labels - # # ) - - # # model.fit(300) - # # model.save(project_name) - - - # # 创建一个 Excel 写入器 - # # 将分类报告转换为DataFrame - # # 获取分类报告 - - # report = classification_report(y_test, y_test_pred, output_dict=True) - - # df_report = pd.DataFrame(report).transpose() - # with pd.ExcelWriter(f'./Result/{project_name}/svm_results.xlsx') as writer: - # from sklearn.decomposition import PCA - # pca = PCA() - # X_pca = pca.fit_transform(X) - # # 创建 2D PCA 坐标的 DataFrame - # df_pca_2d = pd.DataFrame(data = X_pca[:, :2], columns = ['First Principal Component', 'Second Principal Component']) - # df_pca_2d['Label'] = y - # # 创建 3D PCA 坐标的 DataFrame - # df_pca_3d = pd.DataFrame(data = X_pca[:, :3], columns = ['First Principal Component', 'Second Principal Component', 'Third Principal Component']) - # df_pca_3d['Label'] = y - - # # 将 2D PCA 坐标写入 Excel - # df_pca_2d.to_excel(writer, sheet_name='PCA 2D Coordinates', index=False) - # df_pca_3d.to_excel(writer, sheet_name='PCA 3D Coordinates', index=False) - - - # # 将分类报告写入Excel - # df_report.to_excel(writer, sheet_name='Classification Report') - - # # 将最佳参数写入Excel - # pd.DataFrame([grid_search.best_params_]).to_excel(writer, sheet_name='Best Parameters') - - # # 如果你想保存混淆矩阵 - # from sklearn.metrics import confusion_matrix - # # 创建混淆矩阵并添加标签 - # cm = confusion_matrix(y_test, y_test_pred, normalize='true') - # df_cm = pd.DataFrame(cm, index=labels, columns=labels) - # df_cm.index.name = 'True' - # df_cm.columns.name = 'Predicted' - - # # 将混淆矩阵写入Excel - # df_cm.to_excel(writer, sheet_name='Confusion Matrix') - - # # 如果你想保存训练集和测试集的准确率 - # train_accuracy = best_svm.score(X_train_scaled, y_train) - # test_accuracy = best_svm.score(X_test_scaled, y_test) - # pd.DataFrame({ - # 'Train Accuracy': [train_accuracy], - # 'Test Accuracy': [test_accuracy] - # }).to_excel(writer, sheet_name='Accuracy') - - # print("Results have been saved to 'svm_results.xlsx'") \ No newline at end of file diff --git a/remake/.vscode/launch.json b/remake/.vscode/launch.json deleted file mode 100644 index 14b7790..0000000 --- a/remake/.vscode/launch.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Python Debugger: Current File", - "type": "debugpy", - "request": "launch", - "program": "main.py", - "console": "integratedTerminal" - } - ] -} \ No newline at end of file diff --git a/remake/Qtorch/Functions/__init__.py b/remake/Qtorch/Functions/__init__.py deleted file mode 100644 index e2eb142..0000000 --- a/remake/Qtorch/Functions/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from .dataLoader import dLoader -from .dataSplitter import dsplit -from .resSaver import save_to_xlsx - -__all__ = ['dLoader', 'dsplit', 'save_to_xlsx'] \ No newline at end of file diff --git a/remake/Qtorch/Functions/__pycache__/__init__.cpython-312.pyc b/remake/Qtorch/Functions/__pycache__/__init__.cpython-312.pyc deleted file mode 100644 index 3412e26..0000000 Binary files a/remake/Qtorch/Functions/__pycache__/__init__.cpython-312.pyc and /dev/null differ diff --git a/remake/Qtorch/Functions/__pycache__/dataLoader.cpython-312.pyc b/remake/Qtorch/Functions/__pycache__/dataLoader.cpython-312.pyc deleted file mode 100644 index 257c78d..0000000 Binary files a/remake/Qtorch/Functions/__pycache__/dataLoader.cpython-312.pyc and /dev/null differ diff --git a/remake/Qtorch/Functions/__pycache__/dataSplitter.cpython-312.pyc b/remake/Qtorch/Functions/__pycache__/dataSplitter.cpython-312.pyc deleted file mode 100644 index 7e7e830..0000000 Binary files a/remake/Qtorch/Functions/__pycache__/dataSplitter.cpython-312.pyc and /dev/null differ diff --git a/remake/Qtorch/Functions/__pycache__/resSaver.cpython-312.pyc b/remake/Qtorch/Functions/__pycache__/resSaver.cpython-312.pyc deleted file mode 100644 index 55919bb..0000000 Binary files a/remake/Qtorch/Functions/__pycache__/resSaver.cpython-312.pyc and /dev/null differ diff --git a/remake/Qtorch/Functions/dataLoader.py b/remake/Qtorch/Functions/dataLoader.py deleted file mode 100644 index 090d3cf..0000000 --- a/remake/Qtorch/Functions/dataLoader.py +++ /dev/null @@ -1,91 +0,0 @@ -import os -import pandas as pd - -STATIC_PATH = './Static' - -def dLoader(folder, label_names=None): - - """ - Load data from Excel files in a specified folder. - - Args: - folder (str): Name of the folder containing Excel files. - label_names (list): Optional list of label names. If not provided, file names will be used. - - Returns: - pandas.DataFrame: Loaded and processed data. - """ - - folder_path = os.path.join(STATIC_PATH, folder) - file_names = [f for f in os.listdir(folder_path) if f.endswith('.xlsx')] - - - if not label_names: - label_names = [f.split('.')[0] for f in file_names] - - max_row_length = get_max_row_len(folder_path, file_names) - - all_features = [] - for i, file_name in enumerate(file_names): - features = load_xlsx(os.path.join(folder_path, file_name), label_names[i], max_row_length) - all_features.append(features) - - return pd.concat(all_features, ignore_index=True) - -def load_xlsx(file_name, label_name, max_row_length, fill_rule='mean'): - """ - Load and process data from a single Excel file. - - Args: - file_name (str): Path to the Excel file. - label_name (str): Label for the data in this file. - max_row_length (int): Maximum number of rows to consider. - fill_rule (str): Rule for filling missing values ('min', 'mean', or None). - - Returns: - pandas.DataFrame: Processed data from the Excel file. - """ - df = pd.read_excel(file_name) - features = df.iloc[0:, 1::2] - features.dropna(inplace=True) - features.reset_index(drop=True, inplace=True) - features = features.T - features = features.apply(lambda row: fill_to_len(row, max_row_length, fill_rule), axis=1) - - features['label'] = label_name - features.columns = [f'feature{i+1}' for i in range(max_row_length)] + ['label'] - - return features - -def fill_to_len(row, length=1000, rule=None): - """ - Fill a row to a specified length. - - Args: - row (pandas.Series): Row to fill. - length (int): Desired length of the row. - rule (str): Rule for filling ('min', 'mean', or None). - - Returns: - pandas.Series: Filled row. - """ - fill_value = 0 - if rule == 'min': - fill_value = row.min() - elif rule == 'mean': - fill_value = row.mean() - fill_values = pd.Series([fill_value] * (length - len(row))) - return pd.concat([row, fill_values], ignore_index=True) - -def get_max_row_len(folder, filenames): - """ - Get the maximum row length across all Excel files in a folder. - - Args: - folder (str): Path to the folder containing Excel files. - filenames (list): List of Excel file names. - - Returns: - int: Maximum row length. - """ - return max(pd.read_excel(os.path.join(folder, filename)).shape[0] for filename in filenames) \ No newline at end of file diff --git a/remake/Qtorch/Functions/dataSplitter.py b/remake/Qtorch/Functions/dataSplitter.py deleted file mode 100644 index ef9e421..0000000 --- a/remake/Qtorch/Functions/dataSplitter.py +++ /dev/null @@ -1,37 +0,0 @@ -from sklearn.model_selection import train_test_split -from sklearn.preprocessing import StandardScaler, LabelEncoder - -def dsplit(data, labels=None, test_size=0.2, random_state=None): - """ - Split the dataset into training and testing sets. - - Args: - data (pandas.DataFrame): Input data. - labels (list): Optional list of labels. - test_size (float): Proportion of the dataset to include in the test split. - random_state (int): Random state for reproducibility. - - Returns: - tuple: X_train, X_test, y_train, y_test, encoded_labels - """ - encoder = LabelEncoder() - - X = data.iloc[:, :-1] - y = data.iloc[:, -1] - - if labels is not None: - encoded_labels = encoder.fit_transform(labels) - else: - encoder.fit(y) - encoded_labels = None - - X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state) - - scaler = StandardScaler() - X_train = scaler.fit_transform(X_train) - X_test = scaler.transform(X_test) - - y_train = encoder.transform(y_train.values.ravel()) - y_test = encoder.transform(y_test.values.ravel()) - - return X_train, X_test, y_train, y_test, encoded_labels \ No newline at end of file diff --git a/remake/Qtorch/Functions/resSaver.py b/remake/Qtorch/Functions/resSaver.py deleted file mode 100644 index 2713e36..0000000 --- a/remake/Qtorch/Functions/resSaver.py +++ /dev/null @@ -1,15 +0,0 @@ -import os - -def save_to_xlsx(project_name, file_name, data): - """ - Save data to an Excel file. - - Args: - project_name (str): Name of the project (used for folder name). - file_name (str): Name of the file to save. - data (pandas.DataFrame): Data to save. - """ - os.makedirs(f'Result/{project_name}', exist_ok=True) - file_path = f'Result/{project_name}/{file_name}.xlsx' - data.to_excel(file_path, index=True) - print(f"Data saved successfully to {file_path}") \ No newline at end of file diff --git a/remake/Qtorch/__init__.py b/remake/Qtorch/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/remake/Qtorch/__pycache__/__init__.cpython-312.pyc b/remake/Qtorch/__pycache__/__init__.cpython-312.pyc deleted file mode 100644 index 656b4ef..0000000 Binary files a/remake/Qtorch/__pycache__/__init__.cpython-312.pyc and /dev/null differ diff --git a/remake/Qtorch/models/QSVM.py b/remake/Qtorch/models/QSVM.py deleted file mode 100644 index 73e22c8..0000000 --- a/remake/Qtorch/models/QSVM.py +++ /dev/null @@ -1,21 +0,0 @@ -from .Qnn import Qnn - -class QSVM(Qnn): - def __init__(self, data, labels=None, test_size=0.2, random_state=None): - super(QSVM, self).__init__(data, labels, test_size, random_state) - self.result.update({ - "pca_2d": None, - "pca_3d": None - }) - - def forward(self, x): - # Implement SVM forward pass - pass - - def train_model(self, epochs): - # Implement SVM training logic - pass - - def hinge_loss(self, output, target): - # Implement hinge loss - pass \ No newline at end of file diff --git a/remake/Qtorch/models/Qnn.py b/remake/Qtorch/models/Qnn.py deleted file mode 100644 index dddb709..0000000 --- a/remake/Qtorch/models/Qnn.py +++ /dev/null @@ -1,72 +0,0 @@ -import torch -import torch.nn as nn -import pandas as pd -from abc import ABC, abstractmethod -from Qtorch.Functions import dsplit -from Qtorch.Functions import save_to_xlsx as stx -# from sklearn.metrics import confusion_matrix - -class Qnn(nn.Module, ABC): - def __init__(self, data, labels=None, test_size=0.2, random_state=None): - super(Qnn, self).__init__() - self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') - self.original_labels = labels - - # Split data - self.X_train, self.X_test, self.y_train, self.y_test, self.labels = dsplit( - data=data, - labels=labels, - test_size=test_size, - random_state=random_state - ) - - self.train_loader, self.test_loader = self._prepare_data() - - self.result = { - 'acc_and_loss': { - 'epoch': [], - 'loss': [], - 'train_accuracy': [], - 'test_accuracy': [], - }, - 'confusion_matrix': None, - } - - @abstractmethod - def forward(self, x): - pass - - @abstractmethod - def train_model(self, epochs): - pass - - def fit(self, epochs=100): - self.train_model(epochs) - - def save(self, project_name): - for filename, data in self.result.items(): - if filename == 'confusion_matrix': - data = pd.DataFrame(data, columns=self.original_labels, index=self.original_labels) - else: - data = pd.DataFrame(data) - stx(project_name, filename, data) - - def _prepare_data(self): - X_train_tensor = torch.tensor(self.X_train, dtype=torch.float32) - y_train_tensor = torch.tensor(self.y_train, dtype=torch.long) - - X_test_tensor = torch.tensor(self.X_test, dtype=torch.float32) - y_test_tensor = torch.tensor(self.y_test, dtype=torch.long) - - train_dataset = torch.utils.data.TensorDataset(X_train_tensor, y_train_tensor) - test_dataset = torch.utils.data.TensorDataset(X_test_tensor, y_test_tensor) - - train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True) - test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False) - - return train_loader, test_loader - - # def confusion_matrix(self, test_outputs): - # predicted = torch.argmax(test_outputs, dim=1) - # true_label = torch.argmax(self.y_test, dim=1) - # return confusion_matrix(predicted.cpu(), true_label.cpu()) \ No newline at end of file diff --git a/remake/Qtorch/models/Qsvm_brf.py b/remake/Qtorch/models/Qsvm_brf.py deleted file mode 100644 index d94474b..0000000 --- a/remake/Qtorch/models/Qsvm_brf.py +++ /dev/null @@ -1,114 +0,0 @@ -import torch -import torch.nn as nn -import torch.optim as optim -from tqdm import tqdm -from .QSVM import QSVM -from sklearn.metrics import confusion_matrix - -class Qsvm_brf(QSVM): - def __init__(self, data, labels=None, test_size=0.2, random_state=None, - gamma=1.0, C=100, batch_size=64, learning_rate=0.01): - super(Qsvm_brf, self).__init__(data, labels, test_size, random_state) - self.to(self.device) - self.gamma = gamma - self.C = C - self.n_features = data.shape[1] - 1 - self.support_vectors = torch.cat([batch[0] for batch in self.train_loader]).to(self.device) - self.alpha = nn.Parameter(torch.zeros(self.support_vectors.shape[0])).to(self.device) - self.b = nn.Parameter(torch.zeros(1)).to(self.device) - self.batch_size = batch_size - self.learning_rate = learning_rate - print(self.b, self.alpha) - print(list(self.parameters())) - - def train_model(self, epochs): - self.to(self.device) - self.optimizer = optim.SGD(self.parameters(), lr=self.learning_rate) - - for epoch in range(epochs): - self.train() - total_loss = 0 - correct = 0 - total = 0 - - progress_bar = tqdm(self.train_loader, desc=f'Epoch {epoch+1}/{epochs}') - for batch_X, batch_y in progress_bar: - batch_X, batch_y = batch_X.to(self.device), batch_y.to(self.device) - - self.optimizer.zero_grad() - - outputs = self(batch_X) - loss = self.hinge_loss(outputs, batch_y) + self.C * self.regularization() - - loss.backward() - self.optimizer.step() - - total_loss += loss.item() - predicted = torch.sign(outputs) - correct += (predicted == batch_y).sum().item() - total += batch_y.size(0) - - progress_bar.set_postfix({ - 'Loss': total_loss / (progress_bar.n + 1), - 'Acc': 100. * correct / total - }) - - train_accuracy = correct / total - test_accuracy = self.evaluate() - - self.result['acc_and_loss']['epoch'].append(epoch + 1) - self.result['acc_and_loss']['loss'].append(total_loss / len(self.train_loader)) - self.result['acc_and_loss']['train_accuracy'].append(train_accuracy) - self.result['acc_and_loss']['test_accuracy'].append(test_accuracy) - - print(f'Epoch [{epoch+1}/{epochs}], Loss: {total_loss/len(self.train_loader):.4f}, ' - f'Train Acc: {train_accuracy:.4f}, Test Acc: {test_accuracy:.4f}') - - # 计算最终的混淆矩阵 - self.result['confusion_matrix'] = self.compute_confusion_matrix() - - def compute_confusion_matrix(self): - self.eval() - all_predictions = [] - all_labels = [] - - with torch.no_grad(): - for batch_X, batch_y in self.test_loader: - batch_X, batch_y = batch_X.to(self.device), batch_y.to(self.device) - outputs = self(batch_X) - predicted = torch.sign(outputs) - - all_predictions.extend(predicted.cpu().numpy()) - all_labels.extend(batch_y.cpu().numpy()) - - return confusion_matrix(all_labels, all_predictions) - - def evaluate(self): - self.eval() - correct = 0 - total = 0 - with torch.no_grad(): - for batch_X, batch_y in self.test_loader: - batch_X, batch_y = batch_X.to(self.device), batch_y.to(self.device) - outputs = self(batch_X) - predicted = torch.sign(outputs) - correct += (predicted == batch_y).sum().item() - total += batch_y.size(0) - return correct / total - - def rbf_kernel(self, X, Y): - X_norm = (X**2).sum(1).view(-1, 1) - Y_norm = (Y**2).sum(1).view(1, -1) - dist = X_norm + Y_norm - 2.0 * torch.mm(X, Y.t()) - return torch.exp(-self.gamma * dist) - - def forward(self, X): - X = X.to(self.device) - K = self.rbf_kernel(X, self.support_vectors) - return torch.mm(K, self.alpha.unsqueeze(1)).squeeze() + self.b - - def hinge_loss(self, outputs, targets): - return torch.mean(torch.clamp(1 - outputs * targets, min=0)) - - def regularization(self): - return 0.5 * (self.alpha ** 2).sum() \ No newline at end of file diff --git a/remake/Qtorch/models/__init__.py b/remake/Qtorch/models/__init__.py deleted file mode 100644 index 2d60838..0000000 --- a/remake/Qtorch/models/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from .Qsvm_brf import Qsvm_brf - -__all__ = ['Qsvm_brf'] \ No newline at end of file diff --git a/remake/Qtorch/models/__pycache__/QSVM.cpython-312.pyc b/remake/Qtorch/models/__pycache__/QSVM.cpython-312.pyc deleted file mode 100644 index 2a1814b..0000000 Binary files a/remake/Qtorch/models/__pycache__/QSVM.cpython-312.pyc and /dev/null differ diff --git a/remake/Qtorch/models/__pycache__/Qnn.cpython-312.pyc b/remake/Qtorch/models/__pycache__/Qnn.cpython-312.pyc deleted file mode 100644 index 46cccba..0000000 Binary files a/remake/Qtorch/models/__pycache__/Qnn.cpython-312.pyc and /dev/null differ diff --git a/remake/Qtorch/models/__pycache__/Qsvm_brf.cpython-312.pyc b/remake/Qtorch/models/__pycache__/Qsvm_brf.cpython-312.pyc deleted file mode 100644 index 103739a..0000000 Binary files a/remake/Qtorch/models/__pycache__/Qsvm_brf.cpython-312.pyc and /dev/null differ diff --git a/remake/Qtorch/models/__pycache__/__init__.cpython-312.pyc b/remake/Qtorch/models/__pycache__/__init__.cpython-312.pyc deleted file mode 100644 index d8dbabf..0000000 Binary files a/remake/Qtorch/models/__pycache__/__init__.cpython-312.pyc and /dev/null differ diff --git a/remake/README.md b/remake/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/remake/main.py b/remake/main.py deleted file mode 100644 index 31946fa..0000000 --- a/remake/main.py +++ /dev/null @@ -1,23 +0,0 @@ -from Qtorch.Functions import dLoader -from Qtorch.Models.Qmlp import Qmlp -from Qfuctions.divSet import divSet - -def main(): - projet_name = '20240821Sound' - label_names = None - - data = dLoader(projet_name, label_names) - - X_train, X_test, y_train, y_test, encoded_labels = - - - - - - - - - - -if __name__ == '__main__': - main() \ No newline at end of file