brain.js problem with importing LSTM network from json - brain.js

I've made a brain.js LSTM network, predicting addition. Then I saved it to a .json file using net.toJSON()
However, it doesn't seem to work. The traindata.json was successfully created, no errors. However, I get an error:
/home/runner/AngryBlaringTelevision/node_modules/brain.js/dist/brain.js:18102
var matrix = new Matrix(json.rows, json.columns);
^
TypeError: Cannot read property 'rows' of undefined
at Function.fromJSON (/home/runner/AngryBlaringTelevision/node_modules/brain.js/dist/brain.js:18102:36)
at LSTM.fromJSON (/home/runner/AngryBlaringTelevision/node_modules/brain.js/dist/brain.js:20142:26)
at /home/runner/AngryBlaringTelevision/index.js:9:7
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
My code:
const brain = require('brain.js');
const fs = require('fs');
const LSTM = brain.recurrent.LSTM;
const net = new LSTM();
fs.readFile('traindata.json', function(err, data) {
if (err) throw err;
net.fromJSON(data);
console.log("file loaded");
});
Also, here's the link to my traindata.json file on pastebin:
https://pastebin.com/tBZyxq1R
I don't have an idea how to solve this error. It seems like it's an error with brain.js.
I have also tried net.run() after importing the file, but it also didn't work.

I think what might be going on is that what you're passing into BrainJS is not actually JSON.
In your code, you have this:
fs.readFile('traindata.json', function(err, data) {
if (err) throw err;
net.fromJSON(data);
console.log("file loaded");
});
When I tried running your code, I saw the same error you saw.
Using JSON.parse() seemed to fix the issue.
In other words, when I changed net.fromJSON(data); to net.fromJSON(JSON.parse(data));, the error disappeared.

Related

How to wait for openRead function to complete on one file before moving on to the next?

I'm trying to load multiple files, in a certain order, inside of Dart. The files are of a modified GTFS (General Transit Feed Specification) type and are such interconnected through 'ids' between the files. Because of this I'm trying to load each file one by one but due to their large size I am using the openRead method of the File class to stream them in line-by-line using the LineSplitter transformer.
How they are being loaded in (start is the byte offset of the openRead method)
Stream<List<int>> getFileStream(String fileName, {int start}) => File(fileName).openRead(start);
// Example Stream
Stream<List<int>> stops = getFileStream("stops.txt", start: 117);
Stream<List<int>> routes = getFileStream("routes.txt", start: 131);
// How the stream data is being read
stops
.transform(utf8.decoder)
.transform(LineSplitter())
.listen(
(String line) {
List<String> values = line.split(',');
// Do stuff..
},
onDone: () { },
onError: (e) { print(e.toString()); }
);
routes
.transform(utf8.decoder)
.transform(LineSplitter())
.listen(
(String line) {
List<String> values = line.split(',');
// Do stuff..
// Error occurs here as well since the processing taking place here depends on the processing which takes place when the stops file is read
},
onDone: () { },
onError: (e) { print(e.toString()); }
);
However since I'm loading in multiple files within the same function and they are all streams with a listen callback set and they depend on the processing of the files that came before them to be COMPLETELY finished the program is producing errors since all the files are being read at once line by line and the processing of the other files has not finished.
Ideally, I would like to use an await for (String line in stops) line or something similar, however that produces the following error which I do not know how to solve:
The type 'Stream<List<int>>' used in the 'for' loop must implement Stream with a type argument that can be assigned to 'String'.
This error still shows up even if I do the .transform calls on the stream before the await for line.
I've also tried chaining together the onDone methods of the streams which produced an abomination of code which still didn't work (Lists that were created and added to within the function were empty upon returning???)
It would be nice to use the await for syntax as that produces cleaner code, however I do not want to pollute the whole function tree with async, await functions especially the main() function.
The only thing that worked was using the Completer class from dart:async however the onDone methods needed to be chained for this and the code was barely readable.
Any help as well as some guidance on Futures and async/await would be appreciated.
Have you tried something like this?
await for (var line in stops
.transform(utf8.decoder)
.transform(LineSplitter())) {
List<String> values = line.split(',');
// Do stuff..
// Error occurs here as well since the processing taking place here depends on the processing which takes place when the stops file is read
}

how to catch sqflite DatabaseException in Dart/Flutter

