class TimeWindowSequence(Sequence): def __init__(self, x, y, batch_size, window_size): self.x = x self.y = y self.batch_size = batch_size self.window_size = window_size self.window_count = int(np.ceil(x.shape[0] / window_size)) def __len__(self): return int(np.ceil(self.x.shape[0] / self.batch_size)) def __getitem__(self, idx): batch_x = np.zeros((self.batch_size, self.window_size, self.x.shape[1])) batch_y = np.zeros((self.batch_size, self.y.shape[1])) for i in range(self.batch_size): j = idx * self.batch_size + i if j >= self.window_count: break window_x = self.x[j*self.window_size:(j+1)*self.window_size, :] window_y = self.y[j*self.window_size:(j+1)*self.window_size, :] batch_x[i, :window_x.shape[0], :] = window_x batch_y[i, :] = window_y[-1, :] return batch_x, batch_y出现
时间: 2024-03-23 14:42:56 浏览: 171
这段代码中似乎缺少了一个缩进,导致 `return` 语句在 `for` 循环内部。可以尝试将 `return` 语句缩进到 `for` 循环外部,如下所示:
```
class TimeWindowSequence(Sequence):
def __init__(self, x, y, batch_size, window_size):
self.x = x
self.y = y
self.batch_size = batch_size
self.window_size = window_size
self.window_count = int(np.ceil(x.shape[0] / window_size))
def __len__(self):
return int(np.ceil(self.x.shape[0] / self.batch_size))
def __getitem__(self, idx):
batch_x = np.zeros((self.batch_size, self.window_size, self.x.shape[1]))
batch_y = np.zeros((self.batch_size, self.y.shape[1]))
for i in range(self.batch_size):
j = idx * self.batch_size + i
if j >= self.window_count:
break
window_x = self.x[j*self.window_size:(j+1)*self.window_size, :]
window_y = self.y[j*self.window_size:(j+1)*self.window_size, :]
batch_x[i, :window_x.shape[0], :] = window_x
batch_y[i, :] = window_y[-1, :]
return batch_x, batch_y
```
这样应该就可以解决这个问题了。
阅读全文