我正在尝试为项目复制以下批处理生成器。但是我在重塑数据时遇到问题。该函数的目标是获取 [6000,3000] 数组并将其重塑为 [batch_size, 100,3000,1]。
我正在尝试复制的功能代码
def gen(dict_files, aug=False):
while True:
record_name = random.choice(list(dict_files.keys()))
batch_data = dict_files[record_name]
all_rows = batch_data['x']
for i in range(batch_size):
start_index = random.choice(range(all_rows.shape[0]-WINDOW_SIZE))
X = all_rows[start_index:start_index+WINDOW_SIZE, ...]
Y = batch_data['y'][start_index:start_index+WINDOW_SIZE]
X = np.expand_dims(X, 0)
Y = np.expand_dims(Y, -1)
Y = np.expand_dims(Y, 0)
yield X, Y
gen 输出 X、Y:
X.shape=(batch_size, 100, 3000, 1)
Y.shape=(batch_size, 100, 1)
My code:
参数定义:
特征 = 数组[6000,3000] & 标签 = 数组[6000,]
def generator(features, labels, batch_size):
##Define batch shapes
X_train_batch = np.zeros((batch_size,100, 3000, 1))
y_train_batch = np.zeros((batch_size,100, 1))
while True:
sample_index = random.choice(range(features.shape[0]))
sample_data = features[sample_index]
##Generating training batches
for i in range(batch_size):
start_index = random.choice(range(sample_data.shape[0]-100)) #pick random start point in signal (of length 3000timesteps)
X = sample_data[start_index:start_index + 100, ...] #record 100 timesteps in signal from rand start point
Y = labels[start_index:start_index + 100] #Record classification of X
#print(X.shape) #gives arrays of (100,), should be (100,3000)
##reshaping to input shape taken by model
X = np.expand_dims(X, 0)
Y = np.expand_dims(Y, -1)
Y = np.expand_dims(Y, 0)
##Collecting samples into correct batch size
#X_train_batch[i] = X
#y_train_batch[i] = Y
print(y_train_batch.shape) #gives (32,100,1) which is correct!
generator(features, labels, 32)
有人可以解释这里找到的省略号('...')的功能吗X = all_rows[start_index:start_index+WINDOW_SIZE, ...]
?根据我的理解,省略号放置了 Sample_data 的 3000 个时间步来给出输出 (100,3000),但我显然不理解某些内容,因为我无法在代码中获得相同的行为。
接下来我如何复制我的代码输出的内容?