I am unsuccessful at catching an sqflite SQL error that throws a database exception. Note: it's a Future and async.
Sample Code
Future<String> getData() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, globals.databaseName);
Database db = await openDatabase(path);
List results = await db.rawQuery("mal formed example of sql on purpose");
}
When I run the code above, rawQuery throws the on-purpose bad SQL. The console shows:
E/SQLiteLog( 7908): (1) near "mal": syntax error
What I want to do is be able to catch SQL errors and handle them.
I've tried .then and .catchError - but not getting it to work.
And from what I've read, normal try {} catch {} doesn't work on Futures. (I did try that to start with... and it didn't work).
try {
List results = await db.rawQuery("mal formed example of sql on purpose");
} catch (e) {
print(e);
}
results in the same error:
E/SQLiteLog( 3148): (1) near "mal": syntax error
Sounds like the issue got fixed by upgrading to the latest version (don't know which version was used in the reported issue): https://github.com/tekartik/sqflite/issues/17
The SQLException (iOS and Android) is propagated to Dart and can be caught as a DatabaseException using try/catch when using async/await, as shown in the test here: https://github.com/tekartik/sqflite/blob/master/example/lib/exception_test_page.dart#L89-L94

Adobe Air null object reference only on iOS

I am building a fairly large Adobe AIR application that targets iOS
The following exception is appearing in my file loading class, but only on iOS (PC works fine):
[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
This occurs during the "onComplete" call in the following function. It happened suddenly and there were no recent code changes that would have affected that area of the application.
Files are loaded at the beginning of each "scene". Each scene uses the loader to load several files (asset lists, layouts, etc.). This exception only happens on the second file of the third scene to be loaded. The invocation of file loading is handled by a manager and is identical for all scenes. The data for the erroneous load is not corrupt and is successfully loaded before the exception on the onComplete call.
private function onFileLoaded( evt:Event ):void
{
trace( "onFileLoaded" );
var fs:FileStream = evt.currentTarget as FileStream;
var id:uint = m_fileStreamToId[ fs ];
var onComplete:Function = m_fileStreamToCallback[ fs ];
var retVal:Object = null;
var success:Boolean = false;
try
{
retVal = JSON.parse( fs.readUTFBytes( fs.bytesAvailable ) );
success = true;
}
catch ( error:Error )
{
trace( error );
retVal = null;
}
fs.close();
delete m_fileStreamToId[ fs ];
delete m_fileStreamToCallback[ fs ];
onComplete( id, retVal, success );
}
onFileLoaded is only called by:
private function internalAsyncLoadFileFromDisk( id:uint, filePath:File, onComplete:Function ):void
{
var fs:FileStream = new FileStream();
fs.addEventListener( Event.COMPLETE, onFileLoaded );
fs.addEventListener( IOErrorEvent.IO_ERROR, onIoError );
m_fileStreamToId[ fs ] = id;
m_fileStreamToCallback[ fs ] = onComplete;
fs.openAsync( filePath, FileMode.READ );
}
and the onComplete function argument is always a local private function.
When the debugger announces the null object reference and points to onComplete, it should be noted that onComplete is not null and the class encapsulating the functions has not been disposed of. Also, I do not see the "onFileLoaded" printed in the trace.
The m_fileStreamToCallback and m_fileStreamToId were created to remove the use of nested functions during file loading. I had experienced null object exceptions when attempting to access member variables as well as cross-scope local variables from within nested anonymous functions on iOS (even though it always works fine on PC).
Lastly, when I try to step into the file loading class with the debugger before the erroneous call, the debugger will always throw an internal exception and disconnect from the application. It only throws this exception before the erroneous call. The debugger is able to enter it successfully for all previous loads. It is also able to break inside the erroneous function when the null object error triggers. It simply cannot enter the class by stepping into it.
Environment details:
Adobe AIR: 14
Apache Flex SDK: 4.12.1
Editor: FlashDevelop
Build system: Ant
Build system OS: Windows 7 x64
Target Device: iPad 4
Target OS: iOS 7
Update 1
The following is the public interface and the onComplete function. So cleanupAndFinalCallback is the function that is supposedly null. I will also add that I am able to successfully enter this scene from another path through the application. If I enter via multiplayer, it crashes when loading the layout. When I enter from single player it does not. Both paths are loading the same file from the disk.
//! Async load the json file from the disk.
//! #param onComplete function( functorArgs:*, retVal:Object, success:Boolean )
public function asyncLoadFileFromDisk( filePath:File, onComplete:CallbackFunctor ):void
{
var newId:uint = m_idGenerator.obtainId();
m_idToCallback[ newId ] = onComplete;
internalAsyncLoadFileFromDisk( newId, filePath, cleanupAndFinalCallback );
}
private function cleanupAndFinalCallback( id:uint, retVal:Object, success:Boolean ):void
{
var onComplete:CallbackFunctor = m_idToCallback[ id ];
delete m_idToCallback[ id ];
m_idGenerator.releaseId( id );
onComplete.executeWithArgs( retVal, success );
}
Update 2
Stepping trough the app near the error causes debugger to crash. However, if I set breakpoints along the execution path I can jump (F5) through execution near the error. So, as I stated above, onComplete is not null as is reported by the error. I was able to execute it and pass further along in execution. At some point, the debugger throws the null reference error and snaps back to that point in the code. I feel there may be something funny going on with the stack.
I suspected that there may have been some issue with the stack. So, immediately after the file was loaded and the scene transition occurred, I used a delayed call of 1s to make sure that the load call was a real asynchronous call.
It turned out that the problem was with the debugger. I still received the "null object reference" error. However, this time, it reported it in a swc that was used in my app. It appears that this report is correct.
When I remove the delayed call, the program reverts to reporting the incorrect error.

Throw exception from closure

I've been trying to setup a simple Serversocket and I would like to have an exception thrown (other than some other stuff ie. setting a var to false) if some error is encountered, it works using an external callback but what about closures?
The Dart editor gives me an error and refuses to run it!
Server(String address,int port,int backlog)
{
this.s = new ServerSocket(address,port,backlog);
this.s.onError = (e) => throw new Exception(e);
}
I've tried also "throw e" and stuff like that, but as long as "throw" is present the ide won't run it.
I have had the same problem, Dart seams to be unable to accepts throws in single line closures. You should be able to do:
Server(String address,int port,int backlog)
{
this.s = new ServerSocket(address,port,backlog);
this.s.onError = (e) {
throw new Exception(e);
};
}
I have not looked in the spec so I don't know if its intentional or is a bug.

Syntax Error in ActionScript function

it seems there is an error in the code below, but where?
function cloneLoader(source:Loader):Loader
{
var clone:Loader = new Loader();
clone.loadBytes(source.contentLoaderInfo.bytes);
return clone;
};
clone.loadBytes is an asynchronous call. So you probably can't use the returned object right away.
Maybe try return Loader(ObjectUtil.clone(source));
There is no error in the code as such, nothing wrong with the syntax, it will work fine if source is a Loader.
What error message do you get?

Resources