用Pytorch实现利用前馈神经网络预测共享单车的使用数量过程
时间: 2023-07-14 08:14:14 浏览: 176
好的,下面是一个简单的前馈神经网络模型的实现,可以用来预测共享车的使用数量过程。首先,我们需要准备数据集,并对数据进行预处理。
数据集可以从UCI Machine Learning Repository上下载,包含了每小时的共享单车使用数量以及相关的天气和时间信息。我们将数据集分为训练集和测试集,并使用特征缩放将所有特征值缩放到0到1的范围内。
```python
import pandas as pd
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
from sklearn.preprocessing import MinMaxScaler
# 加载数据集
url = "http://archive.ics.uci.edu/ml/machine-learning-databases/00275/Bike-Sharing-Dataset.zip"
data = pd.read_csv(url, compression='zip', header=0, sep=',', quotechar='"')
# 预处理数据
data = data.drop(['instant', 'dteday', 'casual', 'registered'], axis=1)
scaler = MinMaxScaler()
data[['cnt', 'temp', 'atemp', 'hum', 'windspeed']] = scaler.fit_transform(data[['cnt', 'temp', 'atemp', 'hum', 'windspeed']])
# 划分数据集
train_data = data.sample(frac=0.8)
test_data = data.drop(train_data.index)
# 转换为PyTorch张量
train_targets = torch.tensor(train_data['cnt'].values).float()
test_targets = torch.tensor(test_data['cnt'].values).float()
train_features = torch.tensor(train_data.drop(['cnt'], axis=1).values).float()
test_features = torch.tensor(test_data.drop(['cnt'], axis=1).values).float()
# 创建数据集类
class BikeSharingDataset(Dataset):
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return len(self.features)
def __getitem__(self, idx):
return self.features[idx], self.targets[idx]
# 创建数据加载器
train_dataset = BikeSharingDataset(train_features, train_targets)
test_dataset = BikeSharingDataset(test_features, test_targets)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
```
接下来我们可以定义模型了。这里我们使用一个具有两个隐藏层的前馈神经网络,激活函数为ReLU。
```python
class NeuralNet(torch.nn.Module):
def __init__(self, input_size, hidden_size, num_hidden_layers):
super(NeuralNet, self).__init__()
self.layers = []
self.layers.append(torch.nn.Linear(input_size, hidden_size))
self.layers.append(torch.nn.ReLU())
for i in range(num_hidden_layers-1):
self.layers.append(torch.nn.Linear(hidden_size, hidden_size))
self.layers.append(torch.nn.ReLU())
self.layers.append(torch.nn.Linear(hidden_size, 1))
self.layers = torch.nn.Sequential(*self.layers)
def forward(self, x):
return self.layers(x)
```
接下来是训练代码:
```python
def train(model, optimizer, loss_fn, train_loader, device):
model.train()
total_loss = 0
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data).squeeze()
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
total_loss += loss.item()
return total_loss / len(train_loader)
def test(model, loss_fn, test_loader, device):
model.eval()
total_loss = 0
with torch.no_grad():
for batch_idx, (data, target) in enumerate(test_loader):
data, target = data.to(device), target.to(device)
output = model(data).squeeze()
loss = loss_fn(output, target)
total_loss += loss.item()
return total_loss / len(test_loader)
# 设置超参数
input_size = train_features.shape[1]
hidden_size = 64
num_hidden_layers = 2
learning_rate = 0.001
num_epochs = 100
# 定义模型、损失函数和优化器
model = NeuralNet(input_size, hidden_size, num_hidden_layers).to(device)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
train_loss = train(model, optimizer, loss_fn, train_loader, device)
test_loss = test(model, loss_fn, test_loader, device)
print("Epoch {} | Train Loss {:.4f} | Test Loss {:.4f}".format(epoch+1, train_loss, test_loss))
```
最后,我们可以使用训练好的模型进行预测:
```python
# 使用训练好的模型进行预测
test_predictions = model(test_features.to(device)).squeeze().detach().cpu().numpy()
test_targets = test_targets.numpy()
# 可视化预测结果
import matplotlib.pyplot as plt
plt.plot(test_targets, label="Actual")
plt.plot(test_predictions, label="Predicted")
plt.legend()
plt.show()
```
这样就可以利用前馈神经网络预测共享单车的使用数量过程了。
阅读全文