已知无人机撞击地面点的二维概率密度函数,用MATLAB画出无人机撞击地面点的二维分布图
时间: 2023-09-20 10:09:27 浏览: 87
matlab求二维概率密度二维概率分布
5星 · 资源好评率100%
以下是一个基于pytorch的3DCNN分类代码示例:
```
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import numpy as np
class MyDataset(Dataset):
def __init__(self, data_path, label_path):
self.data = np.load(data_path)
self.labels = np.load(label_path)
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
sample = {'data': self.data[idx], 'label': self.labels[idx]}
return sample
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = nn.Conv3d(1, 16, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm3d(16)
self.relu1 = nn.ReLU(inplace=True)
self.pool1 = nn.MaxPool3d(kernel_size=2, stride=2)
self.conv2 = nn.Conv3d(16, 32, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm3d(32)
self.relu2 = nn.ReLU(inplace=True)
self.pool2 = nn.MaxPool3d(kernel_size=2, stride=2)
self.conv3 = nn.Conv3d(32, 64, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm3d(64)
self.relu3 = nn.ReLU(inplace=True)
self.pool3 = nn.MaxPool3d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(64 * 4 * 4 * 4, 128)
self.relu4 = nn.ReLU(inplace=True)
self.dropout = nn.Dropout(p=0.5)
self.fc2 = nn.Linear(128, 2)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.relu3(x)
x = self.pool3(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.relu4(x)
x = self.dropout(x)
x = self.fc2(x)
return x
def train(model, device, train_loader, optimizer, criterion):
model.train()
for batch_idx, sample in enumerate(train_loader):
data, target = sample['data'], sample['label']
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
def test(model, device, test_loader, criterion):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for batch_idx, sample in enumerate(test_loader):
data, target = sample['data'], sample['label']
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += criterion(output, target).item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
accuracy = correct / len(test_loader.dataset)
print('Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format(
test_loss, correct, len(test_loader.dataset), 100. * accuracy))
def main():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_dataset = MyDataset('train_data.npy', 'train_labels.npy')
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
test_dataset = MyDataset('test_data.npy', 'test_labels.npy')
test_loader = DataLoader(test_dataset, batch_size=16, shuffle=True)
model = MyModel().to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
for epoch in range(10):
train(model, device, train_loader, optimizer, criterion)
test(model, device, test_loader, criterion)
if __name__ == '__main__':
main()
```
在这个示例中,我们首先定义了一个`MyDataset`类,这个类用来加载数据集。`MyDataset`类继承自`torch.utils.data.Dataset`类,实现了`__len__`和`__getitem__`方法,分别用来获取数据集的长度和获取数据集中的一个样本。在`__getitem__`方法中,我们返回一个字典,包含了样本数据和标签。
接下来,我们定义了一个`MyModel`类,这个类是我们的模型。这个模型是一个简单的3D卷积神经网络,包含了3个卷积层和2个全连接层。在前向传播中,我们首先进行3次卷积和池化操作,然后将特征图展平并送入2个全连接层中。在第一个全连接层中,我们使用ReLU激活函数和dropout进行非线性变换和正则化。
最后,我们定义了`train`和`test`函数,用来训练和测试模型。在`train`函数中,我们首先将模型设为训练模式,然后对于每个batch,计算输出和损失,并执行反向传播和参数更新。在`test`函数中,我们将模型设为评估模式,然后对于测试集中的每个batch,计算输出和损失,并计算准确率。
最后,在`main`函数中,我们加载数据集,创建模型和优化器,并开始训练和测试模型。
阅读全文