tensorFlow五:feed与fetch,及一个完整示例

一、feed与fetch

这个点位符,类似于定义函数时的形参;在执行函数的时侯,才传递实参。

示例:

import tensorflow as tf

# 构建阶段
m1 = tf.placeholder(dtype=tf.float32, shape=[2, 3], name="placeholder_m1")
m2 = tf.placeholder(dtype=tf.float32, shape=[3, 2], name="placeholder_m2")
m3 = tf.matmul(m1, m2)
# 执行阶段
with tf.Session() as sess:
    # feed_dict字典的key,必须是张量对象
    print("result:\n{}".format(sess.run(fetches=m3,
                                        feed_dict={m1: [[1, 2, 3], [4, 5, 7]],
                                                   m2: [[10, 11], [12, ], [13, 14]]})))
    print("result:\n{}".format(m3.eval(feed_dict={m1: [[11, 21, 31], [4, 5, 7]],
                                                   m2: [[10, 11], [12, ], [13, 14]]})))

 

二、一个完整示例

import tensorflow as tf
import os

# 屏蔽info
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'

# python代码,实现一个累加器:
i = 0
for _ in range(10):
    i += 1
    print(i)

# tensorflow,实现一个累加器
i = tf.Variable(initial_value=0, dtype=tf.int32, name="i_var")

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    for _ in range(10):
        # i = i + 1.0
        i = tf.add(i, 1.0)
        result = sess.run(i)
        print("result:{}".format(result))

 

tensorflow中,一般不使用以上方式去累加或更新一个张量
规范的做法是使用tf.assign()或tf.assign_add去更新
import tensorflow as tf
import os

# 屏蔽info
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'

# tensorflow,实现一个累加器
i = tf.Variable(initial_value=0, dtype=tf.int32, name="i_var")
assign_op = tf.assign(ref=i, value=tf.add(i, tf.constant(1.0), name="assion_op"))
# assign_op = tf.assign_add(ref=i, value=tf.constant(1.0), name="assign_op2")

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    for _ in range(10):
        # 在tensorflow中,一般不使用以上方式去累加或更新一个张量
        # 规范的做法是使用tf.assign()或tf.assign_add去更新
        result = sess.run(assign_op)
        print("result:{}".format(result))

import tensorflow as tf
import os
# 屏蔽info
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'

# 更新变量的维度数目:tensor,张量
# 如: 2*3, 3*2, 1*6,数据的维度一直在变,但数据还是不变

# 参数validate_shape为False时,变量的shape可以是未知的
a = tf.Variable(initial_value=[], dtype=tf.float32, validate_shape=False, name="a_var")

# 更改维度concat()方法, 参数axis=0|1根据行或列连接
b = tf.concat(a, [1.0, 2.0], axis=0)

assign_op = tf.assign(ref=a, value=b, validate_shape=False)

init_op = tf.global_variables_initializer()

config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
with tf.Session(config=config) as sess:
    sess.run(init_op)
    for _ in range(5):
        result = sess.run(assign_op)
        print("result: {}".format(sess.run(a)))
# 阶乘
a = input("pls input a number: ")
def jie(n):
    if n == 1:
        return 1
    return n * jie(n -1)
print(jie(int(a)))
result = 1
for i in range(1, int(a) + 1):
    result *= i
print(result)

 

import tensorflow as tf
import os
# 屏蔽info
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'

num = input("pls input a number: ")
aa = tf.Variable(dtype=tf.int32)
result = tf.Variable(initial_value=1, dtype=tf.int32)
init_op = tf.global_variables_initializer()
assign_op = tf.assign(ref=result, value=result*aa)
with tf.Session() as sess:
    sess.run(init_op)
    for i in range(1, int(num)):
        result = sess.run(assign_op, feed_dict={aa: i})
        print("result: {}".format(sess.run(aa)))

 

posted on 2019-02-01 08:07  myworldworld  阅读(275)  评论(0)    收藏  举报

导航