简体   繁体   English

处理 1D CNN 中的批量大小和时间步长

[英]Dealing with batch size and time step in 1D CNN

I have a batch generator which gives me data in the shape of (500, 1, 12) (ie corresponding to (batch size, time steps, features) ).我有一个批处理生成器,它为我提供(500, 1, 12)形状的数据(即对应于(batch size, time steps, features) )。

def batch_generator(batch_size, gen_x,gen_y): 
    batch_features = np.zeros((batch_size,1, 12))
    batch_labels = np.zeros((batch_size,9))
    while True:
        for i in range(batch_size):
            batch_features[i] = next(gen_x)
            batch_labels[i] = next(gen_y)
        yield batch_features, batch_labels

def generate_X():
    while True:
        with open("/my_path/my_data.csv") as f:
            for line in f:
                currentline = line.rstrip('\n').split(",")
                currentline = np.asarray(currentline)
                currentline = currentline.reshape(1,1,12)
                yield currentline

def generate_y():
    while True:
        for i in range(len(y_train)):
            y= y_train[i]
            yield y

I then try to feed this into a 1D-CNN:然后我尝试将其输入 1D-CNN:

model = Sequential()
model.add(Conv1D(filters=100, kernel_size=1, activation='relu', input_shape=(1,12), data_format="channels_last"))

But now I am not able to use a kernel size of more than 1 (ie kernel_size = 1 ).但现在我无法使用大于 1 的 kernel 大小(即kernel_size = 1 )。 This is probably because my time step is equal to 1.这可能是因为我的时间步长等于 1。

How can I use the whole batch size as input to the 1D-CNN and increase the kernel_size ?如何使用整个批量大小作为 1D-CNN 的输入并增加kernel_size

Keep in mind that 1D-convolution is used when each of our input samples is a sequence, ie data in which the order of values are important/specified, like stock market values over a week or the weather temperature values over a period of month or a sequence of genomes or words.请记住,当我们的每个输入样本都是一个序列时,将使用一维卷积,即值的顺序很重要/指定的数据,例如一周内的股市值或一个月内的天气温度值或一系列基因组或单词。 With that said, considering your data, there are three different scenarios:话虽如此,考虑到您的数据,存在三种不同的情况:

  • If each line in your csv file is a sequence of length 12, then you are dealing with samples of shape (12,1) , ie in each sample there are 12 timesteps where each timestep has only on feature.如果 csv 文件中的每一行都是长度为 12 的序列,那么您正在处理形状为(12,1)的样本,即在每个样本中有 12 个时间步,其中每个时间步仅具有特征。 So you should reshape it accordingly (ie to (12,1) and not to (1,12) ).所以你应该相应地重塑它(即(12,1)而不是(1,12) )。

  • However, if each line is not a sequence by itself, but a group of consecutive lines form a sequence, then you must generate your data accordingly: each sample would consists of multiple consecutive lines, eg if we consider the number of timesteps to be 10 then lines #1 to #10 would be a sample, lines #2 to #12 would be another sample, and so on.但是,如果每一行本身不是一个序列,而是一组连续的行形成一个序列,那么您必须相应地生成数据:每个样本将由多个连续的行组成,例如,如果我们认为时间步数为 10那么第 1 到第 10 行将是一个样本,第 2 到第 12 行将是另一个样本,依此类推。 And in this case each sample would have a shape of (number_of_timesteps, 12) (in the example I mentioned it would be (10,12) ).在这种情况下,每个样本的形状都是(number_of_timesteps, 12) (在我提到的示例中,它是(10,12) )。 Now you can create and generate these samples by writing a custom function, or alternatively you could load all of the data as a numpy array and then use TimeseriesGenerator to do it for you.现在,您可以通过编写自定义 function 来创建和生成这些样本,或者您可以将所有数据加载为 numpy 数组,然后使用TimeseriesGenerator为您完成。

  • If none of the two cases above apply, then it's very likely that your data is not a sequential at all and therefore using 1D-CNN (or any other sequence processing model like RNNs) does not make sense for this data.如果上述两种情况均不适用,那么您的数据很可能根本不是序列数据,因此使用 1D-CNN(或任何其他序列处理 model,如 RNN)对这些数据没有意义。 Instead, you should use other suitable architectures.相反,您应该使用其他合适的架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM