Home
About
Contact
Home
Features
_Multi DropDown
__DropDown 1
__DropDown 2
__DropDown 3
_ShortCodes
_SiteMap
_Error Page
Mega Menu
Documentation
Video Documentation
Download This Template
Home
Gautam AI
July 23, 2024
Neural Network with TensorFlow.js
Simple Neural Network with TensorFlow.js
Run Code
Debug Code
async function trainModel() { // Create a sequential model const model = tf.sequential(); // Add a dense layer with 20 input units and ReLU activation model.add(tf.layers.dense({ inputShape: [20], units: 10, activation: 'relu' })); // Add another dense layer with 3 units and softmax activation model.add(tf.layers.dense({ units: 3, activation: 'softmax' })); // Compile the model with a categorical crossentropy loss function and SGD optimizer model.compile({ optimizer: tf.train.sgd(0.1), loss: 'categoricalCrossentropy' }); // Generate sample data: 3 input samples with 20 features each const xs = tf.tensor2d([ Array.from({ length: 20 }, () => Math.random()), Array.from({ length: 20 }, () => Math.random()), Array.from({ length: 20 }, () => Math.random()) ]); // Generate sample target data: one-hot encoded labels const ys = tf.tensor2d([[1, 0, 0], [0, 1, 0], [0, 0, 1]]); // Train the model await model.fit(xs, ys, { epochs: 50, callbacks: { onEpochEnd: (epoch, logs) => { console.log(`Epoch ${epoch + 1}: loss = ${logs.loss}`); } } }); // Make predictions const prediction = model.predict(xs); prediction.print(); // Display predictions const outputDiv = document.getElementById('output'); const predictionsArray = await prediction.array(); predictionsArray.forEach((pred, index) => { const predDiv = document.createElement('div'); predDiv.classList.add('prediction', index % 2 === 0 ? 'red' : 'blue'); predDiv.textContent = `Sample ${index + 1} Prediction: ${pred}`; outputDiv.appendChild(predDiv); }); }
Post a Comment
0 Comments
Blogger Dashboard
Run
Debug
Build
Sync Project
Run Tests
Profile
Logcat
Update SDK
Generate APK
Firebase
Run Lint
Settings
Project Info
Editor
Output
Console Output will appear here...
!doctype>
Social Plugin
Popular Posts
Technology
3/Technology/post-list
0 Comments