question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is the trainAsync method available for LSTM?

See original GitHub issue

Me Sad

What is wrong?

trainAsync not available for LSTM? image

Where does it happen?

  • macOS v10.15.3
  • VScode v1.43.2
  • node v12.16.1
  • brainjs v2.0.0-alpha.12

How do we replicate the issue?

(1.)

const brain = require('brain.js')
const network = new brain.recurrent.LSTM()
const jsonfileReader = require('jsonfile')

 var mappedTrainingData = await jsonfileReader
    .readFile(trainingDataPath)
    .then(res => {
      console.log('File loaded!')
      console.log('Mapping training data...')
      return res.map(elm => ({
        input: elm.title,
        output: elm.type
      }))
    })
    .then(res => {
      console.log('Mapping completed!')
      return res
    })
    .catch(error =>
      console.error(`ERROR: Error reading training data file: ${error}`)
    )
console.log('Training network...')
network
    .trainAsync(mappedTrainingData)
    .then(res => {
      console.log('Training completed!')
      networkJSON = network.toJSON()
    })

How important is this (1-5)?

2

Expected behavior (i.e. solution)

It would be great if async training is available for the other modes besides NeuralNetwork as well.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
saschamandercommented, Feb 12, 2021

@jennasisis Thx again, worked like a charm. For anyone working on this here is my code:

parent.js

exports.retrainAi = (iterations = 100, callback = undefined) => {
	const { fork } = require('child_process')

	const forked = fork('./child.js', [
		iterations.toString(),
	])

	forked.on('message', (msg) => {
		console.log(msg)
	})

	forked.on('exit', function () {
		if (callback) callback()
	})
}

child.js

const fs = require('file-system')
const brain = require('brain.js')

// Get number of iterations from args
let iterations
try {
	iterations = parseInt(process.argv[2]) || 100
} catch (error) {}

try {
	// Init network
	const network = new brain.recurrent.LSTM({})
	// Load training data
	const trainingData = require('./trainingData.json')
	// Train network
	network.train(trainingData, {
		iterations: iterations,
		log: (stats) => process.send(stats),
		logPeriod: 1,
	})
	// Save trained network
	fs.writeFileSync('./trainedNetwork.json', JSON.stringify(network.toJSON()))
} catch (error) {
	// Catch errors
	process.send(error)
} finally {
	// End child process
	process.exit()
}

0reactions
robertleeplummerjrcommented, Apr 13, 2022

Thanks @saschamander for your example. I’ve tried several times to get this working in LSTM, without success because of the complexity of the net. This I think will be a standard feature of both FeedForward and Recurrent classes when they reach stability.

Read more comments on GitHub >

github_iconTop Results From Across the Web

10 Hyperparameters to keep an eye on for your LSTM model
A pro move is to employ the early stopping method to first specify a large number of training epochs and stop training once...
Read more >
brain.js - npm
For training with NeuralNetwork; For training with RNNTimeStep , LSTMTimeStep and GRUTimeStep; For training with RNN , LSTM and GRU.
Read more >
Time Series Prediction with LSTM Recurrent Neural Networks ...
With time series data, the sequence of values is important. A simple method that you can use is to split the ordered dataset...
Read more >
LSTM: How To Train Neural Networks to Write like Lovecraft
In order to train an LSTM Neural Network to generate text, we must first preprocess our text data so that it can be...
Read more >
Application of Long Short-Term Memory (LSTM) Neural ...
As a result, models do not gain good performance especially in areas where available data is limited. Moreover, process-based methods have limitations ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found