Tensorflow.js 将 simpleRNN 添加到 model()

Tensorflow.js adding simpleRNN to model()

我想构建一个半复杂的神经网络,所以我没有使用 tf.seqential()。

const model = tf.model( {
    inputs: [tickInput,boardInput],
    outputs:moveChoices,
} );

根据我的理解,必须在定义输出后创建...

None 个 tfjs 示例在模型中使用了 simpleRNN()。

图层与 .apply( inputLayer ) 合并;据我所知,这将它们更改为 'built=true' 但我的简单 RNN 没有 .shape() 所以我不能

(node:8616) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
at Dense.build (m:\javascript\tensorflow\node_modules\@tensorflow\tfjs-layers\src\layers\core.ts:277:48)
at m:\javascript\tensorflow\node_modules\@tensorflow\tfjs-layers\src\engine\topology.ts:991:14
at Object.nameScope (m:\javascript\tensorflow\node_modules\@tensorflow\tfjs-layers\src\common.ts:43:20)
at Dense.Layer.apply (m:\javascript\tensorflow\node_modules\@tensorflow\tfjs-layers\src\engine\topology.ts:977:12)
at test3 (file:///m:/javascript/tensorflow/test2.mjs:105:14)
at file:///m:/javascript/tensorflow/test2.mjs:128:1

这是我的代码...

    const batchSize= 1;

const boardInput = tf.layers.input({batchShape:[batchSize, 160, 40*7]});

const tickMask = tf.input( {
    name : "tick",
    batchShape : [batchSize, 160, 1],
    dtype : 'bool',
})
// I expect other layers on input/output before concatenate() 
// but, the conv1d() also wouldn't have a shape.

var concatLayer = tf.layers.concatenate( )
var merge = concatLayer.apply([tickMask, boardInput]);
console.log(JSON.stringify(merge.shape));

const simpleRNNConfig = {
    name : 'theBrain',
    units : 32,
    activation : "relu", 
    useBias : true,
    kernelInializer : 'randomNomral', 
    recurrentInitializer : 'randomNormal',
    biasInitializer : 'randomNormal',

    dropout : 0.10,
    recurrentDropout : 0,

    returnSequences : false, 
    returnState : false, // or true
    goBackwards : false, 
    stateful : false, 
}
var theBrain = tf.layers.simpleRNN( simpleRNNConfig );
theBrain.apply( merge );
console.log( JSON.stringify( theBrain.shape ));

// THE ABOVE CONSOLE.LOG is 'UNDEFINED' 

var moveChoices = tf.layers.dense( { units : 40, activation: "softmax" } )
// and then the following line has the above exception 
// above 'no .length' because theBrain doesn't have a 
// .shape to make the shapeList....
moveChoices.apply( theBrain );

形状不在图层上,而是在对象上 return by apply

var theBrain = tf.layers.simpleRNN( simpleRNNConfig );
output = theBrain.apply( merge );
console.log( JSON.stringify( output.shape ));

这是一个简单的模型,可以满足您的需求:

    const input1 = tf.input({shape: [2, 2]});
    const input2 = tf.input({shape: [2, 3]});
    const concatLayer = tf.layers.concatenate();
    const concat = concatLayer.apply([input1, input2]);
    
    const rnn = tf.layers.simpleRNN({units: 8, returnSequences: true});
    
    const output = rnn.apply(concat);
    
    console.log(JSON.stringify(output.shape));
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>
  </head>

  <body>
  </body>
</html>