-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
设备不一致问题 #4
Comments
@GDUT-ZJJ 看来这个问题仍然存在,之前在 百度贴吧 测试时都没注意到这里出错了🤣 错误堆栈
运行环境
|
🎉 解决方案已找到@abandol @GDUT-ZJJ @XiuzeZhou 👋🏼 Caution 每次运行 需要修改以下部分: # 这是 MLP.ipynb 中的第8个 Python 单元格
# 对应编号 In [8]
class Net(nn.Module):
def __init__(self, feature_size=8, hidden_size=[16, 8]):
super(Net, self).__init__()
self.feature_size, self.hidden_size = feature_size, hidden_size
# 主要改动从下面开始,所有 PyTorch Tensor 或 Module 需要显式移动到目标设备上
# 否则设备将默认位于 CPU 上,疑似 @XiuzeZhou 未使用 CUDA 设备测试
self.layer0 = nn.Linear(self.feature_size, self.hidden_size[0]).to(device)
self.layers = [
nn.Sequential(
nn.Linear(self.hidden_size[i], self.hidden_size[i + 1]).to(device),
nn.ReLU().to(device),
).to(device)
for i in range(len(self.hidden_size) - 1)
]
self.linear = nn.Linear(self.hidden_size[-1], 1).to(device)
# 核心改动区域结束
def forward(self, x):
out = self.layer0(x)
for layer in self.layers:
out = layer(out)
out = self.linear(out)
return out # 这是 MLP.ipynb 的第9个 Python 单元格
# 对应编号 In [9]
def tain(
LR=0.01,
feature_size=8,
hidden_size=[16, 8],
weight_decay=0.0,
window_size=8,
EPOCH=1000,
seed=0,
):
mae_list, rmse_list, re_list = [], [], []
result_list = []
for i in range(4):
name = Battery_list[i]
train_x, train_y, train_data, test_data = get_train_test(
Battery, name, window_size
)
train_size = len(train_x)
print("sample size: {}".format(train_size))
setup_seed(seed)
# 核心改动从此处开始
model = Net(feature_size=feature_size, hidden_size=hidden_size).to(device)
optimizer = torch.optim.Adam(
model.parameters(), lr=LR, weight_decay=weight_decay
)
criterion = nn.MSELoss().to(device)
# 核心改动结束
test_x = train_data.copy()
loss_list, y_ = [0], []
for epoch in range(EPOCH):
X = np.reshape(train_x / Rated_Capacity, (-1, feature_size)).astype(
np.float32
)
y = np.reshape(train_y[:, -1] / Rated_Capacity, (-1, 1)).astype(np.float32)
X, y = torch.from_numpy(X).to(device), torch.from_numpy(y).to(device)
output = model(X)
loss = criterion(output, y)
optimizer.zero_grad() # clear gradients for this training step
loss.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients
if (epoch + 1) % 100 == 0:
test_x = train_data.copy() # 每100次重新预测一次
point_list = []
while (len(test_x) - len(train_data)) < len(test_data):
x = np.reshape(
np.array(test_x[-feature_size:]) / Rated_Capacity,
(-1, feature_size),
).astype(np.float32)
x = torch.from_numpy(x).to(device)
pred = model(
x
) # 测试集 模型预测#pred shape为(batch_size=1, feature_size=1)
# 核心改动从此处开始
# 变量 model 和 x 均位于目标设备(GPU)上,需要将其取回 CPU
# 才能对其调用 .data 方法并进一步 .numpy() 转换为 NumPy 数组
next_point = pred.cpu().data.numpy()[0, 0] * Rated_Capacity
# 核心改动结束
test_x.append(next_point) # 测试值加入原来序列用来继续预测下一个点
point_list.append(next_point) # 保存输出序列最后一个点的预测值
y_.append(point_list) # 保存本次预测所有的预测值
loss_list.append(loss)
mae, rmse = evaluation(y_test=test_data, y_predict=y_[-1])
re = relative_error(
y_test=test_data, y_predict=y_[-1], threshold=Rated_Capacity * 0.7
)
print(
"epoch:{:<2d} | loss:{:<6.4f} | MAE:{:<6.4f} | RMSE:{:<6.4f} | RE:{:<6.4f}".format(
epoch, loss, mae, rmse, re
)
)
if (len(loss_list) > 1) and (abs(loss_list[-2] - loss_list[-1]) < 1e-5):
break
mae, rmse = evaluation(y_test=test_data, y_predict=y_[-1])
re = relative_error(
y_test=test_data, y_predict=y_[-1], threshold=Rated_Capacity * 0.7
)
mae_list.append(mae)
rmse_list.append(rmse)
re_list.append(re)
result_list.append(y_[-1])
return re_list, mae_list, rmse_list, result_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
您好,MLP还是有变量不在同一设备的问题。
The text was updated successfully, but these errors were encountered: