You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow中tf.constant与tf.Variable的理解及类型、可训练性问题

Great questions, let’s break these down clearly:

1. Understanding 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.

2. Type of 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.constant is 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.Variable is trainable by default, but you can make it non-trainable by setting trainable=False when 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:15:00