TensorFlow中tf.constant与tf.Variable的理解及类型、可训练性问题
Great questions, let’s break these down clearly:
tf.constant and tf.Variable in TensorFlow Think of tf.constant as a fixed "value box"—once you define it, its content never changes during your model’s run. It’s ideal for static values like fixed hyperparameters (e.g., a static learning rate) or precomputed constants that don’t need updates. Here’s a quick example:
import tensorflow as tf fixed_offset = tf.constant(0.5, shape=(1,))
No matter how many times you run your graph or train your model, fixed_offset will stay 0.5.
tf.Variable, by contrast, is built for values that need to evolve during training—like your model’s weights, biases, or other learnable parameters. These are the values your optimizer adjusts to minimize loss. Variables require initialization (automatic in TF2.x eager mode, or via tf.global_variables_initializer() in TF1.x). For example:
model_weights = tf.Variable(tf.random.normal((3, 4)), trainable=True)
By default, variables are marked as trainable, but you can toggle this off if you need a variable that stays fixed.
K = tf.random_normal([2, 3]) and Trainability Clarifications First, let’s set the record straight: tf.random_normal([2, 3]) (use tf.random.normal() in TF2.x) returns a regular Tensor object—it doesn’t fall into the tf.constant, tf.Variable, or tf.placeholder categories.
To check its type, run this code:
K = tf.random.normal([2, 3]) # Use tf.random_normal for TF1.x print(type(K)) # Outputs <class 'tensorflow.python.framework.ops.EagerTensor'> (TF2.x) or <class 'tensorflow.python.framework.ops.Tensor'> (TF1.x) print(K.dtype) # Shows the data type, e.g., tf.float32
Now for the trainability bits:
tf.constantis not trainable—since its value is fixed, there’s nothing for the optimizer to update. It’s treated as a static node in the computation graph.tf.Variableis trainable by default, but you can make it non-trainable by settingtrainable=Falsewhen defining it:
static_var = tf.Variable(tf.constant(10.0), trainable=False)
This variable won’t be modified by optimizers during training.
内容的提问来源于stack exchange,提问作者guorui

