简体   繁体   English

Keras 中 Conv1D 的输入层

[英]Input Layer for Conv1D in Keras

Let me explain my problem and the dataset a bit.让我稍微解释一下我的问题和数据集。 In my dataset, i have hourly measurements of a variable x and 7 more columns representing the day of the week that that measurment was taken as dummy variables.在我的数据集中,我每小时测量一个变量x和另外 7 列代表该测量被视为虚拟变量的星期几。 So, it is something like this:所以,它是这样的:

DateTime          x   Mon Tue Wed Thur Fri Sat Sun
2017/01/01 00:00  10   0   0   0   0    0   0   1
2017/01/01 01:00  15   0   0   0   0    0   0   1
2017/01/01 02:00  21   0   0   0   0    0   0   1
             ...
2017/01/01 23:00  32   0   0   0   0    0   0   1
2017/01/02 00:00  17   1   0   0   0    0   0   0
2017/01/02 01:00  19   1   0   0   0    0   0   0
2017/01/02 02:00  48   0   0   0   0    0   0   1
             ...
2022/08/15 00:00  43   0   0   0   0    0   0   1

This dataset has shape of (49249, 8) .该数据集的形状为(49249, 8)

I am using data from 2017~2021 to train, and only 2022 would be used for test.我使用 2017~2021 年的数据进行训练,只有 2022 年用于测试。

Using two days of data ( 48 rows ), i need to forecast x in the next day.使用两天的数据( 48 rows ),我需要在第二天预测x After changing this dataset to look like a supervised learning, i got a new training dataset with the shape (1842, 55) .在将此数据集更改为看起来像监督学习后,我得到了一个形状为(1842, 55)的新训练数据集。 55 is because im using two days of data ( 24+24 ) plus the dummy variables represnting the day i want to make a forecast ( 7 variables) 24+24+7 = 55 . 55是因为我使用了两天的数据( 24+24 )加上代表我要进行预测的那一天的虚拟变量( 7变量) 24+24+7 = 55

Now, according Keras documentation the InputShape of a Conv1D layer should have shape of (batch_size, steps, input_dim) .现在,根据Keras 文档InputShape层的Conv1D应该具有(batch_size, steps, input_dim)的形状。

So i did the following:所以我做了以下事情:

max_batch_size = 1824
steps = 48
input_dim = 55

So, i tried to reshaped it:所以,我试图重塑它:

dataset = dataset.reshape(max_batch_size, steps, input_dim)

And i got this error:我得到了这个错误:

ValueError: cannot reshape array of size 100320 into shape (1824,48,55)

I know this have been asked a lot, but i still don't understand it and none of the answers i have seen here have worked for me.我知道这已经被问了很多,但我仍然不明白,我在这里看到的答案都没有对我有用。 How should i set my InputLayer to use with a Conv1D layer?我应该如何设置我的InputLayer以与Conv1D层一起使用?

in this case you are trying to solve a sequence problem, so if i got it true, you would need only the output values, Make a list of values which are the outputs and then by在这种情况下,您正在尝试解决序列问题,所以如果我做到了,您只需要 output 值,列出作为输出的值,然后按

dataset = tf.data.Dataset.from_tensor_slices(list_of_values)

take the list of values, you said you would need two days to forecast the next day;取值列表,您说您需要两天时间来预测第二天; so based on that you should windowing the output of the function above with the window size of two days, and one day, so it would mean it will take 48 values and then forecast for 24 values on the next day.因此,基于此,您应该将上述 function 的 output 与 window 大小为两天和一天,然后预测第二天的 48 值的 4 个值。 so then;那么那么;

data = data.window(window_size = 48 + 24, drop_remainder = True)
#set drop_remainder to true to drop sequences that are not in the same length as the others

the flat map and shuffle data as like as below扁平 map 和随机数据如下

data = data.flat_map(lambda window : window.batch(48+24))
data = data.shuffle(shuffle_buffer)

now you should map data to separate the inputs and the outputs, somthing like as below现在您应该使用 map 数据来分离输入和输出,如下所示

data = data.map(window : (window[:48], window[48:])
#by this way you set window size 48 values as the input and the others 24 values as the outputs (forecast)

and now set its batch size现在设置它的批量大小

data = data.batch(batch_size).prefetch()

now your data in windowed dataset which could be fed into convolution 1D and for the input layer there are two methods;现在您在窗口数据集中的数据可以输入到卷积一维中,对于输入层有两种方法; you could set input shape into (48,) and then define lambda layer with lambda which reshape or expand dimensions or even set the input shape equal to (48,1) and after that it would fed into Conv1D layer您可以将输入形状设置为 (48,),然后使用 lambda 定义 lambda 层,该层重塑或扩展尺寸,甚至将输入形状设置为等于 (48,1),然后将其馈入 Conv1D 层

Notice In your case, by this way, the model would get 48 values in any format as i mean from each time of a day, but if your model would get its inputs from certain time for example 12 AM, so it would need to define shift parameter in window function and set it equal to 24 to take certain 24 values of each day from 12 AM注意在您的情况下,通过这种方式,model 将获得 48 个任何格式的值,我的意思是从一天中的每个时间开始,但是如果您的 model 将从特定时间(例如上午 12 点)获取其输入,因此它需要定义在 window function 中的shift参数并将其设置为 24 以从上午 12 点开始每天取某些 24 个值

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

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