1、feed_dict传数据
上代码
学编程,最好从代码中学习,这样才可以学以致用
先说明一下下面代码要说明什么问题:
①feed_dict并不是只能改变用占位符设置的变量(不了解占位符,可以看我之前的文章),可以给任意计算图中的量传值,即使申明为tf.constant的变量也可以传值。
②第三个输出是11*6,更加说明每次run的时候都是走计算图的过程
import tensorflow as tf
def model(a,b):
c = tf.add(a,b)
return c
def fun1(c):
d = tf.multiply(c,6)
return d
def main(_):
a = tf.constant(3)
b = 8
c = model(a,b)
d = fun1(c)
with tf.Session() as sess:
#print("a")
print(sess.run(c)) #3+8
print(sess.run(c,feed_dict={a:5})) #5+8
print(sess.run(d)) #11*6,表明上面一行代码,只是运行c计算过程,并不改变c的值
print(sess.run(d,feed_dict={c:8})) #8*6
#print(sess.run(c,feed_dict={a:4,b:9}))
#print("b")
11
13
66
48
2、cache()使用误区
(1)出错代码
我使用的是tensorflow1.10.0
dataset = dataset.cache().batch(5).repeat()
iterator = dataset.make_one_shot_iterator()
images, integer_labels = iterator.get_next()
在使用上面的代码时,出现如下错误
The calling iterator did not fully read the dataset we were attempting to cache. In order to avoid unexpected truncation of the sequence, the current [partially cached] sequence will be dropped. This can occur if you have a sequence similar to `dataset.cache().take(k).repeat()`. Instead, swap the order (i.e. `dataset.take(k).cache().repeat()`)
导致每次使用images.eval()和integer_labels.eval()时,images和integer_labels的值都会发生变化,而且取出来的images和integer_labels并不对应。导致模型测试的准确率为0或0.2
(2)解决方法
将代码改成如下
dataset = dataset.take(2).cache().batch(5).repeat()
iterator = dataset.make_one_shot_iterator()
images, integer_labels = iterator.get_next()