JMullings commited on
Commit
237fb51
1 Parent(s): 0432920

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +13 -37
README.md CHANGED
@@ -38,43 +38,19 @@ Below is the code implementation for training the XOR neural network using Tenso
38
 
39
  ```javascript
40
  // Import TensorFlow.js library
41
- import * as tf from '@tensorflow/tfjs';
42
-
43
- // XOR dataset
44
- const xorData = {
45
- inputs: tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]),
46
- labels: tf.tensor2d([[0], [1], [1], [0]]),
47
- };
48
-
49
- // Define the neural network architecture
50
- const model = tf.sequential();
51
- model.add(tf.layers.dense({ units: 2, inputShape: [2], activation: 'relu' }));
52
- model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));
53
-
54
- // Compile the model
55
- model.compile({ loss: 'meanSquaredError', optimizer: tf.train.sgd(0.1) });
56
-
57
- // Train the model
58
- async function trainModel() {
59
- const history = await model.fit(xorData.inputs, xorData.labels, {
60
- epochs: 10000,
61
- verbose: 0,
62
- });
63
-
64
- // Print the final loss after training
65
- const finalLoss = history.history.loss[history.history.loss.length - 1];
66
- console.log(`Final Loss: ${finalLoss}`);
67
-
68
- // Make predictions
69
- const testData = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);
70
- const predictions = model.predict(testData);
71
-
72
- // Display the resulting predictions
73
- predictions.print();
74
- }
75
-
76
- // Execute the training
77
- trainModel();
78
  ```
79
 
80
  ## Resulting Prediction
 
38
 
39
  ```javascript
40
  // Import TensorFlow.js library
41
+ import * as tf from '@tensorflow/tfjs-node-gpu';
42
+
43
+ this.model = await tf.loadLayersModel(`file://${this.model_path}/model.json`);
44
+ this.model.compile({
45
+ optimizer: tf.train.sgd(0.1),
46
+ loss: 'binaryCrossentropy', // Binary classification loss
47
+ metrics: ['accuracy'],
48
+ });
49
+
50
+ this.model.summary();
51
+
52
+ const x = tf.tensor2d([[1,1]]);
53
+ const prediction = this.model.predict(x) as tf.Tensor;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  ```
55
 
56
  ## Resulting Prediction