Я изменил оптимизатор, и он предсказывает, как и ожидалось.
[[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>