input_size = 24001#excel数据点数
dir_list = os.listdir("E:/pythonProject/doing")#实验数据路径,./表示当下文件夹
all_data_list=[]
all_label_list=[]
for i in range(len(dir_list)):
label = int(dir_list[i].split("_")[-1])
print(label)
dir = dir_list[i]
file_list = os.listdir("./doing/"+dir)
print(file_list)
tmp_data_list = []
tmp_label_list = []
for j in range(len(file_list)):
filename = "./doing/"+dir+"/"+file_list[j]
#print(filename)
tmp_df = pd.read_excel(filename)
tmp_data = np.array(tmp_df.values[:, 1])
# print(len(tmp_data))
tmp_data = F.interpolate(torch.tensor(tmp_data).unsqueeze(0).unsqueeze(0).unsqueeze(0),
(1, input_size)).squeeze(0).squeeze(0).squeeze(0).numpy()
#plt.plot(tmp_data)
#plt.show()
print(tmp_data.shape)
tmp_data_list.append(tmp_data)
tmp_label_list.append(label)
tmp_data = np.array(tmp_data_list)
tmp_label = np.array(tmp_label_list)
all_data_list.append(tmp_data)
all_label_list.append(tmp_label)
all_data = np.array(all_data_list).reshape(-1,input_size)
all_data = (all_data - np.mean(all_data)) / (np.max(all_data) - np.min(all_data))
print(all_data.shape)
label = np.array(all_label_list)
label = label.reshape(label.shape[0]*label.shape[1])
print(label.shape)
class Load_dataset(data.Dataset):
def __init__(self, data, label):
self.len = label.shape[0]
self.data = data
self.label = label
def __len__(self):
return self.len
def __getitem__(self, idx):
x = self.data[idx]
y = self.label[idx]
return x, y
def collate_fn(self, data):
x, y = zip(*data)
return x, y
full_dataset = Load_dataset(all_data, label)
train_num = 80
test_num = 20
train_set, test_set = torch.utils.data.random_split(full_dataset, [train_num, test_num],
generator=torch.Generator().manual_seed(6))
loader_train = torch.utils.data.DataLoader(train_set, batch_size=int(train_num / 2), shuffle=True)
loader_test = torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=False)
# 模型定义函数
class model(nn.Module):
def __init__(self):
super(model, self).__init__()
self.conv1 = nn.Conv1d(in_channels=1, out_channels=64, kernel_size=5, padding=2)
self.pool1 = nn.MaxPool1d(kernel_size=2, stride=2)
self.conv2 = nn.Conv1d(in_channels=64, out_channels=128, kernel_size=5, padding=2)
self.pool2 = nn.MaxPool1d(kernel_size=2, stride=2)
self.conv3 = nn.Conv1d(in_channels=128, out_channels=256, kernel_size=5, padding=2)
self.pool3 = nn.MaxPool1d(kernel_size=2, stride=2)
self.fc = nn.Linear(input_size * 4, 10)
def forward(self, x):
y = F.relu(self.conv1(x))
y = self.pool1(y)
y = F.relu(self.conv2(y))
y = self.pool2(y)
y = F.relu(self.conv3(y))
y = self.pool3(y)
y = y.reshape(y.shape[0], -1)
y = self.fc(y)
return y
net = model()
opt = torch.optim.Adam(net.parameters(), lr=0.001)
best_test_acc = 0
patience = 0
train_loss = []
train_acc = []
test_acc = []
for epoch in range(100):
net.train()
pred_list = []
label_list = []
for i, (x, y) in enumerate(loader_train):
pred = net(x.unsqueeze(1).float())
loss = torch.nn.CrossEntropyLoss()(pred, y.long())
acc = torch.mean((y == pred.argmax(1)).float())
print(epoch, i)
print("train_loss:", loss.item())
print("train_acc", round(acc.item(), 4))
train_acc.append(acc.item())
train_loss.append(loss.item())
opt.zero_grad()
loss.backward()
opt.step()
net.eval()
acc = 0
for i, (x, y) in enumerate(loader_test):
pred = net(x.unsqueeze(1).float())
pred_list.append(pred.argmax(1).item())
label_list.append(y.item())
acc += torch.sum((y == pred.argmax(1)).float())
acc = acc / test_num
if acc > best_test_acc:
patience = 0
best_test_acc = acc.item()
best_pred_list = pred_list
torch.save(net.state_dict(), 'net_best.pkl')
else:
patience += 1
# if patience == 3:
# break
print("test_acc", round(acc.item(), 4))
test_acc.append(acc.item())
print("best_test_acc", round(best_test_acc, 4))
# 损失函数图
import matplotlib.pyplot as plt
print()
plt.plot(train_loss)
train_acc= 'lossrecord/loss.txt'
plt.xlabel("step", font='Times New Roman', fontsize=16)
plt.ylabel("loss", font='Times New Roman', fontsize=16)
plt.show()
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
confusion_mat = confusion_matrix(np.array(label_list), np.array(best_pred_list))
disp = ConfusionMatrixDisplay(confusion_matrix=confusion_mat)
disp.plot(
include_values=True,
cmap="viridis",
ax=None,
xticks_rotation="horizontal",
values_format="d"
)
plt.show()
报错“RuntimeError: mat1 and mat2 shapes cannot be multiplied (40x96000 and 96004x10)”,大神们,这个怎么修改啊?让这个py正常工作。