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 2.3 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. 在TensorFlow中,常量是一种特殊的Tensor,它在计算图运行的时候,不能被修改。比如在线性模型里$\tilde{y_i}=\boldsymbol{w}x_i+b$, 常数$b$就可以用一个常量来表示。既然常量是一种Tensor,那么它也就具有Tensor的所有数据特性,它包括:
  8. * value: 符合TensorFlow中定义的数据类型的常数值或者常数列表;
  9. * dtype:数据类型;
  10. * shape:常量的形状;
  11. * name:常量的名字;
  12. ##### How to create a Constant
  13. 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.
  14. TensorFlow提供了一个很方便的函数用来创建一个Constant, 在TF.NET,可以使用同样的函数名`tf.constant`来创建,TF.NET采取尽可能使用和python binding一样的命名方式来对API命名,虽然这样会让习惯C#命名习惯的开发者感到不舒服,但我经过深思熟虑之后还是决定放弃C#的约定命名方式。
  15. Initialize a scalar constant:
  16. ```csharp
  17. var c1 = tf.constant(3); // int
  18. var c2 = tf.constant(1.0f); // float
  19. var c3 = tf.constant(2.0); // double
  20. var c4 = tf.constant("Big Tree"); // string
  21. ```
  22. Initialize a constant through ndarray:
  23. ```csharp
  24. // dtype=int, shape=(2, 3)
  25. var nd = np.array(new int[][]
  26. {
  27. new int[]{3, 1, 1},
  28. new int[]{2, 3, 1}
  29. });
  30. var tensor = tf.constant(nd);
  31. ```
  32. ##### Dive in Constant
  33. Now let's explore how constant works.
  34. 现在让我探究一下`tf.constant`是怎么工作的。
  35. ##### Other functions to create a Constant
  36. * tf.zeros
  37. * tf.zeros_like
  38. * tf.ones
  39. * tf.ones_like
  40. * tf.fill

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。