各种长度的输入参数的模型?

时间:2018-10-31 17:31:40

标签: javascript tensorflow tensorflow.js

使用以下模型,如何确保输入长度可变?

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();

1 个答案:

答案 0 :(得分:0)

您可以在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>