-

- -

Showing posts with label neural-network. Show all posts
Showing posts with label neural-network. Show all posts

Tuesday, May 31, 2022

Transformer Illustration

 


What Is a Transformer Model?

A transformer model is a neural network that learns context and thus meaning by tracking relationships in sequential data like the words in this sentence.

https://blogs.nvidia.com/blog/2022/03/25/what-is-a-transformer-model/


Transformer Neural Network: Step-By-Step Breakdown of the Beast

https://towardsdatascience.com/transformer-neural-network-step-by-step-breakdown-of-the-beast-b3e096dc857f


The Illustrated Transformer

https://jalammar.github.io/illustrated-transformer/


Drawing the Transformer Network from Scratch

https://towardsdatascience.com/drawing-the-transformer-network-from-scratch-part-1-9269ed9a2c5e


Transformers are RNNs:

Fast Autoregressive Transformers with Linear Attention

https://linear-transformers.com/



Tuesday, May 25, 2021

TensorFlow.Js Tutorial: Making Predictions from 2D Data

This tutorial is based on the original article at: https://codelabs.developers.google.com/codelabs/tfjs-training-regression

html



javascript

See the Pen Untitled by smartcomputing123 (@smartcomputing123) on CodePen.

Friday, February 19, 2021

Artificial Intelligence In JavaScript

.

Artificial Intelligence

AI in JavaScript

These days, several JavaScript AI framework are emerging.

JavaScript frameworks make it possible to execute AI tasks in the browser.

AI and JavaScript

Artificial Intelligence has changed the science of image processing, computer vision, and natural language applications.

Thanks to new AI libraries, JavaScript developers can now build machine learning and deep learning applications without Python or R. This way JavaScript can help developers to bring AI to the browser and to the web.


Is JavaScript Good for AI?

Most AI applications these days use R or Python.

But JavaScript has a great future as an AI language, and it even has a some advantages:

  • JS is better known. All developers can use it.
  • Security is built in. JS cannot access your files.
  • JS is faster than Python.
  • Modern JS compiles into machine code.
  • Modern JS can use hardware acceleration.

WebGL API

WebGL is a JavaScript API for rendering 2d and 3D graphics in any browser.

WebGL can run on both integrated and stanalone graphic cards in any PC.

WebGL brings 3D graphics to the web browser. Major browser vendors Apple (Safari), Google (Chrome), Microsoft (Edge), and Mozilla (Firefox) are members of the WebGL Working Group.

WebGL 1.0 was released in March 2011.

WebGL 2.0 was released in January 2017.

JellyFish


TensorFlow Playground

TensorFlow Playground is a web application written in d3.js.

With TensorFlow Playground you can learn about Neural Networks (NN) without math.

In your own Web Browser you can create a Neural Network and see the result.


Plotting in JavaScript

01234567891516171819

Plotting provided by: Plotly

.

https://web.archive.org/web/20210220012909/https://www.w3schools.com/ai/ai_javascript.asp

.

Thursday, January 30, 2020

[ebook] Deep Learning with JavaScript Neural networks in TensorFlow.js



.

Deep learning has transformed the fields of computer vision, image processing, and natural language applications. Thanks to TensorFlow.js, now JavaScript developers can build deep learning apps without relying on Python or R. Deep Learning with JavaScript shows developers how they can bring DL technology to the web. Written by the main authors of the TensorFlow library, this new book provides fascinating use cases and in-depth instruction for deep learning apps in JavaScript in your browser or on Node.

about the technology

Running deep learning applications in the browser or on Node-based backends opens up exciting possibilities for smart web applications. With the TensorFlow.js library, you build and train deep learning models with JavaScript. Offering uncompromising production-quality scalability, modularity, and responsiveness, TensorFlow.js really shines for its portability. Its models run anywhere JavaScript runs, pushing ML farther up the application stack.

about the book

In Deep Learning with JavaScript, you’ll learn to use TensorFlow.js to build deep learning models that run directly in the browser. This fast-paced book, written by Google engineers, is practical, engaging, and easy to follow. Through diverse examples featuring text analysis, speech processing, image recognition, and self-learning game AI, you’ll master all the basics of deep learning and explore advanced concepts, like retraining existing models for transfer learning and image generation.

what's inside

  • Image and language processing in the browser
  • Tuning ML models with client-side data
  • Text and image creation with generative deep learning
  • Source code samples to test and modify

about the reader

For JavaScript programmers interested in deep learning.

about the author

Shanging CaiStanley Bileschi and Eric D. Nielsen are software engineers with experience on the Google Brain team, and were crucial to the development of the high-level API of TensorFlow.js. This book is based in part on the classic, Deep Learning with Python by François Chollet.

.

https://www.manning.com/books/deep-learning-with-javascript 

Saturday, January 4, 2020

DeepLearning With TensorFlowJS 4 - The intuitions behind Gradient-Descent Optimization

One-layer model is fitting a linear function f(input), defined as output = kernel * input + bias

The kernel and bias are tunable parameters (the weights) of the dense layer.

These weights contain the information learned by the network from exposure to the training data.

Initially, these weights are filled with small random values (a step called random initialization).

