tensorflow 的 sess.partial_run

这篇博客详细记录了TensorFlow中sess.partial_run的使用,包括三种不同形式的实验,展示了即使未在fetches中指定,只要feed_dict提供了placeholder值,相关操作也会执行。作者提醒在使用partial_run时需要注意其可能产生的不同运行结果,因为它的设计初衷是用于分步执行依赖于前一步结果的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小记

tensorflow的sess有一个分步执行的函数partial_run,此函数的文档说明和具体运行有点差异,留文以记录。

形式1

import tensorflow as tf

a = tf.placeholder(dtype=tf.float32, shape=[])
b = tf.placeholder(dtype=tf.float32, shape=[])
c = tf.placeholder(dtype=tf.float32, shape=[])
e = tf.placeholder(dtype=tf.float32, shape=[])

d = tf.Variable(dtype=tf.float32, initial_value=0)
r1 = tf.add(a, b)
a_0 = tf.assign(d, tf.add(d,c),name='0')
a_1 = tf.assign(d, tf.add(d,e),name='1')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    h = sess.partial_run_setup([r1, a_0, a_1], [a, b, c, e])
    res_1 = sess.partial_run(h, r1, feed_dict={a: 1, b: 2})
    res_2 = sess.partial_run(h, a_0, feed_dict={c: res_1})
    print(d.eval())

此时,输出 3.0

形式2

import tensorflow as tf

a = tf.placeholder(dtype=tf.float32, shape=[])
b = tf.placeholder(dtype=tf.float32, shape=[])
c = tf.placeholder(dtype=tf.float32, shape=[])
e = tf.placeholder(dtype=tf.float32, shape=[])

d = tf.Variable(dtype=tf.float32, initial_value=0)
r1 = tf.add(a, b)
a_0 = tf.assign(d, tf.add(d,c),name='0')
a_1 = tf.assign(d, tf.add(d,e),name='1')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    h = sess.partial_run_setup([r1, a_0, a_1], [a, b, c, e])
    res_1 = sess.partial_run(h, r1, feed_dict={a: 1, b: 2})
    res_2 = sess.partial_run(h, a_0, feed_dict={c: res_1})
    print(d.eval())

此时输出 6.0

形式3

import tensorflow as tf

a = tf.placeholder(dtype=tf.float32, shape=[])
b = tf.placeholder(dtype=tf.float32, shape=[])
c = tf.placeholder(dtype=tf.float32, shape=[])
e = tf.placeholder(dtype=tf.float32, shape=[])

d = tf.Variable(dtype=tf.float32, initial_value=0)
r1 = tf.add(a, b)
a_0 = tf.assign(d, tf.add(d,c),name='0')
a_1 = tf.assign(d, tf.add(d,e),name='1')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    h = sess.partial_run_setup([r1, a_0, a_1], [a, b, c, e])
    res_1 = sess.partial_run(h, r1, feed_dict={a: 1, b: 2})
    res_2 = sess.partial_run(h, a_0, feed_dict={c: res_1, e: res_1})
    print(d.eval())

此时,依然输出 6.0

总结

也就是说无论fetches是否写上,只要feed_dict中给与了相应的placeholder值,fetches中的op就会运行。

更多的现象

import tensorflow as tf

c = tf.placeholder(dtype=tf.float32, shape=[2,2])
e = tf.placeholder(dtype=tf.float32, shape=[2,2])

d = tf.Variable(dtype=tf.float32, initial_value=[[0,1],[2,3]])
a_0 = tf.assign(d, tf.matmul(d,c),name='0')
a_1 = tf.assign(d, tf.matmul(d,e),name='1')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    h = sess.partial_run_setup([a_0, a_1], [c, e])
    res_2 = sess.partial_run(h, [a_1,a_0], feed_dict={c: [[1,0],[0,0]], e: [[0,1],[1,0]]})
    print(d.eval())

运行多次,会出现不同的结果。诚然partial运行旨在运行分步的代码,也就是下一步的参数需要是上一步代码的结果,此处不是这种类型,但是在使用时还是要小心。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值