A Short Introduction to TensorFlow.JS

So this is a quick introduction for machine learning with JavaScript.

Above is a series of numbers. You can work out pretty easily that Y = 2X – 1. How you worked this out in your head is exactly how machine learning works.

This is all the code you need to predict the pattern with machine learning and TensorFlow JS. Read on to learn what is happening here.

First up you will need to create a model. A model is a trained neural network. This is a basic model with a single layer. Our layer here is the method `tf.layers.dense`

This layer has a single neuron which is represented by the property `units`. We are also feeding in a single value into the neural network represented by the property `inputShape`, which is our X value. Our neural network will try to predict the Y value.

We need to compile our model. There are two main functions here. Loss and Optimizer. This is how our model learns. It will predict a number and the loss function will calculate how close it was. The Optimizer will predict the next guess and try to get closer to the right formula.

This will happen many times increasing our accuracy. In our example it will happen 1000 times, represented by the `epochs` property. Each time it will enhance its guess, getting us closer to the correct formula.

We have our two data sets represented as two arrays, `xs` and `xy`. We need to convert our data to tensors first using `tf.tensor`. This is just a holder for our data with type information.

We match our two datasets to each other using the `model.fit` and passing in our two tensors (data). This method will take a while to complete, so we use `await` here.

Once the model is trained. We can now predict with our model using `model.predict`.

So we are predicting the Y value for 10. We already know it will be 19 (Y = 2X – 1). When we run the following code our model gives us 18.999. Success!

You are probably wondering about the .999, this is because we are only giving it a small dataset and it is accounting for that in its prediction.

Hoped you like this article.

Below is the complete code.

Get the currently playing song on Triple J via the Command Line

I made a tiny npm library that gets the currently playing song on the radio station Triple J. This tool works on the  command line. If you have npx installed, you can use it like so.

npx triplej

If you want to open the song in Spotify, you can use this command:

npx triplej --spotify

See https://www.npmjs.com/package/triplej for details.

D3EDsSxVAAAvqRIA small feature that I’ve added to the library is a rarity system for the line separator.

I’ve also added a small feature that adds a rarity value to the song line separator. So everytime you use the tool, you may get a rare line.

Slack Beer Bot

I created a simple slack beer bot. It allows you to search for beer. In slack type /beer then any beer you want to look up. Here are a few examples:

/beer corona
/beer newtowner
/beer Hop Sauce
/beer Wayward Brewing
/beer westvleteren 12
/beer anything really

Visit https://slackbeer.herokuapp.com/ for installation.

Add to Slack

screen_shot

A part of the purpose of this build was to road test multiple server solutions. These were: AWS Lambda, Heroku, @zeit/now. A full write up and blog post will be coming soon.

What would a Immediately Invoked Class Expression look like in JavaScript?

What would a Immediately Invoked Class Expression in JavaScript look like. This is the question I asked myself. I couldn’t find anything online, so I created one.

Below we have a Immediately Invoked Class Expression. This could also be called a Self-Executing Anonymous Class.

We have all heard of the Immediately Invoked Function Expression (IIFE) it is also known as a Self-Executing Anonymous Function. I was curious how this would look with Classes. There are two parts of a IIFE. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope. The second part creates the immediately executing function expression () through which the JavaScript engine will directly interpret the function.

To replicate this, our Immediately Invoked Class Expression would need at least these two features.

Above is what a Immediately Invoked Class Expression looks like. There are a few parts to this design pattern. Let me take you through them.

The void operator evaluates the given expression and then returns undefined. This will prevent our class polluting the global scope by always returning undefined.

The new operator interprets the defined anonymous class immediately. This then calls the classes constructor function as per the ECMAscript spec. So there you have it.

NPM Module classList-helper version 1.1.

classList update

I’ve just updated the npm module https://www.npmjs.com/package/classlist-helper  to 1.1. It now has full unit testing.

Introduction

A Element.classList method helper function that can be curried for functional programing.

Example

Task: Add the class active to all list elements.

<ul>
  <li class="item">item</li>
  <li class="item">item</li>
  <li class="item">item</li>
  <li class="item">item</li>
</ul>

