You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Constant.md 1.4 kB

6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Chapter. Constant
  2. In TensorFlow, a constant is a special Tensor that cannot be modified while the graph is running. Like in a linear model $\tilde{y_i}=\boldsymbol{w}x_i+b$, constant $b$ can be represented as a Constant Tensor. Since the constant is a Tensor, it also has all the data characteristics of Tensor, including:
  3. * value: scalar value or constant list matching the data type defined in TensorFlow;
  4. * dtype: data type;
  5. * shape: dimensions;
  6. * name: constant's name;
  7. ##### How to create a Constant
  8. 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.
  9. Initialize a scalar constant:
  10. ```csharp
  11. var c1 = tf.constant(3); // int
  12. var c2 = tf.constant(1.0f); // float
  13. var c3 = tf.constant(2.0); // double
  14. var c4 = tf.constant("Big Tree"); // string
  15. ```
  16. Initialize a constant through ndarray:
  17. ```csharp
  18. // dtype=int, shape=(2, 3)
  19. var nd = np.array(new int[][]
  20. {
  21. new int[]{3, 1, 1},
  22. new int[]{2, 3, 1}
  23. });
  24. var tensor = tf.constant(nd);
  25. ```
  26. ##### Dive in Constant
  27. Now let's explore how `constant` works.
  28. ##### Other functions to create a Constant
  29. * tf.zeros
  30. * tf.zeros_like
  31. * tf.ones
  32. * tf.ones_like
  33. * tf.fill