Posts

    Showing posts with label javascript. Show all posts
    Showing posts with label javascript. Show all posts

    Tuesday, 23 January 2018

    Higher level ops for building neural network layers with deeplearn.js

    I have been meddling with google's deeplearn.js lately for fun. It is surprisingly good given how new the project is and it seems to have a sold roadmap. However it still lacks something like tf.layers and tf.contrib.layers which have many higher level functions that has made using tensorflow so easy. It looks like they will be added to Graphlayers in future but their priorities as of now is to fix the lower level APIs first - which totally makes sense.

    So, I quickly built one for tf.layers.conv2d and tf.layers.flatten which I will share in this post. I have made them as close to function definitions in tensorflow as possible.

    1.  conv2d - Functional interface for the 2D convolution layer.

    Arguments:
    • inputs Tensor input.
    • filters Integer, the dimensionality of the output space (i.e. the number of filters in the convolution).
    • kernel_size Number to specify the height and width of the 2D convolution window.
    • graph Graph opbject.
    • strides Number to specify the strides of convolution.
    • padding One of "valid" or "same" (case-insensitive).
    • data_format "channels_last" or "channel_first"
    • activation Optional. Activation function which is applied on the final layer of the function. Function should accept Tensor and graph as parameters
    • kernel_initializer An initializer object for the convolution kernel.
    • bias_initializer  An initializer object for bias.
    • name string which represents name of the layer.
    Returns:

    Tensor output.

    Usage:

    Add this to your code:

    2. flatten - Flattens an input tensor.


    I wrote these snippets while building a tool using deeplearnjs where I do things like loading datasets, batching, saving checkpoints along with visualization. I will share more on that in my future posts.

    Tuesday, 18 October 2016

    How to download large folders on dropbox

    Recently, someone shared a large folder with me and when I tried to download it, I was getting an error; "There was an error downloading your file".
    This error seemed very vague and after a quick search online, I figured out that it is not possible to download folders which are bigger than 1 GB.  And according to dropbox's help article, I will be able to download it only if I add it to my dropbox. With dropbox's puny 2GB free storage it was not possible and I was not ready to spend $$$ just for this. 
    So, I wrote a simple script in javascript that I can run it on browser console to download all files in a folder!

    How to do it?


    Step 1. Navigate to the dropbox folder on the browser and open your developer console. Press   cmd + j  on mac or  ctrl + shift + j  on linux and windows.

    Step 2. Paste the following code in the console.



    Step 3. The browser will try to block the windows trying to download it. Select the option to  Always allow pop-ups from https://www.dropbox.com . For instance this is how it will look on Google Chrome (you have to click on right most icon in the search bar).


    Step 4. Your files will be downloaded one by one!

    NOTE: If any folder inside the folder is greater than 1GB in size, then you may have to do the same process after navigating to that folder in the browser.

    Update: Make sure that you use the list view to see files by clicking on this:

    Thursday, 15 September 2016

    Right way to set env variable while exec or execFile in nodejs

    According to the official documentation, exec allows you to pass additional environment variables as part of options like this:

    This looks fine right? except that it totally isn't! By passing "env" as an option, you are not adding on to existing environment variables, but you are replacing it.  This is not clear from the documentation and can leave you scratching your head for a while as it can seem to break the command for no particular reason at all! So, you need to essentially make a copy of process.env and modify it like follows.


    Thanks for stopping by!

    Friday, 8 January 2016

    Parsing wav file in node.js

    I have worked with wav audio data in python before. Scipy provides a very nice way to do this using scipy.io.wavfile


    I wanted to do exactly the same in node.js. There is a module called wav which sort of does it. However I faced several problems.

    1. The Reader() method reads the file stream and converts into chunks of Buffer. This is very good while building web apps as you can send chunks of data separately and combine it later. However, I was just writing a script that'd run offline, So I had to put all the buffers to an array and then use the concat method of Buffer.


    2. The wav module doesn't do much processing and just throws the raw binary information at us. So, we have to take care of

    • Combining hex data to get amplitudes - One frame can be represented using several blocks of 8-bit hex. The number of blocks per frame is got using blockAlign parameter in the format.
    • Endianness - Data may or may not be  in little endian format. So, while reading the blocks of hex data, we have to take care of this.
    • Handling negative amplitudes - Frames spanning several blocks when negative can be little challenging to handle as they are just stored as their two's complement.
    • Separating channels - The raw binary contains data of all the channels together. Fortunately, they are mentioned one after the other. Using channels parameter in the format, channels can be separated easily.
    So, let's see how I handled all the above cases. First, we need to capture the "format" of the audio file that contains information about the file.


    Then, on "end" event i.e after all the chunks have been combined, we will handle all the cases mentioned above as follows


    Concluding, in this article I showed you how to handle and parse wav files in node.js by resolving several problems such as merging chunks of Buffer, combining hex data to get amplitudes, endianness, handling negative amplitudes and separating channels.

    Monday, 24 November 2014

    Type checking in javascript has never been easier

    I love languages which aren't strongly typed. It makes software development process very fast and very easy as we need not write huge, complex, unreadable code like in our typed languages. However this can lead to developer's worst nightmare: Runtime errors! In languages such as javascript it is not easy to check for type mismatches while in development process. Hence flow was invented!

    Flow is one of Facebook's open source projects. It allows developers to check for programming errors in javascript code with very less effort. This is a sample code available on their site:

    function foo(x) {
      return x * 10;
    }
    foo('Hello, world!');

    Note that a string is passed into the function foo and an arithmetic operation is performed on that string inside it. When flow is run on this code:

    01_HelloWorld/hello.js:7:5,17: string
    This type is incompatible with
      01_HelloWorld/hello.js:4:10,13: number

    It points out that the string is incompatible with number. Consider a much much complex code. An error like this can simply be unnoticed until a runtime error is raised.

    Source: https://github.com/facebook/flow
    Installation: http://flowtype.org/docs/getting-started.html
    Examples: http://flowtype.org/docs/five-simple-examples.html