save
This commit is contained in:
parent
101ba89c34
commit
847bdae9f6
|
|
@ -1,32 +1,48 @@
|
|||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.optim as optim
|
||||
from Qtorch.Models.Qnn import Qnn
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
|
||||
class Simple1DCNN(nn.Module):
|
||||
def __init__(self, input_size, num_classes):
|
||||
super(Simple1DCNN, self).__init__()
|
||||
self.conv1 = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1)
|
||||
self.relu = nn.ReLU()
|
||||
self.pool = nn.MaxPool1d(kernel_size=2, stride=2)
|
||||
self.conv2 = nn.Conv1d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
|
||||
self.fc1 = nn.Linear(64 * (input_size // 4), 128) # 假设经过两次池化后,长度减半两次
|
||||
self.fc2 = nn.Linear(128, num_classes)
|
||||
class QCNN(Qnn):
|
||||
def __init__(self, X_train, y_train, X_test, y_test,
|
||||
labels=None,
|
||||
dropout_rate=0.3
|
||||
):
|
||||
super(QCNN, self).__init__()
|
||||
|
||||
self.LABEL_ENCODER = LabelEncoder()
|
||||
|
||||
self.X_train, self.y_train, self.X_test, self.y_test = X_train, y_train, X_test, y_test
|
||||
|
||||
self.labels = labels
|
||||
|
||||
input_size = X_train.shape[1]
|
||||
num_classes = len(set(y_train))
|
||||
|
||||
self.layers = nn.ModuleList()
|
||||
|
||||
# Input layer to first Convolutional layer
|
||||
self.layers.append(nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1))
|
||||
self.layers.append(nn.ReLU())
|
||||
self.layers.append(nn.MaxPool1d(kernel_size=2, stride=2))
|
||||
|
||||
# Calculate the size after convolutions and pooling
|
||||
conv_output_size = input_size // 4 # Assuming two pooling layers with stride 2
|
||||
self.layers.append(nn.Linear(32 * conv_output_size, 128))
|
||||
self.layers.append(nn.ReLU())
|
||||
self.layers.append(nn.Dropout(dropout_rate))
|
||||
|
||||
# Output layer
|
||||
self.layers.append(nn.Linear(128, num_classes))
|
||||
self.__init_weights()
|
||||
|
||||
def forward(self, x):
|
||||
x = self.pool(self.relu(self.conv1(x)))
|
||||
x = self.pool(self.relu(self.conv2(x)))
|
||||
x = x.view(-1, 64 * (self.input_size // 4)) # 展平特征图
|
||||
x = self.relu(self.fc1(x))
|
||||
x = self.fc2(x)
|
||||
for layer in self.layers:
|
||||
x = layer(x)
|
||||
return x
|
||||
|
||||
# 实例化模型
|
||||
input_size = 100 # 假设n=100
|
||||
num_classes = 10 # 假设有10个类别
|
||||
model = Simple1DCNN(input_size, num_classes)
|
||||
|
||||
# 定义损失函数和优化器
|
||||
criterion = nn.CrossEntropyLoss()
|
||||
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
||||
|
||||
# 训练和评估模型的代码与之前类似,这里不再赘述。
|
||||
def __init_weights(self):
|
||||
for m in self.modules():
|
||||
if isinstance(m, nn.Conv1d) or isinstance(m, nn.Linear):
|
||||
nn.init.xavier_uniform_(m.weight)
|
||||
if m.bias is not None:
|
||||
m.bias.data.fill_(0.01)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
import torch
|
||||
import torch.nn as nn
|
||||
from Qtorch.Models.Qnn import Qnn
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.metrics import confusion_matrix
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class Qmlp(Qnn):
|
||||
|
|
|
|||
10
main.py
10
main.py
|
|
@ -1,4 +1,5 @@
|
|||
from Qtorch.Models.Qmlp import Qmlp
|
||||
from Qtorch.Models.Qcnn import QCNN
|
||||
from Qfunctions.divSet import divSet
|
||||
from Qfunctions.loaData import load_data
|
||||
from Qfunctions.saveToxlsx import save_to_xlsx as save_to_xlsx
|
||||
|
|
@ -11,15 +12,18 @@ def main():
|
|||
data=data, labels=label_names, test_size= 0.3
|
||||
)
|
||||
|
||||
model = Qmlp(
|
||||
# model = Qmlp(
|
||||
# X_train=X_train, X_test=X_test, y_train=y_train, y_test= y_test,
|
||||
# hidden_layers = [128],
|
||||
# dropout_rate=0
|
||||
# )
|
||||
model = QCNN(
|
||||
X_train=X_train, X_test=X_test, y_train=y_train, y_test= y_test,
|
||||
hidden_layers=[128, 128],
|
||||
dropout_rate=0
|
||||
)
|
||||
|
||||
pca_2d, pca_3d = model.get_PCA()
|
||||
|
||||
|
||||
model.fit(300)
|
||||
|
||||
cm = model.get_cm()
|
||||
|
|
|
|||
Loading…
Reference in New Issue