### Install Object Detection API
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/tf2_object_detection.ipynb
Install the Object Detection API by compiling protocol buffers, copying the setup file, and installing the package. This requires protobuf-compiler to be installed system-wide.
```bash
sudo apt install -y protobuf-compiler
cd models/research/
protoc object_detection/protos/*.proto --python_out=.
cp object_detection/packages/tf2/setup.py .
python -m pip install .
```
--------------------------------
### Install TensorFlow Examples Package
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/generative/cyclegan.ipynb
Installs the tensorflow_examples package required for importing generator and discriminator models.
```python
!pip install git+https://github.com/tensorflow/examples.git
```
--------------------------------
### Load Example Videos and Define Queries
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/text_to_video_retrieval_with_s3d_milnce.ipynb
Loads example videos from URLs and defines corresponding text queries. This setup is for demonstrating text-to-video retrieval.
```python
# @title Load example videos and define text queries { display-mode: "form" }
video_1_url = 'https://upload.wikimedia.org/wikipedia/commons/b/b0/YosriAirTerjun.gif' # @param {type:"string"}
video_2_url = 'https://upload.wikimedia.org/wikipedia/commons/e/e6/Guitar_solo_gif.gif' # @param {type:"string"}
video_3_url = 'https://upload.wikimedia.org/wikipedia/commons/3/30/2009-08-16-autodrift-by-RalfR-gif-by-wau.gif' # @param {type:"string"}
video_1 = load_video(video_1_url)
video_2 = load_video(video_2_url)
video_3 = load_video(video_3_url)
all_videos = [video_1, video_2, video_3]
query_1_video = 'waterfall' # @param {type:"string"}
query_2_video = 'playing guitar' # @param {type:"string"}
query_3_video = 'car drifting' # @param {type:"string"}
all_queries_video = [query_1_video, query_2_video, query_3_video]
all_videos_urls = [video_1_url, video_2_url, video_3_url]
display_video(all_videos_urls)
```
--------------------------------
### Install and Setup Dependencies
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/tf_hub_film_example.ipynb
Installs the mediapy library and ffmpeg for video processing. Ensure these are available before running the interpolation model.
```python
!pip install mediapy
!sudo apt-get install -y ffmpeg
```
--------------------------------
### Get a batch of training examples
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/text_classification_with_hub.ipynb
Fetches the first batch of 10 examples and their corresponding labels from the training dataset. This is useful for inspecting the data format.
```python
train_examples_batch, train_labels_batch = next(iter(train_data.batch(10)))
```
--------------------------------
### Install Seaborn
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/regression.ipynb
Installs the Seaborn library, which is used for data visualization, specifically for creating pairplots in this example. This is a quiet installation.
```python
# Use seaborn for pairplot.
!pip install -q seaborn
```
--------------------------------
### Install Libraries
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb
Installs TensorFlow Text, Bokeh, SimpleNeighbors with Annoy, and TQDM. Ensure these libraries are installed before proceeding.
```bash
!pip install "tensorflow-text==2.11.*"
!pip install bokeh
!pip install simpleneighbors[annoy]
!pip install tqdm
```
--------------------------------
### Install Dependencies
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/bird_vocalization_classifier.ipynb
Installs necessary libraries for the tutorial. Use these commands to set up your environment.
```python
!pip install -q "tensorflow_io==0.28.*"
!pip install -q librosa
```
--------------------------------
### Display training examples batch
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/text_classification_with_hub.ipynb
Prints the first batch of 10 training examples. Each example is a movie review string.
```python
train_examples_batch
```
--------------------------------
### Install Seaborn
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/core/mlp_core.ipynb
Installs the seaborn library, which is used for data visualization, specifically for creating countplots in this example. Use the '-q' flag for quiet installation.
```python
# Use seaborn for countplot.
!pip install -q seaborn
```
--------------------------------
### Install TensorFlow and TensorFlow Datasets
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/audio/simple_audio.ipynb
Installs the necessary libraries for the tutorial. Run this command before importing TensorFlow.
```bash
!pip install -U -q tensorflow tensorflow_datasets
```
--------------------------------
### Display Labeled Examples
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/cord_19_embeddings.ipynb
Fetches and displays a specified number of labeled examples from the training set of the SciCite dataset. Requires pandas and TensorFlow to be installed.
```python
#@title Let's take a look at a few labeled examples from the training set
NUM_EXAMPLES = 20 #@param {type:"integer"}
data = get_example_data(THE_DATASET, NUM_EXAMPLES, for_eval=False)
display_df(
pd.DataFrame({
TEXT_FEATURE_NAME: [ex.decode('utf8') for ex in data[0]],
LABEL_NAME: [THE_DATASET.class_names()[x] for x in data[1]]
}))
```
--------------------------------
### Install Dependencies
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/spice.ipynb
Installs necessary system packages for audio processing.
```bash
#@title Copyright 2020 The TensorFlow Hub Authors. 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.
# ==============================================================================
```
--------------------------------
### Install PrettyMIDI
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/audio/music_generation.ipynb
Installs the pretty_midi library, which is used for parsing and creating MIDI files in this tutorial.
```bash
#!/bin/bash
!pip install pretty_midi
```
--------------------------------
### Install necessary libraries
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/video/video_classification.ipynb
Installs required Python packages for video processing, deep learning, and utility functions. Ensure these are installed before running the tutorial.
```python
#@title 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
#
# https://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.
```
```python
!pip install remotezip tqdm opencv-python einops
!pip install -U tensorflow keras
```
--------------------------------
### Start TensorFlow Build Container with Host Source Mount
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/source.md
Starts a Docker container, mounting the host's TensorFlow source directory to /tensorflow within the container. This is an alternative setup for building TensorFlow.
```bash
docker run -it -w /tensorflow -v /path/to/tensorflow:/tensorflow -v $PWD:/mnt \
-e HOST_PERMS="$(id -u):$(id -g)" tensorflow/tensorflow:devel bash
```
--------------------------------
### Install Gym and Pyglet
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/reinforcement_learning/actor_critic.ipynb
Install the necessary packages for the CartPole environment and visualization.
```bash
#!/bin/bash
# Install additional packages for visualization
sudo apt-get install -y python-opengl > /dev/null 2>&1
pip install git+https://github.com/tensorflow/docs > /dev/null 2>&1
```
--------------------------------
### Install Dependencies
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/text_classification_with_hub.ipynb
Installs necessary libraries for the tutorial, including TensorFlow Hub, TensorFlow Datasets, and TF-Keras.
```python
#@title 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
#
# https://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.
```
```python
#@title MIT License
#
# Copyright (c) 2017 François Chollet
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
```
```python
!pip install tensorflow-hub
!pip install tensorflow-datasets
!pip install tf-keras
```
--------------------------------
### Install protobuf compiler on Linux
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/build_from_source.md
Installs the protobuf compiler using apt on Debian/Ubuntu-based systems. This is required for the developer install method.
```shell
(tensorflow_hub_env)~/hub/$ sudo apt install protobuf-compiler
```
--------------------------------
### Install Portpicker
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/distribute/parameter_server_training.ipynb
Installs the portpicker library, which is used for selecting unused network ports.
```python
!pip install portpicker
```
--------------------------------
### Download Example Images
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/load_data/tfrecord.ipynb
Downloads two example image files using `tf.keras.utils.get_file`. These images will be used for the TFRecord writing example.
```python
cat_in_snow = tf.keras.utils.get_file(
'320px-Felis_catus-cat_on_snow.jpg',
'https://storage.googleapis.com/download.tensorflow.org/example_images/320px-Felis_catus-cat_on_snow.jpg')
williamsburg_bridge = tf.keras.utils.get_file(
'194px-New_East_River_Bridge_from_Brooklyn_det.4a09796u.jpg',
'https://storage.googleapis.com/download.tensorflow.org/example_images/194px-New_East_River_Bridge_from_Brooklyn_det.4a09796u.jpg')
```
--------------------------------
### Install protobuf compiler on Mac
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/build_from_source.md
Installs the protobuf compiler using Homebrew on macOS. This is required for the developer install method.
```shell
(tensorflow_hub_env)~/hub/$ brew install protobuf
```
--------------------------------
### Install Python Packages
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/spice.ipynb
Installs required Python libraries for audio manipulation and analysis.
```bash
!sudo apt-get install -q -y timidity libsndfile1
```
--------------------------------
### Install Fluidsynth
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/audio/music_generation.ipynb
Installs the Fluidsynth audio synthesis software. This is a prerequisite for audio playback in Colab.
```bash
#!/bin/bash
sudo apt install -y fluidsynth
```
--------------------------------
### Install and Import TensorFlow
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/ragged_tensor.ipynb
Installs the latest pre-release version of TensorFlow and imports the library.
```python
!pip install --pre -U tensorflow
import math
import tensorflow as tf
```
--------------------------------
### Install Kaggle Package
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/text_classification_with_tf_hub_on_kaggle.ipynb
Installs the Kaggle API client. This is a prerequisite for interacting with Kaggle datasets and competitions.
```python
# Copyright 2018 The TensorFlow Hub Authors. 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.
# ==============================================================================
```
--------------------------------
### Display First Three Examples from Dictionary Dataset
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/load_data/pandas_dataframe.ipynb
Prints the first three examples from a TensorFlow dataset created from a dictionary of features. Each example will be a dictionary of tensors. Assumes `numeric_dict_ds` is defined.
```python
for row in numeric_dict_ds.take(3):
print(row)
```
--------------------------------
### Install tf_slim
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/migrate/validate_correctness.ipynb
Installs the tf_slim library, which is used for model building and evaluation.
```python
!pip install -q tf_slim
```
--------------------------------
### Install TensorFlow APU Example Plug-in
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/gpu_plugins.md
Install the example plug-in package for the Awesome Processing Unit (APU) using pip. This command is used to add support for a new demonstration device.
```sh
# Install the APU example plug-in package
$ pip install tensorflow-apu-0.0.1-cp36-cp36m-linux_x86_64.whl
...
Successfully installed tensorflow-apu-0.0.1
```
--------------------------------
### Setup Environment
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/retrieval_with_tf_hub_universal_encoder_qa.ipynb
This code block is used to set up the environment for the tutorial. It captures and suppresses output.
```python
%%capture
#@title Setup Environment
```
--------------------------------
### Install scikit-learn
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/estimator/linear.ipynb
Installs the scikit-learn library, which may be used for data preprocessing or evaluation.
```python
!pip install sklearn
```
--------------------------------
### IOError: No such file or directory for setup.py
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/errors.md
This error indicates that the setup.py file could not be found during the installation process. It might be due to an incomplete download or issues with the temporary build directory.
```text
IOError: [Errno 2] No such file or directory:
'/tmp/pip-o6Tpui-build/setup.py'
```
--------------------------------
### Check Clang Version
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/source.md
Verifies the installed Clang version. This command should be run after installing Clang and LLVM to confirm the setup.
```bash
clang --version
```
--------------------------------
### Install Dependencies
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/tf2_object_detection.ipynb
Installs specific versions of numpy and protobuf required for the Colab environment. Ensure these versions are compatible with your setup.
```python
# This Colab requires a recent numpy version.
!pip install numpy==1.24.3
!pip install protobuf==3.20.3
```
--------------------------------
### Generate and Play Example MIDI
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/audio/music_generation.ipynb
Creates an example MIDI file using the `notes_to_midi` function and then plays it back. Assumes `raw_notes` and `instrument_name` are defined.
```python
example_file = 'example.midi'
example_pm = notes_to_midi(
raw_notes, out_file=example_file, instrument_name=instrument_name)
```
```python
display_audio(example_pm)
```
--------------------------------
### Install necessary libraries
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/action_recognition_with_tf_hub.ipynb
Installs required packages for the tutorial, including imageio, opencv-python, and a specific version of tensorflow_docs.
```python
!pip install -q imageio
!pip install -q opencv-python
!pip install -q git+https://github.com/tensorflow/docs
```
--------------------------------
### Load and Prepare Example Images
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/interpretability/integrated_gradients.ipynb
Define URLs for example images, download them using tf.keras.utils.get_file, and preprocess them using the read_image function.
```python
img_url = {
'Fireboat': 'http://storage.googleapis.com/download.tensorflow.org/example_images/San_Francisco_fireboat_showing_off.jpg',
'Giant Panda': 'http://storage.googleapis.com/download.tensorflow.org/example_images/Giant_Panda_2.jpeg',
}
img_paths = {name: tf.keras.utils.get_file(name, url) for (name, url) in img_url.items()}
img_name_tensors = {name: read_image(img_path) for (name, img_path) in img_paths.items()}
```
--------------------------------
### Install tensorflow_docs Package
Source: https://github.com/tensorflow/docs/blob/master/site/en/community/contribute/docs.md
Install the tensorflow_docs package from GitHub to generate Python API reference documentation.
```bash
pip install git+https://github.com/tensorflow/docs
```
--------------------------------
### Install Datasets and Load WER Metric
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/wav2vec2_saved_model_finetuning.ipynb
Installs the HuggingFace datasets library and loads the Word Error Rate (WER) metric. This is the initial setup for evaluation.
```python
!pip3 install -q datasets
from datasets import load_metric
metric = load_metric("wer")
```
--------------------------------
### Download example audio file (miaow_16k.wav)
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/yamnet.ipynb
Downloads a sample audio file named 'miaow_16k.wav' from a Google Cloud Storage bucket. This file is already at the expected 16kHz sample rate.
```bash
!curl -O https://storage.googleapis.com/audioset/miaow_16k.wav
```
--------------------------------
### Install CUDA and cuDNN with Conda
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/pip.md
Install specific versions of CUDA and cuDNN required for GPU support using conda. This is part of the GPU setup process.
```bash
conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
```
--------------------------------
### Demonstrate split_window with example data
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/structured_data/time_series.ipynb
Creates an example window from training data and uses the split_window method to separate it into inputs and labels. Prints the shapes of the original window, inputs, and labels to illustrate the transformation.
```python
# Stack three slices, the length of the total window.
example_window = tf.stack([np.array(train_df[:w2.total_window_size]),
np.array(train_df[100:100+w2.total_window_size]),
np.array(train_df[200:200+w2.total_window_size])])
example_inputs, example_labels = w2.split_window(example_window)
print('All shapes are: (batch, time, features)')
print(f'Window shape: {example_window.shape}')
print(f'Inputs shape: {example_inputs.shape}')
print(f'Labels shape: {example_labels.shape}')
```
--------------------------------
### Install clang-format for C++ code formatting
Source: https://github.com/tensorflow/docs/blob/master/site/en/community/contribute/code_style.md
Install the clang-format tool on Ubuntu 16+ systems to format C++ code according to Google's style guide.
```bash
$ apt-get install -y clang-format
```
--------------------------------
### Install Libraries
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/load_data/video.ipynb
Installs necessary libraries: remotezip for ZIP file inspection, tqdm for progress bars, and opencv-python for video processing.
```bash
!pip install remotezip tqdm opencv-python
!pip install -q git+https://github.com/tensorflow/docs
```
--------------------------------
### Get Predictions from a Subclassed Model
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/load_data/pandas_dataframe.ipynb
Generate predictions for the first three examples using the trained model.
```python
model.predict(dict(numeric_features.iloc[:3]))
```
--------------------------------
### Import Libraries and Setup Environment
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/reinforcement_learning/actor_critic.ipynb
Import required libraries, create the CartPole environment, and set random seeds for reproducibility. Includes a small epsilon value for stabilizing division operations.
```python
import collections
import gym
import numpy as np
import statistics
import tensorflow as tf
import tqdm
from matplotlib import pyplot as plt
from tensorflow.keras import layers
from typing import Any, List, Sequence, Tuple
# Create the environment
env = gym.make("CartPole-v1")
# Set seed for experiment reproducibility
seed = 42
tf.random.set_seed(seed)
np.random.seed(seed)
# Small epsilon value for stabilizing division operations
olds = np.finfo(np.float32).eps.item()
```
--------------------------------
### Display Library Versions
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/tf2_semantic_approximate_nearest_neighbors.ipynb
Prints the installed versions of TensorFlow, TensorFlow Hub, and Apache Beam to verify the setup.
```python
print('TF version: {}'.format(tf.__version__))
print('TF-Hub version: {}'.format(hub.__version__))
print('Apache Beam version: {}'.format(beam.__version__))
```
--------------------------------
### Prepare sample data for demonstration
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/migrate/metrics_optimizers.ipynb
Define sample features and labels for training and evaluation to demonstrate metric calculations.
```python
features = [[1., 1.5], [2., 2.5], [3., 3.5]]
labels = [0, 0, 1]
eval_features = [[4., 4.5], [5., 5.5], [6., 6.5]]
eval_labels = [0, 1, 1]
```
--------------------------------
### Launch TensorBoard
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/images/transfer_learning_with_hub.ipynb
Starts the TensorBoard visualization tool to monitor training metrics and logs stored in the specified directory.
```python
%tensorboard --logdir logs/fit
```
--------------------------------
### Predict with Exported Model
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/text_classification.ipynb
Get predictions for new text examples using an exported model. Ensure the input is a TensorFlow constant tensor.
```python
examples = tf.constant([
"The movie was great!",
"The movie was okay.",
"The movie was terrible..."
])
export_model.predict(examples)
```
--------------------------------
### Inspect dataset examples
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/text_classification.ipynb
Iterate over a tf.data.Dataset to view raw text reviews and their corresponding labels. Use .take(1) to get a single batch.
```python
for text_batch, label_batch in raw_train_ds.take(1):
for i in range(3):
print("Review", text_batch.numpy()[i])
print("Label", label_batch.numpy()[i])
```
--------------------------------
### Load Classifier and Get Predictions
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/cropnet_cassava.ipynb
Loads a pre-trained cassava disease classifier from TensorFlow Hub and generates probability predictions for the input image examples.
```python
classifier = hub.KerasLayer('https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2')
probabilities = classifier(examples['image'])
predictions = tf.argmax(probabilities, axis=-1)
```
--------------------------------
### Verify TensorFlow GPU Installation
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/pip.md
Run this command to verify your GPU setup. If a list of GPU devices is returned, TensorFlow has successfully detected your GPU.
```python
import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))
```
--------------------------------
### Perform Initial Prediction and Get State
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/structured_data/time_series.ipynb
Calls the `warmup` method on example input data to obtain the initial prediction and the model's internal state.
```python
prediction, state = feedback_model.warmup(multi_window.example[0])
prediction.shape
```
--------------------------------
### Initialize Summary Writer for TensorBoard
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/generative/pix2pix.ipynb
Sets up a TensorBoard summary writer to log training metrics. Ensure the log directory exists.
```python
log_dir="logs/"
summary_writer = tf.summary.create_file_writer(
log_dir + "fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
```
--------------------------------
### Verify GPU Installation
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/pip.md
Execute this Python command to check if TensorFlow can detect your GPU. A list of physical GPU devices confirms successful GPU setup.
```python
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
```
--------------------------------
### Run Docstring Tests for a Specific File
Source: https://github.com/tensorflow/docs/blob/master/site/en/community/contribute/docs_ref.md
Use this command to test docstring examples for a single file. Ensure you have TensorFlow installed, preferably the nightly build for up-to-date code.
```bash
python tf_doctest.py --file=
```
--------------------------------
### Launch Steps and Monitor Completion
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/distribute/parameter_server_training.ipynb
Launch multiple training steps using coordinator.schedule and monitor their completion using coordinator.done() in a loop. This allows for concurrent execution and background processing.
```python
for _ in range(total_steps):
coordinator.schedule(step_fn, args=(per_worker_iterator,))
while not coordinator.done():
time.sleep(10)
# Do something like logging metrics or writing checkpoints.
```
--------------------------------
### Build tf.Example Batch
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/ragged_tensor.ipynb
Creates a batch of four tf.Example messages using protobuf encoding, demonstrating variable-length features like 'colors' and 'lengths'.
```python
import google.protobuf.text_format as pbtext
def build_tf_example(s):
return pbtext.Merge(s, tf.train.Example()).SerializeToString()
example_batch = [
build_tf_example(r'''
features {
feature {key: "colors" value {bytes_list {value: ["red", "blue"]} } }
feature {key: "lengths" value {int64_list {value: [7]} } } }'''),
build_tf_example(r'''
features {
feature {key: "colors" value {bytes_list {value: ["orange"]} } }
feature {key: "lengths" value {int64_list {value: []} } } }'''),
build_tf_example(r'''
features {
feature {key: "colors" value {bytes_list {value: ["black", "yellow"]} } }
feature {key: "lengths" value {int64_list {value: [1, 3]} } } }'''),
build_tf_example(r'''
features {
feature {key: "colors" value {bytes_list {value: ["green"]} } }
feature {key: "lengths" value {int64_list {value: [3, 5, 2]} } } }''')]
```
--------------------------------
### Setting TF_CONFIG for Worker and PS Roles
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/distribute/parameter_server_training.ipynb
Configure the TF_CONFIG environment variable for a distributed training setup with workers and parameter servers. This example shows the structure for worker 1.
```python
os.environ["TF_CONFIG"] = json.dumps({
"cluster": {
"worker": ["host1:port", "host2:port", "host3:port"],
"ps": ["host4:port", "host5:port"],
"chief": ["host6:port"]
},
"task": {"type": "worker", "index": 1}
})
```
--------------------------------
### Start TensorFlow Servers for Parameter Server Training
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/migrate/multi_worker_cpu_gpu_training.ipynb
This code initializes TensorFlow servers for a distributed training setup. It configures worker servers with specific inter-op parallelism and parameter servers.
```python
worker_config = tf.compat.v1.ConfigProto()
worker_config.inter_op_parallelism_threads = 4
for i in range(3):
tf.distribute.Server(
cluster_resolver.cluster_spec(),
job_name="worker",
task_index=i,
config=worker_config)
for i in range(2):
tf.distribute.Server(
cluster_resolver.cluster_spec(),
job_name="ps",
task_index=i)
```
--------------------------------
### Sample TensorFlow Configuration Session
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/source_windows.md
This is a sample interactive session for the `./configure.py` script, illustrating the prompts for Python paths, ROCm/CUDA support, compiler choices, and optimization flags. Your session may vary.
```bash
python ./configure.py
You have bazel 6.5.0 installed.
Please specify the location of python. [Default is C:\Python311\python.exe]:
Found possible Python library paths:
C:\Python311\lib\site-packages
Please input the desired Python library path to use. Default is [C:\Python311\lib\site-packages]
Do you wish to build TensorFlow with ROCm support? [y/N]:
No ROCm support will be enabled for TensorFlow.
WARNING: Cannot build with CUDA support on Windows.
Starting in TF 2.11, CUDA build is not supported for Windows. To use TensorFlow GPU on Windows, you will need to build/install TensorFlow in WSL2.
Do you want to use Clang to build TensorFlow? [Y/n]:
Add "--config=win_clang" to compile TensorFlow with CLANG.
Please specify the path to clang executable. [Default is C:\Program Files\LLVM\bin\clang.EXE]:
You have Clang 17.0.6 installed.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]:
Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:
Eigen strong inline overridden.
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:
Not configuring the WORKSPACE for Android builds.
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>") to your build command. See .bazelrc for more details.
--config=mkl # Build with MKL support.
--config=mkl_aarch64 # Build with oneDNN and Compute Library for the Arm Architecture (ACL).
--config=monolithic # Config for mostly static monolithic build.
--config=numa # Build with NUMA support.
--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
--config=v1 # Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
--config=nogcp # Disable GCP support.
--config=nonccl # Disable NVIDIA NCCL support.
```
--------------------------------
### Compile and run TensorFlow C example (basic)
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/lang_c.ipynb
Compiles the hello_tf.c program using gcc and links against the TensorFlow library, then runs the executable.
```bash
gcc hello_tf.c -ltensorflow -o hello_tf
./hello_tf
```
--------------------------------
### Load Image and Run Inference
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/hrnet_semantic_segmentation.ipynb
Loads an example image, preprocesses it, and then runs inference using the loaded HRNet model to get predictions and features. Displays the original image, predictions, and features.
```python
img_file = tf.keras.utils.get_file(origin="https://tensorflow.org/images/bedroom_hrnet_tutorial.jpg")
img = np.array(Image.open(img_file))/255.0
```
```python
plt.imshow(img)
plt.show()
# Predictions will have shape (batch_size, h, w, dataset_output_classes)
predictions = hrnet_model.predict([img])
plt.imshow(predictions[0,:,:,1])
plt.title('Predictions for class #1')
plt.show()
# Features will have shape (batch_size, h/4, w/4, 720)
features = hrnet_model.get_features([img])
plt.imshow(features[0,:,:,1])
plt.title('Feature #1 out of 720')
plt.show()
```
--------------------------------
### Load Universal Sentence Encoder Module
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/semantic_similarity_with_tf_hub_universal_encoder.ipynb
Loads the Universal Sentence Encoder module from TensorFlow Hub. This code requires TensorFlow and TensorFlow Hub to be installed. It defines an 'embed' function to easily get embeddings.
```python
#@title Load the Universal Sentence Encoder's TF Hub module
from absl import logging
import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import re
import seaborn as sns
module_url = "https://tfhub.dev/google/universal-sentence-encoder/4" #@param ["https://tfhub.dev/google/universal-sentence-encoder/4", "https://tfhub.dev/google/universal-sentence-encoder-large/5"]
model = hub.load(module_url)
print ("module %s loaded" % module_url)
def embed(input):
return model(input)
```
--------------------------------
### Train step with multiple optimizers (error case)
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/function.ipynb
This example demonstrates the ValueError that occurs when using multiple Keras optimizers with a single tf.function. It shows the setup and the expected error when attempting to use a second optimizer.
```python
opt1 = tf.keras.optimizers.Adam(learning_rate = 1e-2)
opt2 = tf.keras.optimizers.Adam(learning_rate = 1e-3)
@tf.function
def train_step(w, x, y, optimizer):
with tf.GradientTape() as tape:
L = tf.reduce_sum(tf.square(w*x - y))
gradients = tape.gradient(L, [w])
optimizer.apply_gradients(zip(gradients, [w]))
w = tf.Variable(2.)
x = tf.constant([-1.])
y = tf.constant([2.])
train_step(w, x, y, opt1)
print("Calling `train_step` with different optimizer...")
with assert_raises(ValueError):
train_step(w, x, y, opt2)
```
--------------------------------
### Install virtualenv
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/build_from_source.md
Installs the virtualenv package on Debian/Ubuntu-based systems. Ensure you have Python and pip installed.
```shell
~$ sudo apt-get install python-virtualenv
```
--------------------------------
### Install tfds-nightly
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/customization/custom_training_walkthrough.ipynb
Install the nightly released version of TensorFlow Datasets. Restart the Colab runtime after installation.
```python
#@param {
# "field_type": "text",
# "name": "pip_install_tfds_nightly"
#}
!pip install -q tfds-nightly
```
--------------------------------
### Sample TensorFlow Configuration Session
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/source.md
An example of an interactive session when running the ./configure script. It shows prompts for Python location, library paths, and build options like ROCm, CUDA, and Clang support.
```bash
./configure
You have bazel 6.1.0 installed.
Please specify the location of python. [Default is /Library/Frameworks/Python.framework/Versions/3.9/bin/python3]:
Found possible Python library paths:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages
Please input the desired Python library path to use. Default is [/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages]
Do you wish to build TensorFlow with ROCm support? [y/N]:
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]:
No CUDA support will be enabled for TensorFlow.
Do you want to use Clang to build TensorFlow? [Y/n]:
Clang will be used to compile TensorFlow.
Please specify the path to clang executable. [Default is /usr/lib/llvm-16/bin/clang]:
You have Clang 16.0.4 installed.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n
Not configuring the WORKSPACE for Android builds.
Do you wish to build TensorFlow with iOS support? [y/N]: n
No iOS support will be enabled for TensorFlow.
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>":
--config=mkl # Build with MKL support.
--config=mkl_aarch64 # Build with oneDNN and Compute Library for the Arm Architecture (ACL).
--config=monolithic # Config for mostly static monolithic build.
--config=numa # Build with NUMA support.
--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
--config=v1 # Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
--config=nogcp # Disable GCP support.
--config=nonccl # Disable NVIDIA NCCL support.
```
--------------------------------
### Print Sample Data Points
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/load_data/text.ipynb
Takes the first 10 examples from the prepared dataset and prints the text and its corresponding label. Note that the dataset has not been batched yet.
```python
for text, label in all_labeled_data.take(10):
print("Sentence: ", text.numpy())
print("Label:", label.numpy())
```
--------------------------------
### Install TensorFlow with pip
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/pip.md
Use this command to install the TensorFlow package using pip. Ensure you have pip installed and updated.
```bash
pip install tensorflow
```
--------------------------------
### Load and preprocess an image
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/saved_model.ipynb
Downloads an example image, loads it, and preprocesses it for model input. The image is resized to 224x224 pixels.
```python
file = tf.keras.utils.get_file(
"grace_hopper.jpg",
"https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg")
img = tf.keras.utils.load_img(file, target_size=[224, 224])
plt.imshow(img)
plt.axis('off')
x = tf.keras.utils.img_to_array(img)
x = tf.keras.applications.mobilenet.preprocess_input(
x[tf.newaxis,...])
```
--------------------------------
### Build Model Weights and Display Summary
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/wav2vec2_saved_model_finetuning.ipynb
Initializes the model's weights by calling it with random data and then prints a summary of the model architecture and trainable parameters. This step is necessary before training.
```python
model(tf.random.uniform(shape=(BATCH_SIZE, AUDIO_MAXLEN)))
model.summary()
```
--------------------------------
### Display First 10 Training Examples
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/tf2_text_classification.ipynb
Shows the first 10 movie reviews from the training dataset. Useful for a quick inspection of the raw text data.
```python
train_examples[:10]
```
--------------------------------
### Configure Training, Validation, and Test Datasets
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/images/data_augmentation.ipynb
Apply the `prepare` function to configure the training, validation, and test datasets. This includes enabling shuffling and augmentation for the training set and applying performance optimizations to all datasets.
```python
train_ds = prepare(train_ds, shuffle=True, augment=True)
val_ds = prepare(val_ds)
test_ds = prepare(test_ds)
```
--------------------------------
### Install TensorFlow Compression
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/optimization/compression.ipynb
Installs the latest version of TensorFlow Compression compatible with your installed TensorFlow version. This is a prerequisite for using the library.
```bash
# Installs the latest version of TFC compatible with the installed TF version.
read MAJOR MINOR <<< "$(pip show tensorflow | perl -p -0777 -e 's/.*Version: (\d+)\.(\d+).*/\1 \2/sg')"
pip install "tensorflow-compression<$MAJOR.$(($MINOR+1))"
```
--------------------------------
### Compile and run TensorFlow C example (explicit paths)
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/lang_c.ipynb
Compiles the hello_tf.c program, explicitly specifying include and library paths for the TensorFlow C library, then runs the executable.
```bash
gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf
./hello_tf
```
--------------------------------
### Install TensorFlow Compression
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/generative/data_compression.ipynb
Installs the latest version of TensorFlow Compression compatible with your installed TensorFlow version. This is a prerequisite for using the library.
```bash
#@param \"bash\"
# Installs the latest version of TFC compatible with the installed TF version.
read MAJOR MINOR <<< "$(pip show tensorflow | perl -p -0777 -e 's/.*Version: (\d+)\\.(\d+).*/\1 \\2/sg')"
pip install "tensorflow-compression<$MAJOR.$(($MINOR+1))"
```
--------------------------------
### Verify CPU Installation
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/pip.md
Run this Python command to verify your TensorFlow CPU installation. A tensor output indicates a successful installation.
```python
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
```
--------------------------------
### Configure Model Parameters and Dataset
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/migrate/migration_debugging.ipynb
Sets up model configuration parameters and prepares a small, fixed fake dataset for testing.
```python
params = {
'input_size': 3,
'num_classes': 3,
'layer_1_size': 2,
'layer_2_size': 2,
'num_train_steps': 100,
'init_lr': 1e-3,
'end_lr': 0.0,
'decay_steps': 1000,
'lr_power': 1.0,
}
# make a small fixed dataset
fake_x = np.ones((2, params['input_size']), dtype=np.float32)
fake_y = np.zeros((2, params['num_classes']), dtype=np.int32)
fake_y[0][0] = 1
fake_y[1][1] = 1
step_num = 3
```
--------------------------------
### Create model, datasets, and tf.functions for custom training loop
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/tpu.ipynb
Set up the model, optimizer, metrics, and distribute datasets within the `tf.distribute.Strategy` scope for custom training loops. The `Dataset` batch size should be per-replica.
```python
# Create the model, optimizer and metrics inside the `tf.distribute.Strategy`
# scope, so that the variables can be mirrored on each device.
with strategy.scope():
model = create_model()
optimizer = tf.keras.optimizers.Adam()
training_loss = tf.keras.metrics.Mean('training_loss', dtype=tf.float32)
training_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
'training_accuracy', dtype=tf.float32)
# Calculate per replica batch size, and distribute the `tf.data.Dataset`s
# on each TPU worker.
per_replica_batch_size = batch_size // strategy.num_replicas_in_sync
train_dataset = strategy.distribute_datasets_from_function(
lambda _: get_dataset(per_replica_batch_size, is_training=True))
@tf.function
def train_step(iterator):
"""The step function for one training step."""
def step_fn(inputs):
"""The computation to run on each TPU device."""
images, labels = inputs
with tf.GradientTape() as tape:
logits = model(images, training=True)
per_example_loss = tf.keras.losses.sparse_categorical_crossentropy(
labels, logits, from_logits=True)
loss = tf.nn.compute_average_loss(per_example_loss)
model_losses = model.losses
if model_losses:
loss += tf.nn.scale_regularization_loss(tf.add_n(model_losses))
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(list(zip(grads, model.trainable_variables)))
training_loss.update_state(loss * strategy.num_replicas_in_sync)
training_accuracy.update_state(labels, logits)
strategy.run(step_fn, args=(next(iterator),))
```
--------------------------------
### Install TensorFlow dependencies on Windows
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/source_windows.md
Install the required Python packages for building TensorFlow. Ensure pip is up-to-date before installing other dependencies.
```bash
pip3 install -U pip
```
```bash
pip3 install -U six numpy wheel packaging
```
```bash
pip3 install -U keras_preprocessing --no-deps
```
--------------------------------
### Print Sample File Paths from Dataset
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/load_data/images.ipynb
Takes the first 5 elements from the dataset and prints their file paths. This is useful for verifying the dataset creation.
```python
for f in list_ds.take(5):
print(f.numpy())
```
--------------------------------
### Install Profiler Plugin for TensorBoard
Source: https://github.com/tensorflow/docs/blob/master/site/en/guide/profiler.md
Install the Profiler plugin for TensorBoard using pip. Ensure you have TensorFlow and TensorBoard (>=2.2) installed.
```shell
pip install -U tensorboard_plugin_profile
```
--------------------------------
### Launch TensorBoard for Visualization
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/distribute/keras.ipynb
Launch TensorBoard to visualize training logs. Ensure logs are being written to the specified directory.
```python
%tensorboard --logdir=logs
```
--------------------------------
### Load and Inspect Video Sample
Source: https://github.com/tensorflow/docs/blob/master/site/en/hub/tutorials/action_recognition_with_tf_hub.ipynb
Loads a video from a given path, taking the first 100 frames, and then displays its shape. This assumes the `load_video` function is defined.
```python
video_path = "End_of_a_jam.ogv"
sample_video = load_video(video_path)[:100]
sample_video.shape
```
--------------------------------
### Install TensorFlow
Source: https://github.com/tensorflow/docs/blob/master/site/en/install/pip.md
Install TensorFlow using pip. Use the '[and-cuda]' option for GPU support, or install the base package for CPU-only usage.
```bash
# For GPU users
pip install tensorflow[and-cuda]
# For CPU users
pip install tensorflow
```
--------------------------------
### Main Training Script Setup
Source: https://github.com/tensorflow/docs/blob/master/site/en/tutorials/distribute/multi_worker_with_ctl.ipynb
Provides the basic setup for a distributed TensorFlow training script, including environment variable parsing for TF_CONFIG, calculating global batch size, and defining training parameters like epochs and steps per epoch.
```python
%%writefile main.py
#@title File: `main.py`
import os
import json
import tensorflow as tf
import mnist
from multiprocessing import util
per_worker_batch_size = 64
tf_config = json.loads(os.environ['TF_CONFIG'])
num_workers = len(tf_config['cluster']['worker'])
global_batch_size = per_worker_batch_size * num_workers
num_epochs = 3
num_steps_per_epoch=70
```