From 3fa477125cd317fc52d33dc49e47372955ecd716 Mon Sep 17 00:00:00 2001 From: jacob cheriathundam Date: Sat, 2 Apr 2022 15:35:12 -0400 Subject: [PATCH] update --- tech/tensorflow/machine-learning.md | 28 +++++++ .../template/hello-world/index.html | 22 ++++++ .../tensorflow/template/hello-world/script.js | 20 +++++ .../tensorflow/template/hello-world/style.css | 29 +++++++ tech/tensorflow/tensor.md | 75 +++++++++++++++++++ tech/tensorflow/tensorflow.md | 18 ++++- tech/twistlock/twistlock.md | 14 ++++ tech/typescript/typescript.md | 12 +++ 8 files changed, 214 insertions(+), 4 deletions(-) create mode 100644 tech/tensorflow/template/hello-world/index.html create mode 100644 tech/tensorflow/template/hello-world/script.js create mode 100644 tech/tensorflow/template/hello-world/style.css create mode 100644 tech/tensorflow/tensor.md create mode 100644 tech/twistlock/twistlock.md create mode 100644 tech/typescript/typescript.md diff --git a/tech/tensorflow/machine-learning.md b/tech/tensorflow/machine-learning.md index 0b882c6..651a615 100644 --- a/tech/tensorflow/machine-learning.md +++ b/tech/tensorflow/machine-learning.md @@ -40,3 +40,31 @@ * TensorFlow Lite: Google's ML framework that focuses on mobile native and IoT devices. **Note:** TensorflowJS can also run on these platforms but TensorFlow Lite is optimized for them. * ONNX: The Open Neural Network Exchange is an open-source AI ecosystem of tech companies and research orgs. ONNX establishes open standards for representing ML algorithms in a way agnostic to the library it was written in. +## Selecting a Model +* Understand the problem by asking the client questions: + * What type of app does the customer need + * is it a web app? does it need to run on the client-side or the server-side? + * how important is privacy, speed, accuracy? +* Populate the list of models that can be used to solve the problem +* Use Inference speed (lower the better), file size of the model, RAM in MB to compare models + * calc inference speed: + ``` + async function calculateInferenceSpeed() { + // Record timestamp before model execution + const timeStart = performance.now(); + // Execute model + let results = await useSomeModel(); + // Record timestamp after model execution + const timeEnd = performance.now(); + // Calculate time taken for model execution + const timeTaken = timeEnd - timeStart; + console.log(`Time taken ${timeTaken} ms.`); + // Convert ms to FPS + console.log(`Frames per sec: ${1000 / timeTaken}`); + } + ``` + * file size: check model's documentation or pull up the model and look at file size on chrome dev tools + * models are json or bin files. add up all the json/bin files that re pulled down and you have your model size + * Use memory tab in chrome dev tools + * load the model, go to memory tab, click create snapshot => view total ram in the statistics drop down option + diff --git a/tech/tensorflow/template/hello-world/index.html b/tech/tensorflow/template/hello-world/index.html new file mode 100644 index 0000000..9b10d5d --- /dev/null +++ b/tech/tensorflow/template/hello-world/index.html @@ -0,0 +1,22 @@ + + + + Hello World - TensorFlow.js + + + + + + + +

TensorFlow.js Hello World

+ +

Awaiting TF.js load

