简体   繁体   English

如何创建多维矩阵作为神经网络的输入?

[英]How to create multi-dimensional matrices as input to neural network?

I am trying to create a neural network with a multi-dimensional input matrix.我正在尝试创建一个具有多维输入矩阵的神经网络。 Input could be of size 2x7, 8x7 or any such dimension with 7 columns.输入的大小可以是 2x7、8x7 或任何具有 7 列的此类尺寸。 (This input is used in for loop structure shown below) (此输入用于如下所示的 for 循环结构)

My question is, How to create a training DataFrame that could contain multiple such matrices with different dimensions to feed the neural network?我的问题是,如何创建一个训练 DataFrame 可以包含多个具有不同维度的此类矩阵来馈送神经网络? I tried training model on for loop for every matrix but there should be some more suitable method for creating such a dataset.我尝试在每个矩阵的 for 循环上训练 model,但应该有一些更合适的方法来创建这样的数据集。

Note: I am trying to get a single input with all such different dimensional matrices and that could easily map to their respective outputs.注意:我正在尝试获得具有所有这些不同维度矩阵的单个输入,并且可以轻松地将 map 转换为它们各自的输出。 So input should look like (a, b, 7) where a is the number of data points that are matrices with different lengths of rows, b is the number of rows in that particular matrix and 7 is the number of columns in all the matrices.所以输入应该看起来像 (a, b, 7) 其中a是具有不同行长的矩阵的数据点数, b是该特定矩阵中的行数,7 是所有矩阵中的列数.

The data is an example of time-series data that users created by time.该数据是用户按时间创建的时间序列数据的示例。 So I need to keep each row of single matrices in its order.所以我需要保持每一行单个矩阵的顺序。 And the output will generate the next time a user creates a new row. output 将在用户下次创建新行时生成。 Please understand, the focus of this question is not the model but how to represent this data for the model.请理解,这个问题的重点不是 model,而是如何为 model 表示这个数据。

Here's the code to my model:这是我的 model 的代码:

model.add(Lambda(lambda x: tf.expand_dims(x, axis=1), input_shape=[7]))
model.add(Dense(2048, activation='tanh'))
model.add(Dense(1024, activation='tanh'))
model.add(Dense(512, activation='tanh'))
model.add(Dense(216, activation='tanh'))
model.add(Flatten())
model.add(Dense(128, activation='tanh'))
model.add(Dense(7, activation='tanh'))
model.summary()

My loop to fit model looks like this:我适合 model 的循环如下所示:

for user_id in user_ids:
    df = dailytable[dailytable['user_id']==user_id]
    if df.shape[0] > 3:
        X = df.iloc[:-1, :]
        Y = df.iloc[-1, :]
        Y = tf.reshape(Y, (1, 7))
        if len(X.axes[0].tolist()) > 3:
            model.fit(X, Y, epochs=5, steps_per_epoch=1)

I want to create some data structure which contains all my matrices.我想创建一些包含我所有矩阵的数据结构。 It would look like a 3-dimensional matrix.它看起来像一个 3 维矩阵。 And all those matrices have different input shapes as explained above.如上所述,所有这些矩阵都有不同的输入形状。 But have the same output ie, 1x7.但是具有相同的 output 即 1x7。 Instead of using the loop structure, I want to pass a single input at the start of training.我不想使用循环结构,而是想在训练开始时传递一个输入。

Modelling any temporal series has the same problem.对任何时间序列进行建模都有同样的问题。 You should take a look at CNNs or RNNs since they admit variable dimensions.你应该看看 CNN 或 RNN,因为它们承认可变维度。 In the case of the CNN you can have an input with 7 channels and variable length.在 CNN 的情况下,您可以有 7 个通道和可变长度的输入。

With a fully connected layer you have an input with fixed shape.使用完全连接的层,您有一个固定形状的输入。 So you either apply zero padding to fulfill the maximum size as already suggested or use as input the windowed shifted signal (which is what the CNN does).因此,您要么应用零填充来满足已经建议的最大大小,要么将窗口移位信号用作输入(这是 CNN 所做的)。

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

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