10 KiB
TensorFlow
An end-to-end open source platform for machine learning that provides a comprehensive ecosystem of tools, libraries, and community resources for both research and production deployment. TensorFlow supports stable APIs for Python and C++, along with experimental APIs for other languages, enabling developers to build and deploy ML-powered applications efficiently across various hardware accelerators (CPUs, GPUs, TPUs) and distributed training environments.
Package Information
- Package Name: tensorflow
- Language: Python (with C++ core)
- Installation:
pip install tensorflow - Version: 2.20.0
Core Imports
import tensorflow as tf
For Keras high-level API:
from tensorflow import keras
For specific modules:
from tensorflow.keras import layers, models, optimizers
from tensorflow.data import Dataset
import tensorflow.nn as nn
Basic Usage
import tensorflow as tf
import numpy as np
# Create tensors
x = tf.constant([1.0, 2.0, 3.0])
y = tf.constant([4.0, 5.0, 6.0])
# Basic operations
z = tf.add(x, y)
print(z) # tf.Tensor([5. 7. 9.], shape=(3,), dtype=float32)
# Create a simple neural network with Keras
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Create sample data
X_train = np.random.random((1000, 784))
y_train = np.random.randint(10, size=(1000,))
# Train the model
model.fit(X_train, y_train, epochs=5, batch_size=32)
Architecture
TensorFlow's architecture is built around several key components:
- Tensors: Multi-dimensional arrays that flow through computational graphs
- Operations: Mathematical computations performed on tensors
- Graphs: Computational graphs that define the flow of data and operations
- Sessions: Runtime environments for executing graphs (TF 1.x) or eager execution (TF 2.x)
- Keras: High-level API for building and training neural networks
- Estimators: High-level API for distributed training and evaluation
The framework supports both eager execution (default in TF 2.x) for immediate operation evaluation and graph mode for optimized production deployment.
Capabilities
Core Tensor Operations
Fundamental tensor creation, manipulation, and mathematical operations that form the foundation of TensorFlow computations.
def constant(value, dtype=None, shape=None, name="Const"): ...
def Variable(initial_value, trainable=None, validate_shape=True,
caching_device=None, name=None, variable_def=None, dtype=None,
import_scope=None, constraint=None, synchronization=tf.VariableSynchronization.AUTO,
aggregation=tf.VariableAggregation.NONE, shape=None,
experimental_enable_variable_lifting=True): ...
def convert_to_tensor(value, dtype=None, dtype_hint=None, name=None): ...
def cast(x, dtype, name=None): ...
def reshape(tensor, shape, name=None): ...
def transpose(a, perm=None, conjugate=False, name="transpose"): ...
def zeros(shape, dtype=tf.float32, name=None): ...
def ones(shape, dtype=tf.float32, name=None): ...
Math Operations
Comprehensive mathematical operations including arithmetic, trigonometric, linear algebra, and statistical functions.
def add(x, y, name=None): ...
def subtract(x, y, name=None): ...
def multiply(x, y, name=None): ...
def divide(x, y, name=None): ...
def matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False,
adjoint_b=False, a_is_sparse=False, b_is_sparse=False, output_type=None,
grad_a=False, grad_b=False, name=None): ...
def reduce_sum(input_tensor, axis=None, keepdims=None, name=None): ...
def reduce_mean(input_tensor, axis=None, keepdims=None, name=None): ...
Neural Network Operations
Core neural network operations including activations, convolutions, pooling, normalization, and loss functions.
def relu(features, name=None): ...
def softmax(logits, axis=None, name=None): ...
def conv2d(input, filters, strides, padding, use_cudnn_on_gpu=True, data_format="NHWC",
dilations=[1,1,1,1], name=None): ...
def max_pool2d(input, ksize, strides, padding, data_format="NHWC", name=None): ...
def batch_normalization(x, mean, variance, offset, scale, variance_epsilon, name=None): ...
def softmax_cross_entropy_with_logits(labels, logits, axis=-1, name=None): ...
Keras High-Level API
High-level neural network building blocks including models, layers, optimizers, losses, and metrics for rapid prototyping and production.
class Sequential(Model): ...
class Model: ...
class Dense(Layer): ...
class Conv2D(Layer): ...
class LSTM(Layer): ...
class Adam(Optimizer): ...
class SGD(Optimizer): ...
Data Processing
Dataset creation, transformation, and preprocessing pipeline operations for efficient data handling and training workflows.
class Dataset:
@staticmethod
def from_tensor_slices(tensors, name=None): ...
@staticmethod
def from_tensors(tensors, name=None): ...
def map(self, map_func, num_parallel_calls=None, deterministic=None, name=None): ...
def batch(self, batch_size, drop_remainder=False, num_parallel_calls=None,
deterministic=None, name=None): ...
def shuffle(self, buffer_size, seed=None, reshuffle_each_iteration=None, name=None): ...
def repeat(self, count=None, name=None): ...
Image Processing
Comprehensive image manipulation, transformation, and computer vision operations for preprocessing and augmentation.
def decode_image(contents, channels=None, dtype=tf.uint8, name=None, expand_animations=True): ...
def resize(images, size, method=ResizeMethod.BILINEAR, preserve_aspect_ratio=False,
antialias=False, name=None): ...
def random_flip_left_right(image, seed=None): ...
def random_brightness(image, max_delta, seed=None): ...
def convert_image_dtype(image, dtype, saturate=False, name=None): ...
Model Saving and Loading
Complete model serialization, checkpointing, and deployment utilities for production and inference.
def save(obj, export_dir, signatures=None, options=None): ...
def load(export_dir, tags=None, options=None): ...
class Checkpoint:
def __init__(self, **kwargs): ...
def save(self, file_prefix, session=None): ...
def restore(self, save_path): ...
Distribution Strategies
Multi-device and multi-worker training strategies for scaling machine learning workloads across GPUs and TPUs.
class MirroredStrategy(Strategy): ...
class MultiWorkerMirroredStrategy(Strategy): ...
class TPUStrategy(Strategy): ...
class ParameterServerStrategy(Strategy): ...
Automatic Differentiation
Gradient computation and automatic differentiation functionality for training neural networks.
class GradientTape:
def __init__(self, persistent=False, watch_accessed_variables=True): ...
def watch(self, tensor): ...
def gradient(self, target, sources, output_gradients=None,
unconnected_gradients=UnconnectedGradients.NONE): ...
def gradient(target, sources, grad_ys=None, name="gradients",
gate_gradients=False, aggregation_method=None,
stop_gradients=None, unconnected_gradients=UnconnectedGradients.NONE): ...
Random Operations
Random number generation and sampling operations for stochastic computations.
def random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None): ...
def random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None): ...
def random_shuffle(value, seed=None, name=None): ...
def set_seed(seed): ...
Configuration and System
System configuration, device management, and runtime settings.
def list_physical_devices(device_type=None): ...
def list_logical_devices(device_type=None): ...
def experimental_set_memory_growth(device, enable): ...
def experimental_get_memory_info(device): ...
Input/Output Operations
File system operations, data serialization, and I/O utilities.
def read_file(filename, name=None): ...
def write_file(filename, contents, name=None): ...
def matching_files(pattern, name=None): ...
def decode_raw(input_bytes, out_type, little_endian=True, fixed_length=None, name=None): ...
Types
class Tensor:
"""Multi-dimensional array with uniform data type."""
@property
def shape(self): ...
@property
def dtype(self): ...
@property
def device(self): ...
def numpy(self): ...
class Variable(Tensor):
"""Mutable tensor for storing model parameters."""
def assign(self, value, use_locking=None, name=None, read_value=True): ...
def assign_add(self, delta, use_locking=None, name=None, read_value=True): ...
class Operation:
"""Computation node in a TensorFlow graph."""
@property
def name(self): ...
@property
def type(self): ...
@property
def inputs(self): ...
@property
def outputs(self): ...
# Data types
DType = tf.DType
float16 = tf.float16
float32 = tf.float32
float64 = tf.float64
int8 = tf.int8
int16 = tf.int16
int32 = tf.int32
int64 = tf.int64
uint8 = tf.uint8
uint16 = tf.uint16
uint32 = tf.uint32
uint64 = tf.uint64
bool = tf.bool
string = tf.string
complex64 = tf.complex64
complex128 = tf.complex128
# Enumerations
class VariableSynchronization:
"""Variable synchronization modes for distributed training."""
NONE = "VariableSynchronization.NONE"
ON_WRITE = "VariableSynchronization.ON_WRITE"
ON_READ = "VariableSynchronization.ON_READ"
AUTO = "VariableSynchronization.AUTO"
class VariableAggregation:
"""Variable aggregation modes for distributed training."""
NONE = "VariableAggregation.NONE"
SUM = "VariableAggregation.SUM"
MEAN = "VariableAggregation.MEAN"
ONLY_FIRST_REPLICA = "VariableAggregation.ONLY_FIRST_REPLICA"
class UnconnectedGradients:
"""Gradient computation modes for unconnected inputs."""
NONE = "UnconnectedGradients.NONE"
ZERO = "UnconnectedGradients.ZERO"