The Basic Unit of TensorFlow, Tensor
Tensor is the fundamental unit for representing data in TensorFlow.
In machine learning and deep learning models, you will work with numerous numerical data, primarily used to represent input data (features), weights, and loss.
Lossis the value that represents the difference between the predicted value by the model and the actual value, which the model strives to minimize during training.
Creating a Tensor
You can create immutable tensors using tf.constant().
import tensorflow as tf # Create a 1-D tensor tensor = tf.constant([1, 2, 3, 4, 5])
The code above creates a 1-dimensional tensor using tf.constant([1, 2, 3, 4, 5]).
In deep learning, values such as weights and biases change during training. To represent these mutable values, use tf.Variable().
# Create a trainable variable weight = tf.Variable([0.5, -0.3, 0.8], dtype=tf.float32)
This function is used to store values that update as the model trains, and is essential when using the automatic differentiation feature with tf.GradientTape().
Shape of Tensors
For example, in an image classification model, each pixel value of a specific image can be represented by using a tensor.
# Represent pixel values of an image as a tensor image_tensor = tf.constant([ [0, 128, 255, 64, 32], [12, 200, 30, 90, 255], [255, 180, 75, 40, 0], [80, 190, 250, 140, 30], [50, 100, 150, 200, 250] ], dtype=tf.float32)
In natural language processing (NLP), word embedding vectors are converted into tensors.
Word embeddingis a technique for representing words as numerical vectors, capturing semantic similarity between words.
Words with similar meanings are represented as similar vectors.
# Dog embedding vector dog = tf.constant([0.2, 0.4, 0.6, 0.8, 1.0], dtype=tf.float32) # Cat embedding vector (similar vector to dog) cat = tf.constant([0.25, 0.38, 0.58, 0.85, 0.95], dtype=tf.float32) # Car embedding vector (different vector from dog) car = tf.constant([-0.8, 0.1, -0.5, 0.3, -0.6], dtype=tf.float32)
The tensor examples above illustrate image data in the form of a 2-dimensional array and word embeddings in the form of 1-dimensional vectors.
In the next lesson, we will explore dimensions and shapes of tensors in greater detail.
Lessons in this chapter · Machine Learning: Learning Patterns Hidden in Data
- 1. What’s All the Hype About? What Is Machine Learning?
- 2. What Are Neural Networks That Mimic the Human Brain?
- 3. How Perceptrons Work
- 4. Taking It Further: What Is Deep Learning?
- 5. Machine Learning vs. Deep Learning: Key Differences
- 6. Multiple Choice Quiz
- 7. What Does It Mean to Train AI?
- 8. The Result of AI Training: Files Made of Matrices
- 9. Supervised Learning: Learning with the Right Answers
- 10. Unsupervised Learning: Finding Patterns Without Answers
- 11. Reinforcement Learning: Learning Through Rewards
- 12. Fill-in-the-Blank Quiz
- 13. TensorFlow: A Library for Machine Learning and Deep Learning
- 14. Tensor: The Core Unit of TensorFlow
- 15. Tensor Dimensions
- 16. Multiple Choice Quiz
- 17. Understanding Tensor Operations
- 18. Top 5 Most Used TensorFlow APIs
- 19. Building a Simple Linear Regression Model with TensorFlow
- 20. Fill-in-the-Blank Quiz
- 21. Keras: A Simple and Intuitive Neural Network Library
- 22. Training and Evaluating Models with Keras
- 23. Multiple Choice Quiz
In TensorFlow, tf.constant() is used to represent changing values.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help