ML - tf.cifar10_input.py

原创
2017/05/08 11:25
阅读数 37

Some places to notice:

In method read_cifar10(filename_queue);

1. tf.train.string_input_producer(filenames);

    Explain: where 'filenames' is the <list> contains all the names of the binary data batch files.

    Output: a filename queue which can work with <tf.FixedLengthRecordReader>.

--- --- --- --- --- ---

2. tf.FixedLengthRecordReader(record_bytes=record_bytes);

    Explain: where 'record_bytes' is the number of bytes of one single record.

    Output: a <reader> object which can invoke <reader>.read(<filename_queue>).

    Appendix: <reader>.read(<filename_queue>); --output of the method is a <[key, value]> record, where 'key' is a <string> represents the record, 'value' is the content of the record read from <filename_queue> in <string> form. Before 'value' being used, it needs to be converted into <vector of uint8> by 'tf.decode_raw(value, tf.uint8)'. 

--- --- --- --- --- ---

3. tf.slice(record_bytes, [0], [label_bytes]);

    Explain: where 'record_bytes' is now having been transformed to tf.uint8 by 'tf.decode_raw(value, <format>)'. It's a 1-dimensional vector with :

    1) 'record_bytes[0]' as the label of this record which indicates which class it belongs to.

    2)  'record_bytes[1:image_bytes+1]' as the content of the image becomes flattened into a vector.

    parameter '[0]' decides where the slice begins, it's a <list> instead of a number because sometimes the slice needs to operate on multiple-dimensional array. for example the starting point is at 2nd row&4th column, the parameter is [1,3].

    parameter '[label_bytes]' decides the length of the content from the starting point needs to be sliced. '[label_bytes]' is also a <list> instead of a number because the slice piece can also be a multiple-dimensional content. for example,

    #input = [                [

                        [1,1,1],

                        [2,2,2]

                    ],

                    [

                        [3,3,3],

                        [4,4,4]

                    ],

                    [

                        [5,5,5],

                        [6,6,6]

                    ] ]

    #tf.slice(input, [1,0,0], [1,2,3]);  ==> output: [ [[3,3,3],[4,4,4]] ]

    #tf.slice(input, [1,0,0], [2,1,3]);  ==> output: [ [[3,3,3]] , [[5,5,5]] ]

--- --- --- --- --- ---

4. tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32)

    Explain: convert the output of 'tf.slice()' to <tf.int32>

    Output: the converted output of 'tf.slice()'

--- --- --- --- --- ---

5. tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]), [result.depth, result.height, result.width])

    Explain: tf.reshape(<Tensor> tensor, <list> shape);

    Output: input parameter 'tensor' changed to shape of input parameter 'shape'.

--- --- --- --- --- ---

6. tf.transpose(depth_major, [1,2,0]);

    Explain: switch the dimension of the input tensor 'depth_major' from original order to the new order specified in the second input parameter.

    Output: for example,

    #depth_major = [ [[1,2,3],[4,5,6]],

                                 [[7,8,9],[10,11,12]],

                                 [13,14,15],[16,17,18]] 

    #tf.transpose(depth_major,[1,2,0]);

    First, since the shape of 'depth_major' is [3,2,3], by input parameter '[1,2,0]', the output's shape is [2,3,3]. Then we reconstruct the array to a new one of shape [2,3,3],

    [ [[1,7,13],[2,8,14],[3,9,15]],

      [[4,10,16],[5,11,17],[6,12,18]] ]

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部