XOR神经网络返回垃圾

时间:2018-10-24 09:09:42

标签: javascript tensorflow neural-network xor tensorflow.js

我在Tensorflow.js中的“异或”神经网络不断返回垃圾预测,并且损失始终停留在 0.25 。我不知道我做错了什么。谢谢您的帮助!

<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.13.0/tf.min.js"></script>
IN.Event.on(IN, 'systemReady', function() {
    var shareLink = document.getElementById('shareLink');
    shareLink.onclick = function(){
      event.preventDefault();
      var params = {
        "comment": "Check out developer.linkedin.com! https://www.example.com",
        "visibility": {
          "code": "anyone"
        }
      };

      IN.API.Raw("/people/~/shares?format=json")
      .method("POST")
      .body(JSON.stringify(params))
      .result(function(xhrResult){
        alert('success :)');
      })
      .error(function(errorObj){
        alert('Error');
        alert(errorObj.errorMessage);
      });
    };

  });

1 个答案:

答案 0 :(得分:1)

我已经更改了优化程序,它可以按预期进行预测。

    [[0.0156993],
     [0.985333 ],
     [0.9862437],
     [0.0150503]]

const model = tf.sequential();

model.add(tf.layers.dense({units: 2, activation: 'sigmoid', inputShape: [2]}));
model.add(tf.layers.dense({units: 1, activation: 'sigmoid'}));
const optimizer = tf.train.adam(0.01);
model.compile({loss:'meanSquaredError', optimizer: optimizer });

const xs = tf.tensor2d([[0,0],[0,1],[1,0],[1,1]]);
const ys = tf.tensor2d([[0],[1],[1],[0]]);

async function train() {
    for(let i = 0; i < 200; i++){
        const history = await model.fit(xs, ys, {epochs: 20, shuffle: true});
        console.log("loss: " + history.history.loss[19] + " on " + i + ". iteration.");
    }
}

train().then(() => {
    console.log("trained with " + tf.memory().numTensors + "tensors");
    model.predict(xs).print();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.13.0/tf.min.js"></script>