In TensorFlow, a constant is a special Tensor that cannot be modified while the graph is running. Like in a linear model
- value: scalar value or constant list matching the data type defined in TensorFlow;
- dtype: data type;
- shape: dimensions;
- name: constant's name;
TensorFlow provides a handy function to create a Constant. In TF.NET, you can use the same function name tf.constant to create it. TF.NET takes the same name as python binding to the API. Naming, although this will make developers who are used to C# naming habits feel uncomfortable, but after careful consideration, I decided to give up the C# convention naming method.
Initialize a scalar constant:
var c1 = tf.constant(3); // int
var c2 = tf.constant(1.0f); // float
var c3 = tf.constant(2.0); // double
var c4 = tf.constant("Big Tree"); // stringInitialize a constant through ndarray:
// dtype=int, shape=(2, 3)
var nd = np.array(new int[][]
{
new int[]{3, 1, 1},
new int[]{2, 3, 1}
});
var tensor = tf.constant(nd);Now let's explore how constant works.
- tf.zeros
- tf.zeros_like
- tf.ones
- tf.ones_like
- tf.fill