Tensorflow.js将simpleRNN添加到model()

时间:2019-05-07 18:43:22

标签: recurrent-neural-network tensorflow.js

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

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

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

没有一个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 );

1 个答案:

答案 0 :(得分:1)

形状不在图层上,而是在对象上由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>