ExampleProto是否使用行主要或列主要格式?

时间:2016-06-16 18:26:31

标签: tensorflow

Example proto将多维数组表示为平面列表。

这些列表是C(行主要顺序)还是Fortran(列主要顺序)?

1 个答案:

答案 0 :(得分:0)

多维数组应按行主顺序展平。这应该是numpy中的默认值。

以下是一个说明此

的示例
from tensorflow.core.example import example_pb2
from google.protobuf import json_format
import tensorflow as tf
import numpy as np

# Create a 2-d matrix
m = np.arange(4).reshape([2,2])
print "row 1: {0}".format(m[0,:])
print "row 2: {0}".format(m[1,:])

# Construct the Example proto
e = example_pb2.Example()
e.features.feature['data'].int64_list.value.extend(m.flatten())

print "Json format"
print json_format.MessageToJson(e)

p = tf.parse_single_example(e.SerializeToString(), {'data': tf.FixedLenFeature([2,2], dtype=tf.int64)})
sess = tf.Session()
sess.run(tf.initialize_all_variables())

w = sess.run(p)

print "Parsed data"
print "row 1: {0}".format(w["data"][0,:])
print "row 2: {0}".format(w["data"][1,:])

产生输出

row 1: [0 1]
row 2: [2 3]
Json format
{
  "features": {
    "feature": {
      "data": {
        "int64List": {
          "value": [
            "0", 
            "1", 
            "2", 
            "3"
          ]
        }
      }
    }
  }
}
Parsed data
row 1: [0 1]
row 2: [2 3]