TensorFlow is an open-source machine learning framework developed by Google. It enables developers to build, train, and deploy machine learning models efficiently. TensorFlow 2.0+ introduced significant improvements over its predecessor, making it more user-friendly and flexible.
tf.Session().tf.layers and tf.estimator with Keras.pip install tensorflow
Verify installation:
import tensorflow as tf
print(tf.__version__) # Should output 2.x.x
Eager execution allows operations to be evaluated immediately:
a = tf.constant(5)
b = tf.constant(3)
print(a + b) # Output: tf.Tensor(8, shape=(), dtype=int32)
TensorFlow 2.0 fully integrates Keras, simplifying model building:
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(1)
])
dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
dataset = dataset.shuffle(1000).batch(32)
import tensorflow_hub as hub
model = tf.keras.Sequential([
hub.KerasLayer("https://tfhub.dev/google/imagenet/mobilenet_v2/classification/4"),
tf.keras.layers.Dense(10, activation='softmax')
])
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
inputs = tf.keras.Input(shape=(32, 32, 3))
x = tf.keras.layers.Conv2D(32, 3, activation='relu')(inputs)
outputs = tf.keras.layers.Dense(10)(x)
model = tf.keras.Model(inputs, outputs)
class MyModel(tf.keras.Model):
def __init__(self):
super().__init__()
self.dense1 = tf.keras.layers.Dense(64, activation='relu')
self.dense2 = tf.keras.layers.Dense(10)
def call(self, inputs):
x = self.dense1(inputs)
return self.dense2(x)
model.save('my_model.h5')
loaded_model = tf.keras.models.load_model('my_model.h5')
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
callbacks = [
tf.keras.callbacks.EarlyStopping(patience=3),
tf.keras.callbacks.ModelCheckpoint('best_model.h5')
]
model.fit(X_train, y_train, epochs=10, callbacks=callbacks)
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
model = build_model() # Define model inside strategy scope
import keras_tuner as kt
def build_model(hp):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units=hp.Int('units', 32, 512, 32), activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return model
tuner = kt.RandomSearch(build_model, objective='val_accuracy', max_trials=5)
tuner.search(X_train, y_train, epochs=5, validation_data=(X_val, y_val))
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10, activation='softmax')
])
base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False)
x = base_model.output
x = tf.keras.layers.GlobalAvgPool2D()(x)
predictions = tf.keras.layers.Dense(10, activation='softmax')(x)
model = tf.keras.Model(inputs=base_model.input, outputs=predictions)
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip("horizontal"),
tf.keras.layers.RandomRotation(0.2),
])
tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=1000)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
embedding_layer = tf.keras.layers.Embedding(1000, 64)
model = tf.keras.Sequential([
tf.keras.layers.Embedding(1000, 64),
tf.keras.layers.LSTM(128),
tf.keras.layers.Dense(1, activation='sigmoid')
])
from transformers import TFAutoModel
model = TFAutoModel.from_pretrained("bert-base-uncased")
docker pull tensorflow/serving
docker run -p 8501:8501 --name tfserving_model --mount type=bind,source=$(pwd)/model,target=/models/model -e MODEL_NAME=model -t tensorflow/serving
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
pip install tensorflowjs
tensorflowjs_converter --input_format=keras model.h5 ./tfjs_model
class CustomLayer(tf.keras.layers.Layer):
def __init__(self, units=32):
super().__init__()
self.units = units
def build(self, input_shape):
self.w = self.add_weight(shape=(input_shape[-1], self.units), initializer="random_normal")
def call(self, inputs):
return tf.matmul(inputs, self.w)
tf.keras.mixed_precision.set_global_policy('mixed_float16')
from tfx import v1 as tfx
example_gen = tfx.components.CsvExampleGen(input_base='data_path')
TensorFlow 2.0+ provides a powerful yet flexible framework for machine learning. With its intuitive Keras API, eager execution, and deployment tools, it is an excellent choice for both beginners and experts.