TensorFlow JS 中的单位和输入形状

Units and inputShape in TensorFlowJS

我是 TensorflowJS 和 ML 的新手。在API Reference中,下面的代码是there.

const model = tf.sequential();

// First layer must have an input shape defined.
model.add(tf.layers.dense({units: 32, inputShape: [50]}));

// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 4}));

// Inspect the inferred shape of the model's output, which equals
// `[null, 4]`. The 1st dimension is the undetermined batch dimension; the
// 2nd is the output size of the model's last layer.
console.log(JSON.stringify(model.outputs[0].shape));

我想知道的是,

什么是inputShape

什么是自动形状?

既然unit指的是数据集的属性,为什么unitmodel.add(tf.layers.dense({units: 4}))行设置为4。 (该层在model.add(tf.layers.dense({units: 32, inputShape: [50]}))中将unit定义为32)由于sequential()一层的输出是下一层的输入,所以单位不是必须相同吗?

What is inputShape ?

这是一个包含张量维度的数组,在运行神经网络时用作输入。

What is the automatic shape?

它只是使用之前层的输出形状。在这种情况下 [32] 因为之前的层是一个具有 32 个单元的密集层。

Since the unit referred to attributes of the data set, why unit set to 4 in model.add(tf.layers.dense({units: 4})) line. (the layer defined the unit as 32 in model.add(tf.layers.dense({units: 32, inputShape: [50]}))) Since sequential()'s outputs of one layer are the inputs to the next layer, the aren't the units must be same?

单位定义密集层的输出形状。在这种情况下,神经网络应该有 4 个输出,所以最后一层必须有 4 个单元。输出形状和输入形状不必相同,因为每个神经元的输出(其数量是输出形状)是根据前一层的所有神经元(输出)计算的。 (在密集层的情况下)

我总是喜欢一个有效的例子。这是我能做的最简单的例子。

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.3"> </script> 

<input type="number" id="myAsk" value="5"><br> 
 
<input id="myButton123" type="button" value="Keras Layers Train and Test" onclick="{
       document.getElementById('myButton123').style.backgroundColor = 'red'                                                                              
                                                                                
    model = tf.sequential(); // no const so that it is a global variable 

    model.add(tf.layers.dense({ units: 10,  inputShape: [1] }) );  
    model.add(tf.layers.dense({ units: 10 }) );  
    model.add(tf.layers.dense({ units:  1 }) );  

   model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});

   // Generate some synthetic data for training.
   const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]);
   const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]);

                                                                  
  (async function () {   // inline async so we can use promises and await
                                                                                    
    for (let myLoop = 1; myLoop <= 100; myLoop++) {                                                                                 
        var myFit = await model.fit(xs, ys, { epochs: 10 });
        if (myLoop % 20 == 0){   
             await tf.nextFrame();   // This allows the GUI to update but only every 20 batches      
             document.getElementById('myDiv123').innerHTML  =  'Loss after Batch ' + myLoop + ' : ' + myFit.history.loss[0] +'<br><br>'                                                                           
        }
                                                                   
    }                                                                                    
                                                                                    
                                                                                 
    const myPredictArray = await  model.predict(tf.tensor2d([document.getElementById('myAsk').value.split(',')], [1, 1]))  
                                                                                    
    document.getElementById('myDiv123').innerHTML += 'Input '+document.getElementById('myAsk').value+', Output = ' + await myPredictArray.data() +'<br>'
    document.getElementById('myButton123').style.backgroundColor = 'lightgray'                                                                                
                                                                            
  })() // end the inline async funciton                                                                        
                                                                            
                                                                                                                                                       
}" style="background-color: red;">   

 
<input id="myButton123b" type="button" value="re-Test" onclick="{
   (async function () {                                                                
   const myPredictArray = await  model.predict(tf.tensor2d([document.getElementById('myAsk').value.split(',')], [1, 1]))  
                                                                                    
   document.getElementById('myDiv123').innerHTML = 'Input '+document.getElementById('myAsk').value+', Output = ' + await myPredictArray.data() +'<br>'
   })() // end the inline async funciton                                                                                     
                                                                                  
 }"><br><br>

<div id='myDiv123'>...</div><br>