失败的神经预测器

创建日期:2024-06-21
更新日期:2025-02-01
'''
# 失败的神经预测器
'''

import numpy as np
import pandas as pd
import torch
import torch.optim as optim
import matplotlib.pyplot as plt

# 1. 准备数据
data_path = './sample01/data/bike-sharing-dataset/hour.csv'
rides = pd.read_csv(data_path)
rides.head()
counts = rides['cnt'][:50]
x = np.arange(len(counts))
y = np.array(counts)
plt.figure(figsize=(10, 7))
plt.plot(x, y, 'o-')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

# 2. 训练模型
x = torch.FloatTensor(np.arange(len(counts), dtype=float) / len(counts))
y = torch.FloatTensor(np.array(counts, dtype=float))

sz = 10
weights = torch.randn((1, sz), requires_grad=True)
biases = torch.randn((sz), requires_grad=True)
weights2 = torch.randn((sz, 1), requires_grad=True)

learning_rate = 0.001
losses = []
x = x.view(50, -1)
y = y.view(50, -1)
for i in range(100000):
    hidden = x * weights + biases
    hidden = torch.sigmoid(hidden)
    predictions = hidden.mm(weights2)
    loss = torch.mean((predictions - y) ** 2)
    losses.append(loss.data.numpy())
    if i % 10000 == 0:
        print('loss:', loss)

    loss.backward()

    weights.data.add_(-learning_rate*weights.grad.data)
    biases.data.add_(-learning_rate*biases.grad.data)
    weights2.data.add_(-learning_rate*weights2.grad.data)

    weights.grad.zero_()
    biases.grad.zero_()
    weights2.grad.zero_()

plt.plot(losses)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()

# 3. 拟合模型
x_data = x.data.numpy()
plt.figure(figsize=(10, 7))
xplot, = plt.plot(x_data, y.data.numpy(), 'o')
yplot, = plt.plot(x_data, predictions.data.numpy())
plt.xlabel('X')
plt.ylabel('Y')
plt.legend([xplot, yplot], ['Data', 'Prediction under 100000 epochs'])
plt.show()

# 4. 预测模型
counts_predict = rides['cnt'][50:100]
x = torch.FloatTensor(
    (np.arange(len(counts_predict), dtype=float) + len(counts)) / len(counts))
y = torch.FloatTensor(np.array(counts_predict, dtype=float))
hidden = x.expand(sz, len(x)).t() * weights.expand(len(x), sz)
hidden = torch.sigmoid(hidden)
predictions = hidden.mm(weights2)
loss = torch.mean((predictions - y) ** 2)
print(loss)
x_data = x.data.numpy()
plt.figure(figsize=(10, 7))
xplot, = plt.plot(x_data, y.data.numpy(), 'o')
yplot, = plt.plot(x_data, predictions.data.numpy())
plt.xlabel('X')
plt.ylabel('Y')
plt.legend([xplot, yplot], ['Data', 'Prediction'])
plt.show()