简体   繁体   中英

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. (This input is used in for loop structure shown below)

My question is, How to create a training DataFrame that could contain multiple such matrices with different dimensions to feed the neural network? I tried training model on for loop for every matrix but there should be some more suitable method for creating such a dataset.

Note: I am trying to get a single input with all such different dimensional matrices and that could easily map to their respective outputs. 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.

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. Please understand, the focus of this question is not the model but how to represent this data for the model.

Here's the code to my 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:

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. And all those matrices have different input shapes as explained above. But have the same output ie, 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. In the case of the CNN you can have an input with 7 channels and variable length.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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