TF feeding

供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据。

def extract_data(filename, num_images):

    """Extract the images into a 4D tensor [image index, y, x, channels].
    Values are rescaled from [0, 255] down to [-0.5, 0.5].
    """
    print 'Extracting', filename
     # 把x变成一个4d向量,其第2、第3维对应图片的宽、高,
     # 最后一维代表图片的颜色通道数(因为是灰度图所以这里的通道数为1,如果是rgb彩色图,则为3
    with gzip.open(filename) as bytestream:
        bytestream.read(16)
        buf = bytestream.read(IMAGE_SIZE * IMAGE_SIZE * num_images)
        data = numpy.frombuffer(buf, dtype=numpy.uint8).astype(numpy.float32)
        data = (data - (PIXEL_DEPTH / 2.0)) / PIXEL_DEPTH
        data = data.reshape(num_images, IMAGE_SIZE, IMAGE_SIZE, 1)

        return data # get train_data...

 offset = (step * BATCH_SIZE) % (train_size - BATCH_SIZE)

batch_data = train_data[offset:(offset + BATCH_SIZE), :, :, :] #get process data for every step

#设计placeholder节点的唯一的意图就是为了提供数据供给(feeding)的方法。placeholder节点被声明的时候是未初始化的, 也不包含数据

# shape:指定了需要将tensor转换为什么结构的tensor       
train_data_node = tf.placeholder( tf.float32,     shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))

feed_dict = {train_data_node: batch_data,            train_labels_node: batch_labels}

 # Run the graph and fetch some of the nodes.

tf.Session().run(
                [optimizer, loss, learning_rate, train_prediction], #data compute
                feed_dict=feed_dict)# data feeding!

#Its value mustbe fed using the feed_dict optional argument to Session.run(),Tensor.eval(), or Operation.run().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值