C.W.K.
Stream
Lesson 01 of 07 · published

What Is a Tensor?

~11 min · tensor, rank, shape, dtype

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

The first object you must understand

Every input to a neural network, every model weight, every gradient, every activation — all of them are tensors. Before any architecture talk, internalize what a tensor is and is not.

A tensor is a generalization of scalars, vectors, and matrices to arbitrary numbers of dimensions. The number of dimensions is the rank (also called order or ndim):

  • Rank 0 — a scalar. Shape (). Example: 4.5.
  • Rank 1 — a vector. Shape (n,). Example: [1.0, 2.0, 3.0].
  • Rank 2 — a matrix. Shape (rows, cols). Example: a 28×28 grayscale image, shape (28, 28).
  • Rank 3 — Example: an RGB image, shape (height, width, 3).
  • Rank 4 — Common in deep learning: a batch of images, shape (batch, height, width, channels).

Every tensor has three core properties: shape (dimensions), dtype (data type), and values. The dtype determines storage precision — float32 for neural-network weights, int32/int64 for labels and indices, bool for masks.

Why this matters: Shape mismatches cause the majority of runtime errors in TF code. Once you can read shapes fluently — at every stage of a pipeline, after every layer — debugging becomes radically faster.

Code

Rank별 tensor 만들기·python
import tensorflow as tf

scalar = tf.constant(4.0)
print(scalar.shape, scalar.ndim)   # () 0

vector = tf.constant([1.0, 2.0, 3.0])
print(vector.shape, vector.ndim)   # (3,) 1

matrix = tf.constant([[1, 2, 3], [4, 5, 6]])
print(matrix.shape, matrix.ndim)   # (2, 3) 2

rgb_image = tf.constant([[[255, 0, 0], [0, 255, 0]],
                         [[0, 0, 255], [255, 255, 0]]])
print(rgb_image.shape)             # (2, 2, 3)

batch = tf.zeros([32, 224, 224, 3])
print(batch.shape, batch.ndim)     # (32, 224, 224, 3) 4

External links

Exercise

Pick three real-world data types — an audio waveform, a CSV row of housing data, a video clip. Write down the rank, expected shape (with dimension names), and dtype each one would have when fed to a TF model.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.