To find a good setting for the kernel and bias (collectively, the weights) we need two things:

  • A measure that tells us how well we are doing at a given setting of the weights. This is represented by a loss function measurement. 
  • A method to update the weights’ values so that next time we will do better than we currently are doing, according to the measure previously mentioned. This is accomplished by an optimizer method i.e. the algorithm by which the network will update its weights (kernel and bias, in this case) based on the data and the loss function.
  • The compile() method specifies 'sgd' as the optimizer and 'meanAbsoluteError' as the loss.

    'meanAbsoluteError' means that the loss function will calculate how far the predictions are from the targets, take their absolute values (making them all positive), and then return the average of those values:

    meanAbsoluteError = average( absolute(modelOutput - targets))

    'sgd' stands for stochastic gradient descent, a calculus formula to determine what adjustments should be made to the weights in order to reduce the loss.

    The fit() method is the training process of a model in TensorFlow.js. It can often be long-running, lasting for seconds or minutes. Therefore, the async/await feature is used.

    The evaluate() method calculates the loss function as applied to the provided example features and targets. It is similar to the fit() method in that it calculates the same loss, but evaluate() does not update the model’s weights.

    The training loop iterates through the following steps:

    1. Draw a batch of training samples x and corresponding targets y_true. A batch is simply a number of input examples put together as a tensor. The number of examples in a batch is called the batch size. In practical deep learning, it is often set to be a power of 2, such as 128 or 256. Examples are batched together to take advantage of the GPU’s parallel processing power and to make the calculated values of the gradients more stable.

    2. Run the network on x (a step called the forward pass) to obtain predictions y_pred.

    3. Compute the loss of the network on the batch, a measure of the mismatch between y_true and y_pred. Recall that the loss function is specified when model.compile() is called.

    4. Update all the weights (parameters) in the network in a way that slightly reduces the loss on this batch. The detailed updates to the individual weights are managed by the optimizer, which was specified during the model.compile() call.

    The loss as a function of all tunable parameters is known as the loss surface concept.

    The loss surface for this example has a bowl shape, with a global minimum at the bottom of the bowl representing the best parameter settings. 

    In general, however, the loss surface of a deep-learning model is much more complex. It will have many more than two dimensions and could have many local minima i.e. points that are lower than anything nearby but not the lowest overall.



    For larger problems i.e. when optimizing millions of weights, the likelihood of randomly selecting a good direction becomes vanishingly small. 

    A much better approach is to take advantage of the fact that all operations used in the network are differentiable and hence, to compute the gradient of the loss with regard to the network’s parameters. 

    The mathematical definition of a gradient specifies a direction along which the loss function increases. When training neural networks, the loss should gradually decrease. Therefore the weights should be moved in the direction opposite the gradient. This training process is aptly named gradient descent.

    One of the most desirable properties of deep neural networks are that they are universal approximators. Which means they should be able to cover non-convex functions as well. The problem with non-convex functions is that your initial guess might not be near the global minima and gradient descent might converge to a local minima. A solution to this problem is the stochastic gradient descent  approach.

    The term “stochastic” means drawing random samples from the training data during each gradient-descent step for efficiency, as opposed to using every training data sample at every step. In short, stochastic gradient descent is simply a modification of gradient descent for computational efficiency.

    Stochastic means nondeterministic or unpredictable. Random generally means unrecognizable, not adhering to a pattern. A random variable is also called a stochastic variable. (https://math.stackexchange.com/questions/114373/whats-the-difference-between-stochastic-and-random)


    .

    Friday, January 3, 2020

    DeepLearning With TensorFlowJS 3 - Fitting The Model


     

    This tutorial is based on the book Deep Learning With JavaScript (TensorFlowJS).



    https://codepen.io/tfjs-book/pen/VEVMMd

    Thursday, January 2, 2020

    DeepLearning With TensorFlowJS 2 - Plotting Tensor Data

    This tutorial is based on the book Deep Learning With JavaScript (TensorFlowJS).


    Tensors

    Tensors are the core data structure of TensorFlow.js 

    Tensors can also be thought of as containers for numbers.

    They are a generalization of vectors and matrices to potentially higher dimensions. 

    The number of dimensions and size of each dimension is called the tensor’s shape.
     
    Declaring a tensor

    // Pass an array of values to create a vector.
    tf.tensor([1, 2, 3, 4]).print();

    // Pass a nested array of values to make a matrix or a higher
    // dimensional tensor.
    tf.tensor([[1, 2], [3, 4]]).print();

    //Creates rank-1 tf.Tensor with the provided values, shape and dtype.
    tf.tensor1d([1, 2, 3]).print();

    //Creates rank-2 tf.Tensor with the provided values, shape and dtype.
    // Pass a nested array.
    tf.tensor2d([[1, 2], [3, 4]]).print();




    Plotly.js is a charting library that comes with over 40 chart types, 3D charts, statistical graphs, and SVG maps.





    https://codepen.io/tfjs-book/pen/dgQVze

    Tuesday, December 31, 2019

    DeepLearning With TensorFlowJS 1 - Train Data and Test Data

    This tutorial is based on the book Deep Learning With JavaScript (TensorFlowJS).

    The first script loads the TensorFlow package and defines the symbol tf, which provides a way to refer to names in TensorFlow.

    The second script creates two constants, trainData and testData, each representing 20 samples of how long it took to download a file (timeSec) and the size of that file (sizeMB). The elements in sizeMB and those in timeSec have one-to-one correspondence. For example, the first element of sizeMB in trainData is 0.080 MB, and downloading that file took 0.135 seconds—that is, the first element of timeSec—and so forth.

    The goal in this example will be to estimate timeSec, given just sizeMB.

    https://codepen.io/tfjs-book/pen/VEVMbx