Python教程(四十):PyTorch深度学习-动态计算图
itomcoil 2025-08-21 03:15 1 浏览
今日目标
o 理解PyTorch的基本概念和动态计算图
o 掌握PyTorch张量操作和自动求导
o 学会构建神经网络模型
o 了解PyTorch的高级特性
o 掌握模型训练和部署
PyTorch概述
PyTorch是一个开源的机器学习框架,具有以下特点:
o 动态计算图:运行时构建计算图,灵活性高
o Python优先:与Python生态系统深度集成
o 易于调试:直观的代码结构和错误信息
o 研究友好:适合快速原型和实验
PyTorch vs TensorFlow
# PyTorch特点:动态图、Python优先、研究友好
# TensorFlow特点:静态图、生产部署、工具丰富
# PyTorch适合:研究、快速原型、动态网络
# TensorFlow适合:生产部署、移动端、大规模训练
PyTorch基础
1. 安装和导入
pip install torch torchvision torchaudio matplotlib seaborn scikit-learn
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, confusion_matrix
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 检查PyTorch版本和设备
print(f"PyTorch版本: {torch.__version__}")
print(f"CUDA可用: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"CUDA设备数量: {torch.cuda.device_count()}")
print(f"当前CUDA设备: {torch.cuda.current_device()}")
print(f"设备名称: {torch.cuda.get_device_name(0)}")
# 设置设备
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"使用设备: {device}")
2. 张量操作
def pytorch_tensor_operations():
"""PyTorch张量操作示例"""
# 1. 创建张量
# 从Python列表创建
tensor_from_list = torch.tensor([1, 2, 3, 4, 5])
print(f"从列表创建: {tensor_from_list}")
# 从NumPy数组创建
numpy_array = np.array([[1, 2, 3], [4, 5, 6]])
tensor_from_numpy = torch.from_numpy(numpy_array)
print(f"从NumPy创建:\n{tensor_from_numpy}")
# 特殊张量
zeros = torch.zeros(2, 3)
ones = torch.ones(3, 2)
random_tensor = torch.randn(2, 2)
print(f"\n零张量:\n{zeros}")
print(f"一张量:\n{ones}")
print(f"随机张量:\n{random_tensor}")
# 2. 张量属性
tensor = torch.randn(3, 4, 5)
print(f"\n张量形状: {tensor.shape}")
print(f"张量维度: {tensor.ndim}")
print(f"张量大小: {tensor.numel()}")
print(f"张量类型: {tensor.dtype}")
print(f"张量设备: {tensor.device}")
# 3. 张量运算
a = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
b = torch.tensor([[5, 6], [7, 8]], dtype=torch.float32)
addition = a + b
multiplication = a * b
matrix_mult = torch.matmul(a, b)
print(f"\n张量加法:\n{addition}")
print(f"张量乘法:\n{multiplication}")
print(f"矩阵乘法:\n{matrix_mult}")
# 4. 张量形状操作
tensor = torch.randn(2, 3, 4)
print(f"\n原始张量形状: {tensor.shape}")
# 重塑
reshaped = tensor.reshape(6, 4)
print(f"重塑后形状: {reshaped.shape}")
# 展平
flattened = tensor.flatten()
print(f"展平后形状: {flattened.shape}")
# 转置
transposed = tensor.transpose(0, 1)
print(f"转置后形状: {transposed.shape}")
# 5. 张量索引和切片
tensor = torch.randn(5, 5)
print(f"\n原始张量:\n{tensor}")
print(f"第一个元素: {tensor[0, 0]}")
print(f"第一行: {tensor[0, :]}")
print(f"第一列: {tensor[:, 0]}")
print(f"子张量:\n{tensor[1:3, 1:3]}")
# 6. 张量到NumPy转换
tensor = torch.randn(3, 3)
numpy_array = tensor.numpy()
print(f"\n张量:\n{tensor}")
print(f"NumPy数组:\n{numpy_array}")
return {
'tensor_from_list': tensor_from_list,
'tensor_from_numpy': tensor_from_numpy,
'random_tensor': random_tensor,
'a': a,
'b': b
}
# 运行张量操作示例
tensor_examples = pytorch_tensor_operations()
3. 自动求导
def autograd_example():
"""自动求导示例"""
# 1. 基本自动求导
x = torch.tensor(2.0, requires_grad=True)
y = x ** 2 + 3 * x + 1
y.backward()
print(f"x = {x}")
print(f"y = x^2 + 3x + 1 = {y}")
print(f"dy/dx = 2x + 3 = {x.grad}")
# 2. 多变量求导
x = torch.tensor([2.0, 3.0], requires_grad=True)
y = torch.sum(x ** 2)
y.backward()
print(f"\nx = {x}")
print(f"y = Σx^2 = {y}")
print(f"dy/dx = 2x = {x.grad}")
# 3. 链式求导
x = torch.tensor(1.0, requires_grad=True)
y = torch.sin(x)
z = y ** 2
z.backward()
print(f"\nx = {x}")
print(f"y = sin(x) = {y}")
print(f"z = y^2 = {z}")
print(f"dz/dx = 2sin(x)cos(x) = {x.grad}")
# 4. 梯度累积
x = torch.tensor(1.0, requires_grad=True)
for t in range(3):
y = x ** 2
y.backward()
print(f"第{t+1}次: x.grad = {x.grad}")
# 重置梯度
x.grad.zero_()
print(f"重置后: x.grad = {x.grad}")
# 5. 禁用梯度计算
with torch.no_grad():
y = x ** 2
print(f"\n禁用梯度计算: y = {y}")
print(f"y.requires_grad = {y.requires_grad}")
return x
# 运行自动求导示例
gradient_example = autograd_example()
构建神经网络
1. 简单神经网络
def simple_neural_network():
"""简单神经网络示例"""
# 生成示例数据
np.random.seed(42)
X = np.random.randn(1000, 10).astype(np.float32)
y = np.sum(X, axis=1) + np.random.normal(0, 0.1, 1000).astype(np.float32)
# 转换为PyTorch张量
X_tensor = torch.from_numpy(X)
y_tensor = torch.from_numpy(y).unsqueeze(1)
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(
X_tensor, y_tensor, test_size=0.2, random_state=42
)
print(f"训练集大小: {X_train.shape}")
print(f"测试集大小: {X_test.shape}")
# 1. 使用nn.Sequential构建模型
model = nn.Sequential(
nn.Linear(10, 64),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(64, 32),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(32, 1)
)
print(f"\n模型结构:")
print(model)
# 计算参数数量
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"总参数数量: {total_params}")
print(f"可训练参数数量: {trainable_params}")
# 2. 使用nn.Module构建模型
class SimpleNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(SimpleNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size // 2)
self.fc3 = nn.Linear(hidden_size // 2, output_size)
self.dropout = nn.Dropout(0.2)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.dropout(x)
x = self.relu(self.fc2(x))
x = self.dropout(x)
x = self.fc3(x)
return x
model_custom = SimpleNet(10, 64, 1)
print(f"\n自定义模型结构:")
print(model_custom)
# 3. 训练模型
def train_model(model, X_train, y_train, X_test, y_test, epochs=100):
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练历史
train_losses = []
test_losses = []
for epoch in range(epochs):
# 训练模式
model.train()
# 前向传播
outputs = model(X_train)
loss = criterion(outputs, y_train)
# 反向传播
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 评估模式
model.eval()
with torch.no_grad():
test_outputs = model(X_test)
test_loss = criterion(test_outputs, y_test)
train_losses.append(loss.item())
test_losses.append(test_loss.item())
if (epoch + 1) % 20 == 0:
print(f'Epoch [{epoch+1}/{epochs}], '
f'Train Loss: {loss.item():.4f}, '
f'Test Loss: {test_loss.item():.4f}')
return train_losses, test_losses
# 训练模型
train_losses, test_losses = train_model(model, X_train, y_train, X_test, y_test)
# 4. 可视化训练过程
plt.figure(figsize=(10, 6))
plt.plot(train_losses, label='训练损失')
plt.plot(test_losses, label='测试损失')
plt.title('模型训练过程')
plt.xlabel('轮次')
plt.ylabel('损失')
plt.legend()
plt.grid(True)
plt.show()
# 5. 模型预测
model.eval()
with torch.no_grad():
predictions = model(X_test[:5])
print(f"\n预测结果:")
for i in range(5):
print(f"真实值: {y_test[i].item():.4f}, "
f"预测值: {predictions[i].item():.4f}")
return model, model_custom, train_losses, test_losses
# 运行简单神经网络示例
simple_model, custom_model, train_losses, test_losses = simple_neural_network()
2. 分类神经网络
def classification_neural_network():
"""分类神经网络示例"""
# 使用鸢尾花数据集
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
iris = load_iris()
X = iris.data.astype(np.float32)
y = iris.target
# 数据预处理
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 转换为PyTorch张量
X_tensor = torch.from_numpy(X_scaled)
y_tensor = torch.from_numpy(y).long()
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(
X_tensor, y_tensor, test_size=0.2, random_state=42, stratify=y_tensor
)
print(f"训练集大小: {X_train.shape}")
print(f"测试集大小: {X_test.shape}")
print(f"类别数量: {len(np.unique(y))}")
# 构建分类模型
class ClassificationNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(ClassificationNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.bn1 = nn.BatchNorm1d(hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size // 2)
self.bn2 = nn.BatchNorm1d(hidden_size // 2)
self.fc3 = nn.Linear(hidden_size // 2, num_classes)
self.dropout = nn.Dropout(0.3)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.bn1(self.fc1(x)))
x = self.dropout(x)
x = self.relu(self.bn2(self.fc2(x)))
x = self.dropout(x)
x = self.fc3(x)
return x
model = ClassificationNet(4, 64, 3)
print(f"\n分类模型结构:")
print(model)
# 训练函数
def train_classification_model(model, X_train, y_train, X_test, y_test, epochs=100):
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
train_losses = []
train_accuracies = []
test_accuracies = []
for epoch in range(epochs):
# 训练模式
model.train()
# 前向传播
outputs = model(X_train)
loss = criterion(outputs, y_train)
# 反向传播
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 评估模式
model.eval()
with torch.no_grad():
train_outputs = model(X_train)
train_pred = torch.argmax(train_outputs, dim=1)
train_acc = (train_pred == y_train).float().mean()
test_outputs = model(X_test)
test_pred = torch.argmax(test_outputs, dim=1)
test_acc = (test_pred == y_test).float().mean()
train_losses.append(loss.item())
train_accuracies.append(train_acc.item())
test_accuracies.append(test_acc.item())
if (epoch + 1) % 20 == 0:
print(f'Epoch [{epoch+1}/{epochs}], '
f'Loss: {loss.item():.4f}, '
f'Train Acc: {train_acc.item():.4f}, '
f'Test Acc: {test_acc.item():.4f}')
return train_losses, train_accuracies, test_accuracies
# 训练模型
train_losses, train_accuracies, test_accuracies = train_classification_model(
model, X_train, y_train, X_test, y_test
)
# 可视化训练过程
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_losses)
plt.title('训练损失')
plt.xlabel('轮次')
plt.ylabel('损失')
plt.grid(True)
plt.subplot(1, 2, 2)
plt.plot(train_accuracies, label='训练准确率')
plt.plot(test_accuracies, label='测试准确率')
plt.title('模型准确率')
plt.xlabel('轮次')
plt.ylabel('准确率')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
# 模型评估
model.eval()
with torch.no_grad():
test_outputs = model(X_test)
test_pred = torch.argmax(test_outputs, dim=1)
# 分类报告
print(f"\n分类报告:")
print(classification_report(y_test.numpy(), test_pred.numpy(),
target_names=iris.target_names))
# 混淆矩阵
cm = confusion_matrix(y_test.numpy(), test_pred.numpy())
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=iris.target_names,
yticklabels=iris.target_names)
plt.title('混淆矩阵')
plt.xlabel('预测标签')
plt.ylabel('真实标签')
plt.show()
return model, train_losses, train_accuracies, test_accuracies
# 运行分类神经网络示例
classification_model, cls_train_losses, cls_train_accs, cls_test_accs = classification_neural_network()
卷积神经网络(CNN)
1. 图像分类CNN
def convolutional_neural_network():
"""卷积神经网络示例"""
# 使用MNIST数据集
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)) # MNIST标准化
])
# 加载数据
train_dataset = torchvision.datasets.MNIST(
root='./data', train=True, download=True, transform=transform
)
test_dataset = torchvision.datasets.MNIST(
root='./data', train=False, download=True, transform=transform
)
# 创建数据加载器
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
)
print(f"训练集大小: {len(train_dataset)}")
print(f"测试集大小: {len(test_dataset)}")
# 构建CNN模型
class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm2d(64)
self.pool = nn.MaxPool2d(2, 2)
self.dropout = nn.Dropout(0.25)
self.fc1 = nn.Linear(64 * 3 * 3, 128)
self.fc2 = nn.Linear(128, num_classes)
self.relu = nn.ReLU()
def forward(self, x):
# 第一个卷积块
x = self.pool(self.relu(self.bn1(self.conv1(x))))
x = self.dropout(x)
# 第二个卷积块
x = self.pool(self.relu(self.bn2(self.conv2(x))))
x = self.dropout(x)
# 第三个卷积块
x = self.relu(self.bn3(self.conv3(x)))
# 全连接层
x = x.view(x.size(0), -1)
x = self.relu(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x
model = ConvNet().to(device)
print(f"\nCNN模型结构:")
print(model)
# 训练函数
def train_cnn_model(model, train_loader, test_loader, epochs=5):
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.7)
train_losses = []
train_accuracies = []
test_accuracies = []
for epoch in range(epochs):
# 训练阶段
model.train()
running_loss = 0.0
correct = 0
total = 0
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
_, predicted = torch.max(output.data, 1)
total += target.size(0)
correct += (predicted == target).sum().item()
if batch_idx % 100 == 0:
print(f'Epoch: {epoch+1}, Batch: {batch_idx}, '
f'Loss: {loss.item():.4f}')
# 计算训练准确率
train_acc = 100 * correct / total
train_losses.append(running_loss / len(train_loader))
train_accuracies.append(train_acc)
# 测试阶段
model.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += criterion(output, target).item()
_, predicted = torch.max(output.data, 1)
total += target.size(0)
correct += (predicted == target).sum().item()
test_acc = 100 * correct / total
test_accuracies.append(test_acc)
print(f'Epoch {epoch+1}: Train Acc: {train_acc:.2f}%, '
f'Test Acc: {test_acc:.2f}%')
scheduler.step()
return train_losses, train_accuracies, test_accuracies
# 训练模型
train_losses, train_accuracies, test_accuracies = train_cnn_model(
model, train_loader, test_loader
)
# 可视化训练过程
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_losses)
plt.title('CNN训练损失')
plt.xlabel('轮次')
plt.ylabel('损失')
plt.grid(True)
plt.subplot(1, 2, 2)
plt.plot(train_accuracies, label='训练准确率')
plt.plot(test_accuracies, label='测试准确率')
plt.title('CNN模型准确率')
plt.xlabel('轮次')
plt.ylabel('准确率 (%)')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
# 模型预测可视化
model.eval()
with torch.no_grad():
# 获取一批测试数据
dataiter = iter(test_loader)
images, labels = next(dataiter)
images = images.to(device)
labels = labels.to(device)
# 预测
outputs = model(images)
_, predicted = torch.max(outputs, 1)
# 可视化前10个样本
plt.figure(figsize=(15, 6))
for i in range(10):
plt.subplot(2, 5, i + 1)
plt.imshow(images[i].cpu().squeeze(), cmap='gray')
plt.title(f'预测: {predicted[i]}\n真实: {labels[i]}')
plt.axis('off')
plt.tight_layout()
plt.show()
return model, train_losses, train_accuracies, test_accuracies
# 运行CNN示例
cnn_model, cnn_train_losses, cnn_train_accs, cnn_test_accs = convolutional_neural_network()
高级特性
1. 数据加载器
def custom_dataset_example():
"""自定义数据集示例"""
# 创建自定义数据集
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, X, y, transform=None):
self.X = X
self.y = y
self.transform = transform
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
x = self.X[idx]
y = self.y[idx]
if self.transform:
x = self.transform(x)
return x, y
# 生成示例数据
X = torch.randn(1000, 10)
y = torch.randint(0, 3, (1000,))
# 创建数据集
dataset = CustomDataset(X, y)
# 创建数据加载器
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=32, shuffle=True, num_workers=0
)
print(f"数据集大小: {len(dataset)}")
print(f"批次数量: {len(dataloader)}")
# 遍历数据加载器
for batch_idx, (data, target) in enumerate(dataloader):
print(f"批次 {batch_idx}: 数据形状 {data.shape}, 标签形状 {target.shape}")
if batch_idx >= 2: # 只显示前3个批次
break
return dataset, dataloader
# 运行自定义数据集示例
custom_dataset, custom_dataloader = custom_dataset_example()
2. 模型保存和加载
def model_save_load():
"""模型保存和加载示例"""
# 使用之前训练的CNN模型
model = cnn_model
# 1. 保存整个模型
torch.save(model, 'mnist_cnn_model.pth')
print("模型已保存为 mnist_cnn_model.pth")
# 加载模型
loaded_model = torch.load('mnist_cnn_model.pth')
print("模型已加载")
# 2. 保存模型状态字典
torch.save(model.state_dict(), 'mnist_cnn_state_dict.pth')
print("模型状态字典已保存")
# 加载状态字典
new_model = ConvNet()
new_model.load_state_dict(torch.load('mnist_cnn_state_dict.pth'))
print("状态字典已加载到新模型")
# 3. 保存检查点(包含优化器状态)
checkpoint = {
'epoch': 5,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optim.Adam(model.parameters()).state_dict(),
'loss': 0.1,
}
torch.save(checkpoint, 'mnist_cnn_checkpoint.pth')
print("检查点已保存")
# 加载检查点
loaded_checkpoint = torch.load('mnist_cnn_checkpoint.pth')
print(f"检查点信息: 轮次={loaded_checkpoint['epoch']}, "
f"损失={loaded_checkpoint['loss']}")
return loaded_model, new_model, loaded_checkpoint
# 运行模型保存加载示例
saved_models = model_save_load()
3. GPU加速
def gpu_acceleration():
"""GPU加速示例"""
# 检查GPU可用性
if torch.cuda.is_available():
print("GPU可用,使用GPU加速")
# 将模型移到GPU
model = ConvNet().cuda()
print(f"模型设备: {next(model.parameters()).device}")
# 将数据移到GPU
x = torch.randn(100, 1, 28, 28).cuda()
y = torch.randint(0, 10, (100,)).cuda()
print(f"输入数据设备: {x.device}")
print(f"标签数据设备: {y.device}")
# GPU训练示例
model.train()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())
for epoch in range(3):
optimizer.zero_grad()
output = model(x)
loss = criterion(output, y)
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}")
# 内存管理
torch.cuda.empty_cache()
print("GPU内存已清理")
else:
print("GPU不可用,使用CPU")
return model if torch.cuda.is_available() else None
# 运行GPU加速示例
gpu_model = gpu_acceleration()
今日总结
今天我们学习了PyTorch深度学习的基础知识:
1. PyTorch基础:张量操作、自动求导、设备管理
2. 神经网络构建:nn.Sequential、nn.Module、自定义层
3. 分类和回归:全连接神经网络、损失函数、优化器
4. 卷积神经网络:CNN架构、图像分类、数据加载
5. 高级特性:自定义数据集、模型保存、GPU加速
PyTorch是深度学习的重要框架,掌握这些知识可以构建各种深度学习模型。
相关推荐
- 最强聚类模型,层次聚类 !!_层次聚类的优缺点
-
哈喽,我是小白~咱们今天聊聊层次聚类,这种聚类方法在后面的使用,也是非常频繁的~首先,聚类很好理解,聚类(Clustering)就是把一堆“东西”自动分组。这些“东西”可以是人、...
- python决策树用于分类和回归问题实际应用案例
-
决策树(DecisionTrees)通过树状结构进行决策,在每个节点上根据特征进行分支。用于分类和回归问题。实际应用案例:预测一个顾客是否会流失。决策树是一种基于树状结构的机器学习算法,用于解决分类...
- Python教程(四十五):推荐系统-个性化推荐算法
-
今日目标o理解推荐系统的基本概念和类型o掌握协同过滤算法(用户和物品)o学会基于内容的推荐方法o了解矩阵分解和深度学习推荐o掌握推荐系统评估和优化技术推荐系统概述推荐系统是信息过滤系统,用于...
- 简单学Python——NumPy库7——排序和去重
-
NumPy数组排序主要用sort方法,sort方法只能将数值按升充排列(可以用[::-1]的切片方式实现降序排序),并且不改变原数组。例如:importnumpyasnpa=np.array(...
- PyTorch实战:TorchVision目标检测模型微调完
-
PyTorch实战:TorchVision目标检测模型微调完整教程一、什么是微调(Finetuning)?微调(Finetuning)是指在已经预训练好的模型基础上,使用自己的数据对模型进行进一步训练...
- C4.5算法解释_简述c4.5算法的基本思想
-
C4.5算法是ID3算法的改进版,它在特征选择上采用了信息增益比来解决ID3算法对取值较多的特征有偏好的问题。C4.5算法也是一种用于决策树构建的算法,它同样基于信息熵的概念。C4.5算法的步骤如下:...
- Python中的数据聚类及可视化分析实践
-
探索如何通过聚类分析揭露糖尿病预测数据集的特征!我们将运用Python的强力工具,深入挖掘数据,以直观的可视化揭示不同特征间的关系。一同探索聚类分析在糖尿病预测中的实践!所有这些可视化都可以通过数据操...
- 用Python来统计大乐透号码的概率分布
-
用Python来统计大乐透号码的概率分布,可以按照以下步骤进行:导入所需的库:使用Python中的numpy库生成数字序列,使用matplotlib库生成概率分布图。读取大乐透历史数据:从网络上找到大...
- python:支持向量机监督学习算法用于二分类和多分类问题示例
-
监督学习-支持向量机(SVM)支持向量机(SupportVectorMachine,简称SVM)是一种常用的监督学习算法,用于解决分类和回归问题。SVM的目标是找到一个最优的超平面,将不同类别的...
- 25个例子学会Pandas Groupby 操作
-
groupby是Pandas在数据分析中最常用的函数之一。它用于根据给定列中的不同值对数据点(即行)进行分组,分组后的数据可以计算生成组的聚合值。如果我们有一个包含汽车品牌和价格信息的数据集,那么可以...
- 数据挖掘流程_数据挖掘流程主要有哪些步骤
-
数据挖掘流程1.了解需求,确认目标说一下几点思考方法:做什么?目的是什么?目标是什么?为什么要做?有什么价值和意义?如何去做?完整解决方案是什么?2.获取数据pandas读取数据pd.read.c...
- 使用Python寻找图像最常见的颜色_python 以图找图
-
如果我们知道图像或对象最常见的是哪种颜色,那么可以解决图像处理中的几个用例,例如在农业领域,我们可能需要确定水果的成熟度。我们可以简单地检查一下水果的颜色是否在预定的范围内,看看它是成熟的,腐烂的,还...
- 财务预算分析全网最佳实践:从每月分析到每天分析
-
原文链接如下:「链接」掌握本文的方法,你就掌握了企业预算精细化分析的能力,全网首发。数据模拟稍微有点问题,不要在意数据细节,先看下最终效果。在编制财务预算或业务预算的过程中,通常预算的所有数据都是按月...
- 常用数据工具去重方法_数据去重公式
-
在数据处理中,去除重复数据是确保数据质量和分析准确性的关键步骤。特别是在处理多列数据时,保留唯一值组合能够有效清理数据集,避免冗余信息对分析结果的干扰。不同的工具和编程语言提供了多种方法来实现多列去重...
- Python教程(四十):PyTorch深度学习-动态计算图
-
今日目标o理解PyTorch的基本概念和动态计算图o掌握PyTorch张量操作和自动求导o学会构建神经网络模型o了解PyTorch的高级特性o掌握模型训练和部署PyTorch概述PyTorc...
- 一周热门
- 最近发表
- 标签列表
-
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)
- shutil.copy() (33)