+ + + + + + + + diff --git a/tech/tensorflow/template/hello-world/script.js b/tech/tensorflow/template/hello-world/script.js new file mode 100644 index 0000000..f792a07 --- /dev/null +++ b/tech/tensorflow/template/hello-world/script.js @@ -0,0 +1,20 @@ +/** + * @license + * Copyright 2018 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ + + const status = document.getElementById('status'); + status.innerText = 'Loaded TensorFlow.js - version: ' + tf.version.tfjs; + \ No newline at end of file diff --git a/tech/tensorflow/template/hello-world/style.css b/tech/tensorflow/template/hello-world/style.css new file mode 100644 index 0000000..bac4868 --- /dev/null +++ b/tech/tensorflow/template/hello-world/style.css @@ -0,0 +1,29 @@ +/** + * @license + * Copyright 2018 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ + +/* CSS files add styling rules to your content */ + +body { + font-family: helvetica, arial, sans-serif; + margin: 2em; + } + + h1 { + font-style: italic; + color: #FF6F00; + } + \ No newline at end of file diff --git a/tech/tensorflow/tensor.md b/tech/tensorflow/tensor.md new file mode 100644 index 0000000..9fe773b --- /dev/null +++ b/tech/tensorflow/tensor.md @@ -0,0 +1,75 @@ +# Tensor + +## What +* Primary data structure in tensorflow models +* Similar to arrays that can have multiple dimensions. **ONLY contain numerical data** +* ML models take tensors as inputs and output tensors +* Tensor is a class +* Need to explicitly dispose tensors when they are no longer needed + * tf.dispose() => to destroy a specific tensor + * tf.tidy() => dispose any tensor(s) created in the same function +* Terminology: + * DataType (DType) => either integer or floating point + * Shape => Number of elements present along each axis of the tensor (a cube would have a shape of [3,3,3] if it had 3 elements on each axis) + * Rank => # of "Axis" in the tensor. 6 Axis is max number tensorflow handles + * Size => total elements in the tensor +* Example: + * 0 Dimensions: + ``` + // js 0 dimension + let value = 6; + + // tf 0 dimension + let tensor = tf.scalar(6); + ``` + * 1 Dimensional: example as a x,y,x coordinates on a 3 dimensional plane + ``` + // js 1 dimension + let value = [1,2,3]; + + // tf 1 dimension. + let tensor = tf.tensor1d([1,2,3]); + ``` + * 2 Dimensional: example grayscale image + ``` + // js 2 dimension + let values = [ + [1,2,3], + [4,5,6], + [7,8,9] + ] + + // tf 2 dimensional + let tensor = tf.tensor2d([ + [1,2,3], + [4,5,6], + [7,8,9] + ]); + ``` + * 3 Dimensiona: exmaple rgb image where each pixel would need a rgb array value + * 4 Dimensional: example video where the rgb images above now have a time component associated with it + +## Code +``` +/** + * Line below creates a 2D tensor. + * Printing its values at this stage would give you: + * Rank: 2, shape: [2,3], values: [[1, 2, 3], [4, 5, 6]] + **/ +let tensor = tf.tensor2d([[1, 2, 3], [4, 5 ,6]]); + +// Create a Tensor holding a single scalar value: +let scalar = tf.scalar(2); + +// Multiply all values in tensor by the scalar value 2. +let newTensor = tensor.mul(scalar); + +// Values of newTensor would be: [[2, 4, 6], [8, 10 ,12]] +newTensor.print(); + +// You can even change the shape of the Tensor. +// This would convert the 2D tensor to a 1D version with +// 6 elements instead of a 2D 2 by 3 shape. +let reshaped = tensor.reshape([6]); +``` + diff --git a/tech/tensorflow/tensorflow.md b/tech/tensorflow/tensorflow.md index 1b053f8..80ac00a 100644 --- a/tech/tensorflow/tensorflow.md +++ b/tech/tensorflow/tensorflow.md @@ -1,8 +1,5 @@ # Tensorflow -## What? -JS and Python library to develop models, train them, and use them to run ML and AI on many platforms. It is written in C++ - ## Common Terms * Deep Learning results feed better Machine Learning Models. Better Machine Learning Models feed better AI. * AI - Science of making things smart @@ -27,7 +24,8 @@ JS and Python library to develop models, train them, and use them to run ML and * This works by feeding the Kerans Model the Layers API. The Core/OPS API then translates the Keras Model to a TensorFlow SavedModel. ## What is it -TensorflowJS - Javascript library that contains Google's machine learning library +* JS and Python library to develop models, train them, and use them to run ML and AI on many platforms. It is written in C++ +* TensorflowJS - Javascript library that contains Google's machine learning library * Python vs Javascript * Running Tensorflow in NodeJS allows you to leverage CUDA and AVX acceleration to perform a LOT faster than Python. * NodeJS supports integrating with Python Keras and Tensorflow SavedModels created in Python. @@ -44,6 +42,18 @@ TensorflowJS - Javascript library that contains Google's machine learning librar * Interactivity - can directly interact with the library using the browser platform * Reach & Scale - Running on a browser is basically omnipresent. +## Setup +**Note:** If using typescript, then set "skipLibCheck: true" in tsconfig.json file. +**Note:** NPM is shipped with ES Modules and ES2017 syntax +* JS + * npm install @tensorflow/tfjs +* Node: + * npm install @tensorflow/tfjs-node +* Linux with CUDA support (NVIDIA GPU) + * npm install @tensorflow/tfjs-node-gpu +* Script tag + * + ## Structurte Models --> Layers API --> Core/OPS API * Layers API - high level api diff --git a/tech/twistlock/twistlock.md b/tech/twistlock/twistlock.md new file mode 100644 index 0000000..01e2cf6 --- /dev/null +++ b/tech/twistlock/twistlock.md @@ -0,0 +1,14 @@ +# Twistlock + +## Background +When using containers, there are three primary challenges that need to be solved to ensure that your deployment is secure: +1) Access Control - Ensuring that each container follows least-access privelage +2) Inter-Container Communication - Ensuring that communication between containers is secure (authenticated) +3) App within Container - Ensuring that code within your container doesnt violate key security, that exceptions are handled properly, or contain malicious code + +Twistlock is a full suite of tools to address those challenges. + +On VA Notify, Twistlock is used to address challenge #3; container scanning. + +## How +Twistlock is integrated into our CI/CD pipeline. When a developer commits their code to the notify repository, Github triggers a pre-commit hook that launches Twistlock in a github VM environment. Twistlock then does a scan of the user's commit and the resultant container. If no issues are found and the container score is above a specified threshold, then the code is committed and Github kicks off any post commit actions. If there ARE issues found, then the commit is rejected and the dev is notified that an error was found. \ No newline at end of file diff --git a/tech/typescript/typescript.md b/tech/typescript/typescript.md new file mode 100644 index 0000000..0c6c93a --- /dev/null +++ b/tech/typescript/typescript.md @@ -0,0 +1,12 @@ +# Typescript + +## Common Commands: +* Setup a typescript project using [GTS](https://github.com/google/gts) + * creates a code formatter, linter, and code fixer for typescript projects. + * npx gts init + * command to compile: npm run compile + * command to lin +* Compile to JS + * npx tsc index.ts +* Activate watch command + * npx tsc -w \ No newline at end of file