Learn Tensorflow - 10 Code Examples & CST Typing Practice Test
TensorFlow is an open-source, end-to-end platform for machine learning developed by Google. It provides comprehensive tools, libraries, and community resources for building and deploying ML models across different environments.
View all 10 Tensorflow code examples →
Learn TENSORFLOW with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
TensorFlow Simple Linear Regression
import tensorflow as tf
import numpy as np
# Sample data
x_train = np.array([1, 2, 3, 4], dtype=float)
y_train = np.array([2, 4, 6, 8], dtype=float)
# Define a simple linear model
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
# Train the model
model.fit(x_train, y_train, epochs=500)
# Predict
y_pred = model.predict([10.0])
print("Prediction for 10:", y_pred)
A minimal TensorFlow example showing linear regression training on sample data.
TensorFlow Simple Neural Network
import tensorflow as tf
import numpy as np
# Sample data
x_train = np.random.rand(100, 3)
y_train = np.random.randint(0, 2, 100)
# Define model
model = tf.keras.Sequential([
tf.keras.layers.Dense(8, activation='relu', input_shape=[3]),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train
model.fit(x_train, y_train, epochs=50)
A basic feedforward neural network for classification.
TensorFlow MNIST Example
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train/255.0, x_test/255.0
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')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
Train a simple MNIST digit classifier using TensorFlow.
TensorFlow Convolutional Neural Network
import tensorflow as tf
from tensorflow.keras import layers, models
model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
A simple CNN for image classification.
TensorFlow LSTM Example
import tensorflow as tf
import numpy as np
# Sample sequence data
x_train = np.random.rand(100, 10, 1)
y_train = np.random.rand(100, 1)
model = tf.keras.Sequential([
tf.keras.layers.LSTM(50, input_shape=(10,1)),
tf.keras.layers.Dense(1)
])
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epochs=20)
A simple LSTM network for sequence prediction.
TensorFlow Autoencoder Example
import tensorflow as tf
from tensorflow.keras import layers, models
input_dim = 20
model = models.Sequential([
layers.Dense(10, activation='relu', input_shape=(input_dim,)),
layers.Dense(5, activation='relu'),
layers.Dense(10, activation='relu'),
layers.Dense(input_dim, activation='sigmoid')
])
model.compile(optimizer='adam', loss='mse')
A simple autoencoder for data compression.
TensorFlow Regression with Multiple Inputs
import tensorflow as tf
import numpy as np
x_train = np.random.rand(100,3)
y_train = np.dot(x_train, [1.5, -2.0, 1.0]) + 0.5
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, input_shape=[3])
])
model.compile(optimizer='sgd', loss='mse')
model.fit(x_train, y_train, epochs=100)
Linear regression with multiple input features.
TensorFlow Transfer Learning Example
import tensorflow as tf
base_model = tf.keras.applications.MobileNetV2(input_shape=(128,128,3), include_top=False, weights='imagenet')
base_model.trainable = False
model = tf.keras.Sequential([
base_model,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Use a pre-trained MobileNet model for image classification.
TensorFlow Custom Callback Example
import tensorflow as tf
class PrintLossCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
print(f"Epoch {epoch+1}: loss = {logs['loss']}")
model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mse')
model.fit([1,2,3], [2,4,6], epochs=5, callbacks=[PrintLossCallback()])
Define a custom callback to print loss after each epoch.
TensorFlow GAN Example
import tensorflow as tf
from tensorflow.keras import layers
# Generator
generator = tf.keras.Sequential([
layers.Dense(128, activation='relu', input_shape=(100,)),
layers.Dense(784, activation='sigmoid')
])
# Discriminator
discriminator = tf.keras.Sequential([
layers.Dense(128, activation='relu', input_shape=(784,)),
layers.Dense(1, activation='sigmoid')
])
# Compile discriminator
discriminator.compile(optimizer='adam', loss='binary_crossentropy')
A minimal GAN example structure.
Frequently Asked Questions about Tensorflow
What is Tensorflow?
TensorFlow is an open-source, end-to-end platform for machine learning developed by Google. It provides comprehensive tools, libraries, and community resources for building and deploying ML models across different environments.
What are the primary use cases for Tensorflow?
Deep learning for image, video, and speech recognition. Natural language processing and translation. Reinforcement learning for AI agents. Time series forecasting and predictive analytics. Deployment of AI models on cloud, mobile, and embedded devices
What are the strengths of Tensorflow?
Highly scalable for large datasets and models. Cross-platform support: desktop, mobile, cloud. Extensive ecosystem with tools and libraries. Strong community support and documentation. Production-ready pipelines and deployment options
What are the limitations of Tensorflow?
Steep learning curve for beginners. Verbose for low-level model definitions. Debugging can be complex for graph-based models. Python-centric (other languages supported but limited). Can be overkill for small or simple ML tasks
How can I practice Tensorflow typing speed?
CodeSpeedTest offers 10+ real Tensorflow code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.