1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| def load_data(folder_path, num_sites): files = sorted([f for f in os.listdir(folder_path) if f.endswith('.csv')]) all_data = [] for file in files: file_path = os.path.join(folder_path, file) df = pd.read_csv(file_path,encoding='gbk') all_data.append(df.iloc[:, [3]].values) combined_data = np.concatenate(all_data, axis=1) return combined_data
class LSTM_Model(nn.Module): def __init__(self, input_size, hidden_size, output_size, num_layers=4, dropout=0.1): super(LSTM_Model, self).__init__() self.lstm = nn.LSTM(input_size, hidden_size, num_layers=num_layers, batch_first=True, dropout=dropout, bidirectional=False) self.dropout = nn.Dropout(dropout) self.fc1 = nn.Linear(hidden_size, hidden_size) self.fc2 = nn.Linear(hidden_size, output_size) def forward(self, x): h0 = torch.zeros(self.lstm.num_layers, x.size(0), self.lstm.hidden_size).to(x.device) c0 = torch.zeros(self.lstm.num_layers, x.size(0), self.lstm.hidden_size).to(x.device) out, _ = self.lstm(x, (h0, c0)) out = self.dropout(out[:, -1, :]) out = F.relu(self.fc1(out)) out = self.fc2(out) return out
def create_sliding_windows(data, window_size): num_time_steps = data.shape[0] num_features = data.shape[1] windows = [] for i in range(num_time_steps - window_size): windows.append(data[i:i + window_size]) return np.array(windows)
|