Setup:

// import various libs
import { map, curry } from 'lodash';
let classListHelper = require('classlist-helper');

// Get the list of elements that you want to change
let nodeList = document.querySelectorAll('.item'); // 4 html elements

Usage:

// Create Active method with classListHelper.
let setActive = curry(classlistHelper)('active')('add');

// Set all elements to Active.
map(nodeList, setActive);

After running this, the html will look like:

<ul>
  <li class="item active">item</li>
  <li class="item active">item</li>
  <li class="item active">item</li>
  <li class="item active">item</li>
</ul>

Other example methods

// Remove a Class
let setInActive = curry(classlistHelper)('active')('remove');

// Add a Class
let setActive = curry(classlistHelper)('active')('add');

// Toggle a class 
let toggleActive = curry(classlistHelper)('active')('toggle');

// Add multiple classes / Pass an Array
let setMultiple = curry(classlistHelper)(['active','updated'])('add');

// Test all elements in an Array for a class
let anyActive = curry(classlistHelper)('active')('contains');

// Replace a class with another.
let setInactive = curry(classlistHelper)(['active', 'inactive'])('replace');

Note: For more examples please check the unit tests.

npm browser-classes

I’ve just released version 1.0.1 of a new npm module browser-classes.

https://npmjs.com/package/browser-classes

browser-classes

Adds browser classes to document body. For better cross-browser SCSS styling.

This library adds the current browser and version to the <body> tag as a class to the site.

Once you run the class, your <body> tag will have the following class added.

<body class="browser-firefox-65">
<body class="browser-ie-10">

Installation

npm i --save browser-classes

Usage

import BrowserClasses from 'browser-classes';
new BrowserClasses();

SCSS Usage

Use the @at-root method when using with SCSS

.myClass {
    color: red;
    @at-root .browser-ie-10 {
        color: blue
    }
}

This will target Internet Explorer 10 with browser-ie-10 added to the body tag.

<body class="browser-ie-10">

Javascript usage

The below method can test for Internet Explorer 9

if (document.body.classList.contains('browser-ie-9')) {
    // fix
}

Browser only targeting

To target only a certain browser not by version use the following syntax.

CSS
body[class^="browser-firefox"] {
    color: red;
}

Browser Support

Currently, this library identifies the following browsers:

  • Chrome
  • Internet Explorer
  • Edge
  • Safari
  • Firefox

Example class names for browsers

Below are some examples of the classes that will be generated for the browsers.

Browser Class
Internet Explorer 10 <body class="browser-ie-10">
Edge 12 <body class="browser-edge-12">
Google Chrome 50 <body class="browser-chrome-50">
Firefox 60 <body class="browser-firefox-60">
Safari 11 <body class="browser-safari-1">

GIT Cheat Sheet

As a front end developer tools like source tree and Webstorm’s GIT GUI are life savers. But sometimes you need to open up the terminal to work with your GIT repo. Over the years I’ve saved a few commands that I use frequently. There are a few commands that will save your bacon on the odd occasion. Below is the GIST that I use and update.

Stories – Alone in the Fallout

Stories have announced that their debut album ‘The Youth To Become‘ will be released on Friday August 14th via UNFD. The album is available to pre-order now from 24Hundred.

The five piece draw influence from post-hardcore, metal, alternative and progressive rock, but never dwell too long on a particular style.

With two brothers guiding the creative process – Nic writes the music and Morgan the lyrics –

“Because we’re brothers, we tend to be on the same sort of track with a lot of our opinions and feelings. This has been the case since I can remember. Since we started writing The Youth To Become, our bond has certainly strengthened more and more, and I definitely feel like we make a good team when working anything creative together.“

The first single from the album is ‘Under Haze‘. You can check out a video for the song below.

“Being the last song we wrote on the album, it took little pieces from each stretch of the record and came together with a pretty dark vibe. We knew right from when we wrote it that it would be a cool song for people to hear first. Lyrically, I feel like I created a strong sense of imagery with this track, and it parallels with the high energy of the sounds.“

Words: http://www.bombshellzine.com/2015/08/stories-announce-new-album-the-youth-to-become/