pytorch如何将csv数据输入cnn模型中,数据需要归一化且有14个特征
时间: 2024-05-09 09:20:39 浏览: 170
利用Python如何将数据写到CSV文件中
可以使用PyTorch的数据加载工具(DataLoader)和数据转换工具(Transform)来将CSV数据输入CNN模型中。
首先,将CSV数据读取为Pandas DataFrame对象,然后使用sklearn.preprocessing库中的MinMaxScaler对数据进行归一化处理。
接下来,使用PyTorch的Dataset和DataLoader将数据转换为可以输入模型的Tensor格式。在Dataset中,可以将特征和标签分开处理,其中特征需要通过Transform进行归一化处理。在DataLoader中,可以设置batch size、shuffle等参数。
最后,将数据加载到CNN模型中进行训练和测试。具体代码示例如下:
```python
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
# 读取CSV数据为Pandas DataFrame
df = pd.read_csv('data.csv')
# 分离特征和标签
X = df.iloc[:, :-1].values
y = df.iloc[:, -1].values
# 归一化处理特征
scaler = MinMaxScaler()
X = scaler.fit_transform(X)
# 定义Dataset和Transform
class MyDataset(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):
sample = {'X': self.X[idx], 'y': self.y[idx]}
if self.transform:
sample = self.transform(sample)
return sample
class MyTransform(object):
def __init__(self):
pass
def __call__(self, sample):
X, y = sample['X'], sample['y']
X = torch.from_numpy(X).float()
y = torch.tensor(y).long()
return {'X': X, 'y': y}
# 定义Transform
transform = transforms.Compose([
MyTransform()
])
# 定义Dataset和DataLoader
dataset = MyDataset(X, y, transform=transform)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
# 定义CNN模型
class MyCNN(torch.nn.Module):
def __init__(self):
super(MyCNN, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1)
self.conv2 = torch.nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
self.fc1 = torch.nn.Linear(32 * 7 * 7, 128)
self.fc2 = torch.nn.Linear(128, 10)
def forward(self, x):
x = x.unsqueeze(1)
x = torch.nn.functional.relu(self.conv1(x))
x = torch.nn.functional.max_pool2d(x, kernel_size=2, stride=2)
x = torch.nn.functional.relu(self.conv2(x))
x = torch.nn.functional.max_pool2d(x, kernel_size=2, stride=2)
x = x.view(-1, 32 * 7 * 7)
x = torch.nn.functional.relu(self.fc1(x))
x = self.fc2(x)
return x
# 训练CNN模型
cnn = MyCNN()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=0.001)
for epoch in range(10):
for batch in dataloader:
X_batch, y_batch = batch['X'], batch['y']
optimizer.zero_grad()
y_pred = cnn(X_batch)
loss = criterion(y_pred, y_batch)
loss.backward()
optimizer.step()
print('Epoch: {}, Loss: {:.4f}'.format(epoch+1, loss.item()))
```
阅读全文