Home > Enterprise >  lstm different input and output shape
lstm different input and output shape

Time:02-27

input shape

tf.tensor3d([
    [
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
    ],
    [
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
      [0.01, 0.02, 0.03],
    ],
    [
      [0.03, 0.05, 0.06],
      [0.03, 0.05, 0.06],
      [0.03, 0.05, 0.06],
      [0.03, 0.05, 0.06],
      [0.01, 0.02, 0.03],
    ],
  ]);

output shape

  const ys = tf.tensor3d([
    [
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
    ],
    [
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
    ],
    [
      [-0.03, 0.05, 0.06],
      [0.03, -0.05, 0.06],
      [0.03, 0.05, -0.06],
    ],
  ]);

I am trying to use the lstm layers to create a prediction model. The problem is that I just know how to change units variable of lstm layers only.

I've been looking for a way to convert to tensor3d but with different rows. I could only find a way to turn it to 1d or 2d shape.

  model.add(
    tf.layers.lstm({
      units: 30,
      returnSequences: true,
      inputShape: [5, 3],
      batchInputShape: [3, 3, 3],
    })
  );
  model.add(tf.layers.lstm({ units: 3, returnSequences: true }));
  // Prepare the model for training: Specify the loss and the optimizer.
  model.compile({ loss: "meanSquaredError", optimizer: "adam" });

Which layers and variables do I have to put in there to turn the input of [3,5,3] to [3,3,3]?

CodePudding user response:

Here is an example of what can be done

 const model = tf.sequential();

 model.add(
   tf.layers.lstm({
    units: 30,
    returnSequences: true,
    inputShape: [5, 3],
    batchInputShape: [3, 3, 3],
   })
 );
 model.add(tf.layers.lstm({ units: 3, returnSequences: true }));
 model.add(tf.layers.flatten());
 // flatten is used so as to be able to change the size of the second dimension using the dense layer   
 model.add(tf.layers.dense({ units: 15}));
 // dense allow to remap the size of the previous layer to a different size 

 model.add(tf.layers.reshape({targetShape: [5, 3]}))
 // reshape to the appropriate shape
 model.summary() // will print the shape of all the layers; last layer will be [3, 5, 3]
  • Related