Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96144
License: OTHER
1
""" Placeholder and feed_dict example
2
Created by Chip Huyen ([email protected])
3
CS20: "TensorFlow for Deep Learning Research"
4
cs20.stanford.edu
5
Lecture 02
6
"""
7
import os
8
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
9
10
import tensorflow as tf
11
12
# Example 1: feed_dict with placeholder
13
14
# a is a placeholderfor a vector of 3 elements, type tf.float32
15
a = tf.placeholder(tf.float32, shape=[3])
16
b = tf.constant([5, 5, 5], tf.float32)
17
18
# use the placeholder as you would a constant
19
c = a + b # short for tf.add(a, b)
20
21
writer = tf.summary.FileWriter('graphs/placeholders', tf.get_default_graph())
22
with tf.Session() as sess:
23
# compute the value of c given the value of a is [1, 2, 3]
24
print(sess.run(c, {a: [1, 2, 3]})) # [6. 7. 8.]
25
writer.close()
26
27
28
# Example 2: feed_dict with variables
29
a = tf.add(2, 5)
30
b = tf.multiply(a, 3)
31
32
with tf.Session() as sess:
33
print(sess.run(b)) # >> 21
34
# compute the value of b given the value of a is 15
35
print(sess.run(b, feed_dict={a: 15})) # >> 45
36