Javascript TensorFlowJS中的单位和输入形状
我不熟悉TensorflowJS和ML 我想知道的是 什么是Javascript TensorFlowJS中的单位和输入形状,javascript,tensorflow,tensorflow.js,Javascript,Tensorflow,Tensorflow.js,我不熟悉TensorflowJS和ML 我想知道的是 什么是inputShape 什么是自动形状 由于unit引用了数据集的属性,为什么unit在model.add(tf.layers.dense({units:4}))行中设置为4。(该层在model.add(tf.layers.dense({units:32,inputShape:[50]}))中将单元定义为32))既然sequential()一层的输出是下一层的输入,那么这些单元不一定是相同的吗 什么是inputShape 它是一个包含张量
inputShape
什么是自动形状
由于unit
引用了数据集的属性,为什么unit
在model.add(tf.layers.dense({units:4}))行中设置为4。(该层在model.add(tf.layers.dense({units:32,inputShape:[50]}))中将单元定义为32
)
)既然sequential()
一层的输出是下一层的输入,那么这些单元不一定是相同的吗
什么是inputShape
它是一个包含张量维数的数组,在运行神经网络时用作输入
什么是自动形状
它只使用之前图层的输出形状。在这种情况下,[32]
,因为前面的层是一个有32个单元的致密层
既然单元引用了数据集的属性,为什么单元设置为
4在model.add(tf.layers.densite({units:4}))
行中。(定义的图层)
model.add(tf.layers.dense)中的单位为32({units:32,inputShape:
[50]}))
,因为sequential()的一层输出是
下一层,单位不是必须相同吗
单位定义密集层的输出形状。在这种情况下,神经应该有4个输出,所以最后一层必须有4个单元。输出和输入形状不必相同,因为每个神经元的输出(其数量为outputshape)是基于前一层的所有神经元(输出)计算的。(如果是致密层)我总是喜欢一个工作示例。这是一个简单的例子
我曾经有一个指向我的网站的链接,上面有50多个TFJS示例,但似乎放一个链接被认为是垃圾邮件,所以我不愿意分享
<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>
…
在链接自己的网站时,必须添加披露,否则可能会被视为垃圾邮件。看,这不是一个好答案。没有任何解释。这段代码基本上是从TensorFlow.js网站复制粘贴的,带有inputShape:[1]
它太简单了,实际上没有用处/它应该是一个简化的答案,以便用户可以看到inputShape是如何工作的。这是一个完整的工作网页没有编译器。Tensorflowjs站点上绝对没有这样容易运行的示例,因为所有Tensorflowjs示例都希望您使用某种编译器。我完全是白手起家的。是的,它可以有更多的评论,但它不会投票。虽然这提供了示例代码,但它对我没有用处,因为它没有解释示例代码在做什么,或者为什么。如果我想要没有解释的示例代码,我可以检查API引用。
<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>