Input and parameter tensors are not at the same device, input tensor at cuda:0 and parameter at cpu

部署运行你感兴趣的模型镜像

第一种情况 比较简单的处理是 

x.to(device)

model.to(device)

第二、实际模型没有定义好,有一部分没有定义好,遗漏了

报错信息

  File "/usr/local/lib/python3.10/site-packages/torch/nn/modules/rnn.py", line 951, in forward
    result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,
RuntimeError: Input and parameter tensors are not at the same device, found input tensor at cuda:0 and parameter tensor at cpu

错误的代码

        self.lstm.weight_ih_l0 = PyroSample(
                                    dist.Normal(0, prior_scale)
                                        ).expand([4 * hidden_size, nput_size]).to_event(2))
        self.lstm.bias_ih_l0 = PyroSample(
                                        dist.Normal(
                                            torch.tensor([0.], device=self.device),
                                            torch.tensor([prior_scale], device=self.device)
                                        ).expand([4 * hidden_size]).to_event(1))

修改后的代码 

import torch
import pyro
from pyro.distributions import Normal, Gamma
from pyro.nn import PyroModule, PyroSample
import torch.nn as nn
class Model(PyroModule):
    def __init__(self, input_size=1, num_classes=1, hidden_size=3, num_layers=1,
                 prior_scale=50.0
                 , device='cuda:0'
                 ):
        super().__init__()
        self.device = device
        self.num_classes = num_classes
        self.num_layers = num_layers
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.activation = nn.ReLU()  # or nn.ReLU()
        # Correctly initialize the LSTM layer with bidirectional=False
        self.lstm = PyroModule[nn.LSTM](input_size,
                                        hidden_size,
                                        num_layers,
                                        batch_first=True,
                                        bidirectional=False).to(device)
        self.linear = PyroModule[nn.Linear](hidden_size, 512).to(device)  # Adjusted for unidirectional LSTM
        self.fc = PyroModule[nn.Linear](512, num_classes).to(device)

        # Initialize weights and biases for each layer
        # Input to hidden layer
        self.lstm.weight_ih_l0 = PyroSample(
                                     dist.Normal(
                                            torch.tensor([0.], device=self.device),
                                            torch.tensor([prior_scale], device=self.device)
                                        ).expand([4 * hidden_size, input_size]).to_event(2))
        self.lstm.bias_ih_l0 = PyroSample(
                                        dist.Normal(
                                            torch.tensor([0.], device=self.device),
                                            torch.tensor([prior_scale], device=self.device)
                                        ).expand([4 * hidden_size]).to_event(1))

        # Hidden to hidden layer
        self.lstm.weight_hh_l0 = PyroSample(
                                    dist.Normal(
                                        torch.tensor([0.], device=self.device),
                                        torch.tensor([prior_scale], device=self.device)
                                    ).expand([4 * hidden_size, hidden_size]).to_event(2))
        self.lstm.bias_hh_l0 = PyroSample(
            dist.Normal(
                torch.tensor([0.], device=self.device),
                torch.tensor([prior_scale], device=self.device)
            ).expand([4 * hidden_size]).to_event(1))

        self.linear.weight = PyroSample(
            dist.Normal(
                torch.tensor([0.], device=self.device),
                torch.tensor([prior_scale], device=self.device)
            ).expand([128, hidden_size]).to_event(2))
        self.linear.bias = PyroSample(
            dist.Normal(
                torch.tensor([0.], device=self.device),
                torch.tensor([prior_scale], device=self.device)
            ).expand([128]).to_event(1))
        self.fc.weight = PyroSample(
                                dist.Normal(
                                    torch.tensor([0.], device=self.device),
                                    torch.tensor([prior_scale], device=self.device)
                                ).expand([num_classes, 128]).to_event(2))
        self.fc.bias = PyroSample(
                                    dist.Normal(
                                        torch.tensor([0.], device=self.device),
                                        torch.tensor([prior_scale], device=self.device)
                                    ).expand([num_classes]).to_event(1))

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

