tensorflow学习中的问题(二)

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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值