tf.train.slice_input_producer变量初始化问题

slice_input_producer(tensor_list, num_epochs=None, shuffle=True, seed=None,
                         capacity=32, shared_name=None, name=None)

对于num_epochs,如果设置为默认None的话,则在初始化的时候使用:

sess.run(tf.global_variables_initializer())

当num_epochs设置为其它常数时,则初始化时需要使用:

sess.run(tf.local_variables_initializer())

如果有全局变量的话,其实两者都需要加上,即:

 sess.run(tf.global_variables_initializer())
 sess.run(tf.local_variables_initializer())

说明:num_epochs为其它数值时表示队列里面原始数据可以供遍历的次数,为None时表示无限次

import tensorflow as tf

images = ['img1', 'img2', 'img3', 'img4', 'img5']
labels = [1, 2, 3, 4, 5]

epoch_num = 5

f = tf.train.slice_input_producer([images, labels], num_epochs=1, shuffle=False)

with tf.Session() as sess:
    sess.run(tf.local_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    for i in range(epoch_num):
        k = sess.run(f)
        print('************************')
        print(i, k)

    coord.request_stop()
    coord.join(threads)

对于该示例,由于num_epochs=1,即限制遍历一次,因此epoch_num就必须是小于6

如果超过则会报以下错误:

OutOfRangeError (see above for traceback): FIFOQueue '_0_input_producer/input_producer' is closed and has insufficient elements (requested 1, current size 0)
     [[Node: input_producer/input_producer_Dequeue = QueueDequeueV2[component_types=[DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](input_producer/input_producer)]]
 

另外batch_size即每次拿出多少样本进行训练也是直接影响循环次数,例:

# -*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np

# 样本个数
sample_num = 5
# 设置迭代次数
epoch_num = 2
# 设置一个批次中包含样本个数
batch_size = 3
# 计算每一轮epoch中含有的batch个数
batch_total = int(sample_num / batch_size) + 1


# 生成4个数据和标签
def generate_data(sample_num=sample_num):
    labels = np.asarray(range(0, sample_num))
    images = np.random.random([sample_num, 224, 224, 3])
    print('image size {},label size :{}'.format(images.shape, labels.shape))
    return images, labels


def get_batch_data(batch_size=batch_size):
    images, label = generate_data()
    # 数据类型转换为tf.float32
    images = tf.cast(images, tf.float32)
    label = tf.cast(label, tf.int32)
    # 从tensor列表中按顺序或随机抽取一个tensor准备放入文件名称队列
    input_queue = tf.train.slice_input_producer([images, label], num_epochs=epoch_num, shuffle=False)

    # 从文件名称队列中读取文件准备放入文件队列
    image_batch, label_batch = tf.train.batch(input_queue, batch_size=batch_size, num_threads=2, capacity=64,
                                              allow_smaller_final_batch=False)
    return image_batch, label_batch


image_batch, label_batch = get_batch_data(batch_size=batch_size)

with tf.Session() as sess:
    # 先执行初始化工作
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())

    # 开启一个协调器
    coord = tf.train.Coordinator()
    # 使用start_queue_runners 启动队列填充
    threads = tf.train.start_queue_runners(sess, coord)
    i = 0
    try:
        while not coord.should_stop():
            print('************')
            # 获取每一个batch中batch_size个样本和标签
            image_batch_v, label_batch_v = sess.run([image_batch, label_batch])
            # print(image_batch_v.shape, label_batch_v)
            i+=1
            print(i)
    except tf.errors.OutOfRangeError:  # 如果读取到文件队列末尾会抛出此异常
        print("done! now lets kill all the threads……")
    finally:
        # 协调器coord发出所有线程终止信号
        coord.request_stop()
        print('all threads are asked to stop!')
    coord.join(threads)  # 把开启的线程加入主线程,等待threads结束
    print('all threads are stopped!')

输出:

************
1
************
2
************
3
************
done! now lets kill all the threads……
all threads are asked to stop!
all threads are stopped!

该样例一共有5个样本,放入队列迭代两次,因此队列有10个样本,每次用于训练的样本是3个,因此队列中10个样本最多只能训练3次就会停止

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LoveWeeknd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值