import os
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import tifffile
from sklearn.metrics import accuracy_score, classification_report
class GeologicalDataset(Dataset):
def __init__(self, positive_paths, negative_paths, transform=None):
self.file_paths = []
self.labels = []
# 加载正样本
for path in positive_paths:
for file in os.listdir(path):
if file.endswith('.tif'):
self.file_paths.append(os.path.join(path, file))
self.labels.append(1)
# 加载负样本
for path in negative_paths:
for file in os.listdir(path):
if file.endswith('.tif'):
self.file_paths.append(os.path.join(path, file))
self.labels.append(0)
self.transform = transform
def __len__(self):
return len(self.file_paths)
def __getitem__(self, idx):
img = tifffile.imread(self.file_paths[idx])
label = self.labels[idx]
# 验证数据维度
if img.shape[0] !=13:
raise ValueError(f"Expected 13 channels, but got {img.shape[0]} channels in file {self.file_paths[idx]}")
# 数据预处理
img = np.clip(img, -1e6, 1e6)
for i in range(img.shape[0]):
channel = img[i]
mean = np.mean(channel)
std = np.std(channel)
if std > 0:
img[i] = (channel - mean) / std
img = torch.from_numpy(img).float()
return img, label
class SimplifiedLeNet5(nn.Module):
def __init__(self, input_channels=13, input_size=32):
super(SimplifiedLeNet5, self).__init__()
self.conv1 = nn.Conv2d(input_channels, 26, 5, padding=0)
self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(26, 52, 3, padding=1)
self.relu2 = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(2, 2)
# 动态计算fc1的输入尺寸
with torch.no_grad():
x = torch.randn(1, input_channels, input_size, input_size)
x = self.conv1(x)
x = self.relu1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.relu2(x)
x = self.maxpool2(x)
self.fc_input_size = x.view(1, -1).size(1)
self.flatten = nn.Flatten()
self.fc1 = nn.Linear(self.fc_input_size, 120)
self.relu3 = nn.ReLU()
self.dropout = nn.Dropout(0.5)
self.fc2 = nn.Linear(120, 84)
self.relu4 = nn.ReLU()
self.output = nn.Linear(84, 2)
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
nn.init.constant_(m.bias, 0)
def forward(self, x):
x = self.conv1(x)
x = self.relu1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.relu2(x)
x = self.maxpool2(x)
x = self.flatten(x)
x = self.fc1(x)
x = self.relu3(x)
x = self.dropout(x)
x = self.fc2(x)
x = self.relu4(x)
x = self.output(x)
return x
def train_and_evaluate():
# 设置随机种子
torch.manual_seed(42)
np.random.seed(42)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# 数据路径配置
train_positive = [r'D:\201app\arcgis出图资料\地质特征量化\样本_芮芮\分割数据集\正样本\训练集']
train_negative = [r"D:\201app\arcgis出图资料\地质特征量化\样本_芮芮\分割数据集\负样本\训练集"]
test_positive = [r"D:\201app\arcgis出图资料\地质特征量化\样本_芮芮\分割数据集\正样本\测试集"]
test_negative = [r"D:\201app\arcgis出图资料\地质特征量化\样本_芮芮\分割数据集\负样本\测试集"]
# 创建数据集和数据加载器
train_dataset = GeologicalDataset(train_positive, train_negative)
test_dataset = GeologicalDataset(test_positive, test_negative)
# # 验证输入尺寸
# sample_img, _ = train_dataset[0]
# print(f"Input image size: {sample_img.shape[1:]}") # 输出应该是 [24, 24]
#
# # 动态获取输入尺寸并初始化模型
# input_size = sample_img.shape[1] # 假设高度和宽度相同
# model = SimplifiedLeNet5(input_size=input_size).to(device)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=4)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)
# 初始化模型
model = SimplifiedLeNet5().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4, weight_decay=1e-4)
# 学习率调度器
scheduler = torch.optim.lr_scheduler.OneCycleLR(
optimizer,
max_lr=1e-3,
steps_per_epoch=len(train_loader),
epochs=20,
pct_start=0.3
)
# 训练参数
num_epochs = 20
best_acc = 0.0
save_dir = r"D:\201app\arcgis出图资料\地质特征量化\模型"
os.makedirs(save_dir, exist_ok=True)
# 训练循环
for epoch in range(num_epochs):
model.train()
running_loss = 0.0
all_preds = []
all_labels = []
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels.long())
loss.backward()
optimizer.step()
scheduler.step()
running_loss += loss.item()
preds = torch.argmax(outputs, dim=1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
# 计算训练指标
train_acc = accuracy_score(all_labels, all_preds)
print(
f"Epoch [{epoch + 1}/{num_epochs}] | Loss: {running_loss / len(train_loader):.4f} | Train Acc: {train_acc:.4f}")
# 每个epoch后进行验证
model.eval()
test_preds = []
test_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs = inputs.to(device)
outputs = model(inputs)
preds = torch.argmax(outputs, dim=1)
test_preds.extend(preds.cpu().numpy())
test_labels.extend(labels.cpu().numpy())
# 计算测试指标
test_acc = accuracy_score(test_labels, test_preds)
report = classification_report(test_labels, test_preds, target_names=['负样本', '正样本'])
print(f"Validation Results:")
print(report)
# 保存最佳模型
if test_acc > best_acc:
best_acc = test_acc
torch.save(model.state_dict(), os.path.join(save_dir, "best_CNN_model.pth"))
# 最终测试评估
print("\nFinal Test Evaluation:")
final_acc, detailed_report, neg_acc, pos_acc = predict_model(model, test_loader, device)
print(f"Overall Accuracy: {final_acc:.4f}")
print(f"Negative Sample Recall: {neg_acc:.4f}")
print(f"Positive Sample Recall: {pos_acc:.4f}")
print("Detailed Classification Report:")
print(detailed_report)
def predict_model(model, test_loader, device):
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs = inputs.to(device)
outputs = model(inputs)
preds = torch.argmax(outputs, dim=1).cpu().numpy()
all_preds.extend(preds)
all_labels.extend(labels.numpy())
accuracy = accuracy_score(all_labels, all_preds)
report = classification_report(all_labels, all_preds, target_names=['负样本', '正样本'], output_dict=True)
return accuracy, report, report['负样本']['recall'], report['正样本']['recall']
if __name__ == "__main__":
train_and_evaluate()
我的代码的训练过程是这样,有什么问题,帮我分析一下D:\201app\PythonProject_new\.venv\Scripts\python.exe D:\201app\PythonProject_new\地质特征量化\CNN_SimplifiedLeNet5.py
Using device: cpu
Epoch [1/20] | Loss: 0.3016 | Train Acc: 0.9232
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [2/20] | Loss: 0.2439 | Train Acc: 0.9449
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [3/20] | Loss: 0.2056 | Train Acc: 0.9449
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [4/20] | Loss: 0.1867 | Train Acc: 0.9449
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [5/20] | Loss: 0.1820 | Train Acc: 0.9488
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [6/20] | Loss: 0.1317 | Train Acc: 0.9528
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [7/20] | Loss: 0.1064 | Train Acc: 0.9587
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [8/20] | Loss: 0.0677 | Train Acc: 0.9783
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [9/20] | Loss: 0.0520 | Train Acc: 0.9803
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [10/20] | Loss: 0.0329 | Train Acc: 0.9941
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [11/20] | Loss: 0.0190 | Train Acc: 0.9921
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [12/20] | Loss: 0.0074 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [13/20] | Loss: 0.0047 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [14/20] | Loss: 0.0023 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [15/20] | Loss: 0.0022 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [16/20] | Loss: 0.0034 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [17/20] | Loss: 0.0021 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [18/20] | Loss: 0.0023 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Epoch [19/20] | Loss: 0.0028 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Epoch [20/20] | Loss: 0.0019 | Train Acc: 1.0000
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Validation Results:
precision recall f1-score support
负样本 0.00 0.00 0.00 7
正样本 0.94 1.00 0.97 120
accuracy 0.94 127
macro avg 0.47 0.50 0.49 127
weighted avg 0.89 0.94 0.92 127
Final Test Evaluation:
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
Overall Accuracy: 0.9449
Negative Sample Recall: 0.0000
Positive Sample Recall: 1.0000
Detailed Classification Report:
{'负样本': {'precision': 0.0, 'recall': 0.0, 'f1-score': 0.0, 'support': 7.0}, '正样本': {'precision': 0.9448818897637795, 'recall': 1.0, 'f1-score': 0.97165991902834, 'support': 120.0}, 'accuracy': 0.9448818897637795, 'macro avg': {'precision': 0.47244094488188976, 'recall': 0.5, 'f1-score': 0.48582995951417, 'support': 127.0}, 'weighted avg': {'precision': 0.8928017856035712, 'recall': 0.9448818897637795, 'f1-score': 0.9181038604992189, 'support': 127.0}}
Process finished with exit code 0