import h5py import numpy as np import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import Dataset, DataLoader import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from scipy.io import savemat # 用于保存为 .mat 文件 import warnings warnings.filterwarnings('ignore') # 设置随机种子保证可重复性 torch.manual_seed(42) np.random.seed(42) # 1. 读取数据 def load_data(file_path): """读取MAT文件中的数据""" with h5py.File(file_path, 'r') as f: # 获取real_part数据集 data = f['real_part'][:] # 转置为(8395,)的一维数组(HDF5存储为列向量) data = data.flatten() return data # 2. 数据预处理类 class TimeSeriesDataset(Dataset): def __init__(self, data, seq_length=30, pred_length=30): self.data = data self.seq_length = seq_length self.pred_length = pred_length self.total_length = seq_length + pred_length def __len__(self): return len(self.data) - self.total_length + 1 def __getitem__(self, idx): # 获取输入序列(前seq_length个点) x = self.data[idx:idx + self.seq_length] # 获取目标序列(后pred_length个点) y = self.data[idx + self.seq_length:idx + self.seq_length + self.pred_length] return torch.FloatTensor(x), torch.FloatTensor(y) # 3. 定义神经网络模型 class TimeSeriesPredictor(nn.Module): def __init__(self, input_size=1, hidden_size=64, num_layers=2, output_size=30): super(TimeSeriesPredictor, self).__init__() self.hidden_size = hidden_size self.num_layers = num_layers # LSTM层 self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, dropout=0.2) # 全连接层 self.fc = nn.Sequential( nn.Linear(hidden_size, 32), nn.ReLU(), nn.Dropout(0.2), nn.Linear(32, output_size) ) def forward(self, x): # x形状: (batch_size, seq_length, input_size) batch_size = x.size(0) # 初始化隐藏状态 h0 = torch.zeros(self.num_layers, batch_size, self.hidden_size).to(x.device) c0 = torch.zeros(self.num_layers, batch_size, self.hidden_size).to(x.device) # LSTM前向传播 lstm_out, _ = self.lstm(x, (h0, c0)) # 只取最后一个时间步的输出 out = lstm_out[:, -1, :] # (batch, hidden_size) # 全连接层 out = self.fc(out) # (batch, output_size=30) return out # 4. 训练函数 def train_model(model, train_loader, val_loader, criterion, optimizer, num_epochs=100): train_losses = [] val_losses = [] for epoch in range(num_epochs): # 训练模式 model.train() train_loss = 0.0 for batch_x, batch_y in train_loader: batch_x = batch_x.unsqueeze(-1) # 添加特征维度 -> (batch, 30, 1) batch_y = batch_y # 目标: (batch, 30) # 前向传播 outputs = model(batch_x) loss = criterion(outputs, batch_y) # 反向传播 optimizer.zero_grad() loss.backward() optimizer.step() train_loss += loss.item() # 验证模式 model.eval() val_loss = 0.0 with torch.no_grad(): for batch_x, batch_y in val_loader: batch_x = batch_x.unsqueeze(-1) batch_y = batch_y outputs = model(batch_x) loss = criterion(outputs, batch_y) val_loss += loss.item() # 计算平均损失 avg_train_loss = train_loss / len(train_loader) avg_val_loss = val_loss / len(val_loader) train_losses.append(avg_train_loss) val_losses.append(avg_val_loss) if (epoch + 1) % 20 == 0: print(f'Epoch [{epoch + 1}/{num_epochs}], Train Loss: {avg_train_loss:.6f}, Val Loss: {avg_val_loss:.6f}') # 学习率调度 scheduler.step(avg_val_loss) return train_losses, val_losses # 5. 主函数 def main(): # 文件路径 file_path = r'F:\beng\test\12.3\real_part.mat' output_mat_file = r'F:\beng\test\12.3\test_predictions_results.mat' # 输出的.mat文件路径 # 读取数据 print("正在读取数据...") data = load_data(file_path) print(f"数据形状: {data.shape}") # 数据划分 train_size = 8030 train_data = data[:train_size] test_data = data[train_size - 30:] # 保留前30个作为第一个样本的输入 # 数据标准化 scaler = MinMaxScaler(feature_range=(-1, 1)) train_data_scaled = scaler.fit_transform(train_data.reshape(-1, 1)).flatten() test_data_scaled = scaler.transform(test_data.reshape(-1, 1)).flatten() # 创建数据集 train_dataset = TimeSeriesDataset(train_data_scaled, seq_length=30, pred_length=30) test_dataset = TimeSeriesDataset(test_data_scaled, seq_length=30, pred_length=30) # 划分训练集和验证集 train_subset_size = int(0.8 * len(train_dataset)) val_subset_size = len(train_dataset) - train_subset_size train_subset, val_subset = torch.utils.data.random_split( train_dataset, [train_subset_size, val_subset_size], generator=torch.Generator().manual_seed(42) ) # 创建数据加载器 batch_size = 32 train_loader = DataLoader(train_subset, batch_size=batch_size, shuffle=True) val_loader = DataLoader(val_subset, batch_size=batch_size, shuffle=False) test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False) # 创建模型 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(f"Using device: {device}") model = TimeSeriesPredictor(input_size=1, hidden_size=64, num_layers=2, output_size=30).to(device) # 定义损失函数和优化器 criterion = nn.MSELoss() optimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-5) scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=10, factor=0.5, verbose=True) # 训练模型 print("开始训练模型...") train_losses, val_losses = train_model(model, train_loader, val_loader, criterion, optimizer, num_epochs=100) # 测试模型 print("开始测试模型...") model.eval() test_predictions = [] # 存储反归一化后的预测值 test_targets = [] # 存储反归一化后的真实值 with torch.no_grad(): for batch_x, batch_y in test_loader: batch_x = batch_x.unsqueeze(-1).to(device) # (1, 30, 1) output = model(batch_x) # (1, 30) # 移动到 CPU 并转为 numpy pred = output.cpu().numpy().flatten() # (30,) true = batch_y.numpy().flatten() # (30,) # 反归一化:注意需要 reshape 成二维才能用 scaler.inverse_transform pred_original = scaler.inverse_transform(pred.reshape(-1, 1)).flatten() true_original = scaler.inverse_transform(true.reshape(-1, 1)).flatten() test_predictions.append(pred_original) test_targets.append(true_original) # 转换为 numpy 数组 test_predictions = np.array(test_predictions) # shape: (N_test_samples, 30) test_targets = np.array(test_targets) # shape: (N_test_samples, 30) # 计算整体测试指标 mse = np.mean((test_predictions - test_targets) ** 2) mae = np.mean(np.abs(test_predictions - test_targets)) rmse = np.sqrt(mse) print("\n=== 测试集效果 ===") print(f"MSE (均方误差): {mse:.6f}") print(f"MAE (平均绝对误差): {mae:.6f}") print(f"RMSE (均方根误差): {rmse:.6f}") # ==================== 新增:保存预测结果为 .mat 文件 ==================== results = { 'test_predictions': test_predictions, # 预测值 'test_targets': test_targets, # 真实值 'test_mse': mse, 'test_mae': mae, 'test_rmse': rmse, 'model_type': 'LSTM_Seq2One', # 可选元信息 'seq_length_in': 30, 'seq_length_out': 30 } try: savemat(output_mat_file, results) print(f"\n✅ 测试结果已成功保存至: {output_mat_file}") except Exception as e: print(f"\n❌ 保存失败: {e}") # ==================== 可视化 ==================== plt.figure(figsize=(15, 10)) # 绘制损失曲线 plt.plot(train_losses, label='Training Loss', color='blue') plt.xlabel('Epoch') plt.ylabel('Loss (MSE)') plt.title('Training Loss') plt.legend() plt.grid(True, alpha=0.3) if __name__ == "__main__": main() 报错RuntimeError: Input and parameter tensors are not at the same device, found input tensor at cpu and parameter tensor at cuda:0
最新发布
12-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值