- TensorFlow Machine Learning Projects
- Ankit Jain Armando Fandango Amita Kapoor
- 278字
- 2021-06-10 19:15:27
Placeholders
While constants store the value at the time of defining the tensor, placeholders allow you to create empty tensors so that the values can be provided at runtime. The TensorFlow library provides the tf.placeholder() function with the following signature to create placeholders:
tf.placeholder(
dtype,
shape=None,
name=None
)
As an example, let's create two placeholders and print them:
p1 = tf.placeholder(tf.float32)
p2 = tf.placeholder(tf.float32)
print('p1 : ', p1)
print('p2 : ', p2)
The following output shows that each placeholder has been created as a tensor:
p1 : Tensor("Placeholder:0", dtype=float32)
p2 : Tensor("Placeholder_1:0", dtype=float32)
Let's define an operation using these placeholders:
mult_op = p1 * p2
In TensorFlow, shorthand symbols can be used for various operations. In the preceding code, p1 * p2 is shorthand for tf.multiply(p1,p2):
print('run(mult_op,{p1:13.4, p2:61.7}) : ',tfs.run(mult_op,{p1:13.4, p2:61.7}))
The preceding command runs mult_op in the TensorFlow session and feeds the values dictionary (the second argument to the run() operation) with the values for p1 and p2.
The output is as follows:
run(mult_op,{p1:13.4, p2:61.7}) : 826.77997
We can also specify the values dictionary by using the feed_dict parameter in the run() operation:
feed_dict={p1: 15.4, p2: 19.5}
print('run(mult_op,feed_dict = {p1:15.4, p2:19.5}) : ',
tfs.run(mult_op, feed_dict=feed_dict))
The output is as follows:
run(mult_op,feed_dict = {p1:15.4, p2:19.5}) : 300.3
Let's look at one final example, which is of a vector being fed to the same operation:
feed_dict={p1: [2.0, 3.0, 4.0], p2: [3.0, 4.0, 5.0]}
print('run(mult_op,feed_dict={p1:[2.0,3.0,4.0], p2:[3.0,4.0,5.0]}):',
tfs.run(mult_op, feed_dict=feed_dict))
The output is as follows:
run(mult_op,feed_dict={p1:[2.0,3.0,4.0],p2:[3.0,4.0,5.0]}):[ 6. 12. 20.]
The elements of the two input vectors are multiplied in an element-wise fashion.
- AutoCAD快速入門與工程制圖
- 機器學(xué)習(xí)及應(yīng)用(在線實驗+在線自測)
- Seven NoSQL Databases in a Week
- R Machine Learning By Example
- VB語言程序設(shè)計
- 21天學(xué)通Visual Basic
- Pig Design Patterns
- AWS Certified SysOps Administrator:Associate Guide
- 現(xiàn)代傳感技術(shù)
- Blender Compositing and Post Processing
- 基于32位ColdFire構(gòu)建嵌入式系統(tǒng)
- 嵌入式操作系統(tǒng)
- 悟透AutoCAD 2009完全自學(xué)手冊
- 邊緣智能:關(guān)鍵技術(shù)與落地實踐
- 精通LabVIEW程序設(shè)計