diff --git a/source/_posts/homebrew.md b/source/_posts/homebrew.md index 7b5b228..e54a7c1 100644 --- a/source/_posts/homebrew.md +++ b/source/_posts/homebrew.md @@ -39,7 +39,6 @@ Install Homebrew : | `brew upgrade git` | Upgrade package | | `brew unlink git` | Unlink | | `brew link git` | Link | -| `brew switch git 1.0.0` | Switch package version | | `brew list --versions git` | List the installed versions of package | ### Help diff --git a/source/_posts/npm.md b/source/_posts/npm.md new file mode 100644 index 0000000..cf511a0 --- /dev/null +++ b/source/_posts/npm.md @@ -0,0 +1,166 @@ +--- +title: npm +date: 2020-12-24 17:12:25 +background: bg-[#ebd94e] +tags: + - js + - web + - node + - npm +categories: + - Programming +intro: | + A NPM (node package manager) cheat sheet with the most important concepts, and commands to install, update, and manage your node packages. +plugins: + - copyCode + - runCode +--- + +## Getting Started with npm + +### Introduction + +npm (Node Package Manager) is the default package manager for Node.js. It allows you to install, update, and manage packages (libraries or modules) for your Node.js applications. + +### Installing and checking npm version + +Install Node.js from [nodejs.org](https://nodejs.org/), which includes npm. After installation, you can check the installed version of npm using the following command: + +```bash +npm --version +# or +npm -v +``` + +### Initializing a new project + +```bash +npm init # Interactive setup +# or +npm init -y # Creates a package.json with default values +``` + +## Installing packages + +### Install dependencies from package.json + +```bash +npm install +# or +npm i +``` + +### Install a specific package + +```bash +npm install # Install and add to dependencies +npm install --save-dev # Add to devDependencies +npm install -D # Shorthand for --save-dev +npm install -g # Install globally +npm install @ # Install specific version +npm install @latest # Install latest version +``` + +### Uninstall a package + +```bash +npm uninstall # Uninstall package +npm uninstall -g # Uninstall globally +npm rm # Shorthand for uninstall +``` + +## Viewing, searching and exploring packages + +### Viewing packages + +```bash +npm view # View package details +npm view versions # View all versions of a package +npm view version # View the latest version of a package +npm view description # View the description of a package +``` + +### Searching for packages + +```bash +npm search # Search for packages with similar terms +``` + +### Exploring packages + +```bash +npm docs # Open package documentation in the browser +npm home # Open package homepage in the browser +npm repo # Open package repository in the browser +``` + +## Managing packages + +### Updating packages + +```bash +npm outdated # Check for outdated packages +npm update # Update a specific package +npm update # Update all packages +``` + +### Listing installed packages + +```bash +npm list # List installed packages in the current project +npm list -g # List globally installed packages +npm list --depth=0 # List top-level installed packages only +``` + +### Running scripts + +```bash +npm run # Run a script defined in package.json +npm start # Run the 'start' script +npm test # Run the 'test' script +npm build # Run the 'build' script +``` + +### Cache management + +```bash +npm cache verify # Verify the integrity of the npm cache +npm cache clean # Clear the npm cache +npm cache clean --force # Force clear the npm cache +``` + +### Configuring npm + +```bash +npm config list # List all npm configuration settings +npm config set # Set a configuration value +npm config get # Get the value of a configuration key +npm config delete # Delete a configuration key +``` + +### Auditing and security + +```bash +npm audit # Audit dependencies for vulnerabilities +npm audit fix # Fix vulnerabilities in dependencies +npm audit fix --force # Force fix vulnerabilities, may update major versions +``` + +## Publishing packages + +```bash +npm login # Log in to your npm account (npmjs.com) +npm publish # Publish a package to the npm registry +npm version # Bump the version of your package (e.g., patch, minor, major) +npm unpublish # Unpublish a package (within 72 hours of publishing) +``` + +## Other useful commands + +```bash +npm help # Get general help on npm +npm help # Get help on a specific npm command +npm ci # Install dependencies from package-lock.json (clean install) +npm prune # Remove extraneous packages +npm dedupe # Reduce duplication in the node_modules folder +``` diff --git a/source/_posts/tensorflow.md b/source/_posts/tensorflow.md new file mode 100644 index 0000000..174a900 --- /dev/null +++ b/source/_posts/tensorflow.md @@ -0,0 +1,170 @@ +--- +title: Tensorflow +date: 2025-11-04 +background: bg-[#ffa800] +tags: + - AI + - Python +categories: + - Other +intro: This is a quick reference list of cheat sheets for Tensorflow. See also [Tensorflow website](https://tensorflow.org/). +--- + +## Imports { .cols-1 } + +### General + +``` +import tensorflow as tf # root package +import tensorflow_datasets as tfds # dataset representation and loading +model.compile(optimizer, loss, metrics) # compile necessary components for training and evaluation +model.fit(x_train, y_train, epoch, batch_size) # model training +model.evaluate(x_test, y_test) # model evaluation +``` + +## Tensors { .cols-1 } + +### Basic Operations +``` +a = tf.constant(5) + tf.constant(3) # tf.constant is an immutable tensor storing the fixed value +a.numpy() # This will return the value, which is 8 +b = tf.Variable(10) # tf.Variable is a shared state for an entire execution time +b.assign(15) # this assign the new value to the variable +with tf.GradientTape() as tape: # record operations on variables for automatic differentiation +``` + +### Creation + +``` +x = +tf.random_normal_initializer(mean, std) # tensor with independent N(mean,stf) entries +tf.random_uniform_initializer(min_val, max_val) # tensor with independent Uniform(min_val, max_val) entries +x = tf.[ones|zeros](*size) # tensor with all 1's [or 0's] +y = x.clone() # clone of x +with torch.no_grad(): # code wrap that stops autograd from tracking tensor history +requires_grad=True # arg, when set to True, tracks computation + # history for future derivative calculations +``` + +### Dimensionality + +``` +tf.shape # shape of the tensor +tf.rank # number of dimension of the tensors +tf.size # number of elements in the tensor? +x = tf.concat(tensor_seq, axis=0) # concatenates tensors along axis +y = tf.reshape(tensor, [new_shape]) # reshapes x into size (a,b,...) +y = tf.reshape(tensor, [(-1,a]) # reshapes x into size (b,a) for some b +y = x.permute(*dims) # permutes dimensions +y = tf.expand_dims(x) # tensor with added axis +y = tf.expand_dims(x, axis=2) # (a,b,c) tensor -> (a,b,1,c) tensor +``` + +### Algebra + +``` +tf.add(a, b), a + b # matrix addition +tf.multiply(a, b), a * b # matrix-vector multiplication +tf.matmul(a, b), a @ b # matrix multiplication +tf.transpose() # matrix transpose +``` + +### GPU Usage + +``` +gpus = tf.config.list_physical_devices('GPU') # check whether there is a GPU usage +if gpus: + +tf.device() # manual device placement + # either "/CPU:0", "/GPU:0", or other qualified name + # of the second GPU of your machine + +try: + tf.config.set_visible_devices(gpus[0], 'GPU') # Limiting GPU memory growth + + +``` + +## Deep Learning Models { .cols-1 } + +### Creating Models +``` +tf.keras.Sequential # stack layers in a way that the computation + # will be performed sequentially +``` + +### Layers + +``` +tf.keras.layers.Dense(m,n) # fully connected layer from + # m to n units + +tf.keras.layers.ConvXd(m,n,s) # X dimensional conv layer from + # m to n channels where X⍷{1,2,3} + # and the kernel size is s + +tf.keras.layers.MaxPoolXd(s) # X dimension pooling layer + # (notation as above) + +tf.keras.layers.BatchNormalization # batch norm layer +tf.keras.layers.RNN/LSTM/GRU # recurrent layers +tf.keras.layers.Dropout(rate=0.5) # dropout layer for any dimensional input +tf.keras.layers.Embedding(input_dim, output_dim) # (tensor-wise) mapping from + # indices to embedding vectors +``` + +### Loss Functions + +``` +tf.keras.losses.X # where X is BinaryCrossentropy, BinaryFocalCrossentropy, CTC + # CategoricalCrossentropy, CategoricalFocalCrossentropy, + # CategoricalHinge, CosineSimilarity, Dice, Hinge, Huber + # KLDivergence, LogCosh, MeanAbsoluteError, MeanAbsolutePercentageError + # MeanSquaredError, MeanSquaredLogarithmicError, Poisson + # Reduction, SparseCategoricalCrossentropy, SquaredHinge, Tversky +``` + +### Activation Functions + +``` +tf.keras.activations.X # where X is ReLU, ReLU6, ELU, SELU, PReLU, LeakyReLU, + # RReLu, CELU, GELU, Threshold, Hardshrink, HardTanh, + # Sigmoid, LogSigmoid, Softplus, SoftShrink, + # Softsign, Tanh, TanhShrink, Softmin, Softmax, + # Softmax2d, LogSoftmax or AdaptiveSoftmaxWithLoss +``` + +### Optimizers + +``` +opt = tf.keras.optimizer.x(model.parameters(), ...) # create optimizer +opt.step() # update weights +optim.X # where X is SGD, Adadelta, Adafactor, + # Adagrad, Adam, AdamW, Adamax, Ftrl, Lion, + # LossScaleOptimizer ,RMSprop or Rprop +``` + +### Learning rate scheduling - Callbacks + +``` +callbacks = tf.keras.callbacks.LearningRateScheduler(scheduler) # create lr scheduler +model.fit(..., callbacks=[callback], ....) # update lr after optimizer updates weights + # using with fit(), evaluate(), and predict() +``` + +### Saving and Loading Models +``` +tf.keras.models.clone_model(...) # Clone a Functional or Sequential Model instance. +tf.keras.models.load_model(...) # Loads a model saved via model.save(). +tf.keras.models.model_from_json(...) # Parses a JSON model configuration string and returns a model instance. +tf.keras.models.save_model(...) # Saves a model as a .keras file. +``` + +## Data Utilities { .cols-1 } + +### Datasets + +``` +pip install tensorflow-datasets # install the module +tfds.load('mnist', split, shuffle_files) # loading a dataset +```