- Mastering TensorFlow 1.x
- Armando Fandango
- 216字
- 2021-06-25 22:50:54
Constants
The constant valued tensors are created using the tf.constant() function that has the following signature:
tf.constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
Let's look at the example code provided in the Jupyter Notebook with this book:
c1=tf.constant(5,name='x')
c2=tf.constant(6.0,name='y')
c3=tf.constant(7.0,tf.float32,name='z')
Let's look into the code in detail:
- The first line defines a constant tensor c1, gives it value 5, and names it x.
- The second line defines a constant tensor c2, stores value 6.0, and names it y.
- When we print these tensors, we see that the data types of c1 and c2 are automatically deduced by TensorFlow.
- To specifically define a data type, we can use the dtype parameter or place the data type as the second argument. In the preceding code example, we define the data type as tf.float32 for c3.
Let's print the constants c1, c2, and c3:
print('c1 (x): ',c1)
print('c2 (y): ',c2)
print('c3 (z): ',c3)
When we print these constants, we get the following output:
c1 (x): Tensor("x:0", shape=(), dtype=int32)
c2 (y): Tensor("y:0", shape=(), dtype=float32)
c3 (z): Tensor("z:0", shape=(), dtype=float32)
In order to print the values of these constants, we have to execute them in a TensorFlow session with the tfs.run() command:
print('run([c1,c2,c3]) : ',tfs.run([c1,c2,c3]))
We see the following output:
run([c1,c2,c3]) : [5, 6.0, 7.0]
推薦閱讀
- Python GUI Programming:A Complete Reference Guide
- 計算機組裝與系統(tǒng)配置
- 深入淺出SSD:固態(tài)存儲核心技術(shù)、原理與實戰(zhàn)(第2版)
- scikit-learn:Machine Learning Simplified
- 計算機維修與維護技術(shù)速成
- 分布式系統(tǒng)與一致性
- 計算機組裝維修與外設(shè)配置(高等職業(yè)院校教改示范教材·計算機系列)
- 單片機開發(fā)與典型工程項目實例詳解
- 基于PROTEUS的電路設(shè)計、仿真與制板
- LPC1100系列處理器原理及應(yīng)用
- FreeSWITCH Cookbook
- Intel FPGA權(quán)威設(shè)計指南:基于Quartus Prime Pro 19集成開發(fā)環(huán)境
- 單片機項目設(shè)計教程
- USB應(yīng)用開發(fā)寶典
- Corona SDK Mobile Game Development:Beginner's Guide