具有不同长度输入参数的模型?

model with a various length of input parameters?

下面的模型,如何保证输入长度是可变的?

const input = tf.input({shape: [5]});
const denseLayer1 = tf.layers.dense({units: 10, activation: 
'relu'});
const denseLayer2 = tf.layers.dense({units: 4, activation: 
'softmax'});
const output = 
denseLayer2.apply(denseLayer1.apply(input));
const model = tf.model({inputs: input, outputs: output});
model.predict(tf.ones([2, 5])).print();

您可以在 inputShape 中使用 null 指定可变长度。它仅适用于维度大于 1 的输入。

const input = tf.input({shape: [null, 2]});
const denseLayer1 = tf.layers.dense({units: 10, activation: 
'relu'});
const denseLayer2 = tf.layers.dense({units: 4, activation: 
'softmax'});
const output = 
denseLayer2.apply(denseLayer1.apply(input));
const model = tf.model({inputs: input, outputs: output});
model.predict(tf.randomNormal([6, 5, 2])).print();
model.predict(tf.randomNormal([6, 4, 2])).print();
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.0"> </script>
  </head>

  <body>
  </body>
</html>