tf.train.Example and tf.train.Feature

叁歲伎倆 2022-12-24 15:54 288阅读 0赞

例子:

  1. #coding:utf-8
  2. import tensorflow as tf
  3. import numpy as np
  4. import IPython.display as display
  5. def _bytes_feature(value):
  6. """Returns a bytes_list from a string / byte."""
  7. if isinstance(value, type(tf.constant(0))):
  8. value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
  9. # if isinstance(value, list):
  10. # return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
  11. return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  12. def _float_feature(value):
  13. """Returns a float_list from a float / double."""
  14. # if isinstance(value, list):
  15. # return tf.train.Feature(float_list=tf.train.FloatList(value=value))
  16. return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
  17. def _int64_feature(value):
  18. """Returns an int64_list from a bool / enum / int / uint."""
  19. # if isinstance(value, list):
  20. # return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
  21. return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  22. print(_bytes_feature(b'test_string'))
  23. print(_bytes_feature(u'test_bytes'.encode('utf-8')))
  24. print(_float_feature(np.exp(1)))
  25. print(_int64_feature(True))
  26. print(_int64_feature(1))
  27. # convert feature to searilize string
  28. print("SerializeToString:", _int64_feature(True).SerializeToString())
  29. # 随机生成feature 数据
  30. # The number of observations in the dataset.
  31. n_observations = int(1e4)
  32. # Boolean feature, encoded as False or True.
  33. feature0 = np.random.choice([False, True], n_observations)
  34. # Integer feature, random from 0 to 4.
  35. feature1 = np.random.randint(0, 5, n_observations)
  36. print("feature1:", feature1)
  37. # String feature
  38. strings = np.array([b'cat', b'dog', b'chicken', b'horse', b'goat'])
  39. feature2 = strings[feature1]
  40. # Float feature, from a standard normal distribution
  41. feature3 = np.random.randn(n_observations)
  42. def serialize_example(feature0, feature1, feature2, feature3):
  43. """
  44. Creates a tf.Example message ready to be written to a file.
  45. """
  46. # Create a dictionary mapping the feature name to the tf.Example-compatible
  47. # data type.
  48. feature = {
  49. 'feature0': _int64_feature(feature0),
  50. 'feature1': _int64_feature(feature1),
  51. 'feature2': _bytes_feature(feature2),
  52. 'feature3': _float_feature(feature3),
  53. }
  54. # Create a Features message using tf.train.Example.
  55. example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
  56. return example_proto.SerializeToString()
  57. # This is an example observation from the dataset.
  58. example_observation = []
  59. serialized_example = serialize_example(False, 4, b'goat', 0.9876)
  60. print("serialized_example:", serialized_example)
  61. # 解码消息
  62. example_proto = tf.train.Example.FromString(serialized_example)
  63. print("example_proto:", example_proto)

发表评论

表情:
评论列表 (有 0 条评论,288人围观)

还没有评论,来说两句吧...

相关阅读

    相关 AND & OR

    AND 和 OR 运算符用于基于一个以上的条件对记录进行过滤。 AND 和 OR 运算符 AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来。 如果

    相关 and or php,AND OR

    \\ AND OR 上面是基础的Where语句,下面看一下复杂一点的 你可以使用"AND" 或 "OR" 来拼接非常复杂的SQL语句 ~~~ namespace Act