{"action":"created","issue":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/12876","repository_url":"https://api.github.com/repos/tensorflow/tensorflow","labels_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/12876/labels{/name}","comments_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/12876/comments","events_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/12876/events","html_url":"https://github.com/tensorflow/tensorflow/issues/12876","id":255899683,"node_id":"MDU6SXNzdWUyNTU4OTk2ODM=","number":12876,"title":"Reading float value from tfrecord, return a not same value.","user":{"login":"TechBK","id":8995895,"node_id":"MDQ6VXNlcjg5OTU4OTU=","avatar_url":"https://avatars0.githubusercontent.com/u/8995895?v=4","gravatar_id":"","url":"https://api.github.com/users/TechBK","html_url":"https://github.com/TechBK","followers_url":"https://api.github.com/users/TechBK/followers","following_url":"https://api.github.com/users/TechBK/following{/other_user}","gists_url":"https://api.github.com/users/TechBK/gists{/gist_id}","starred_url":"https://api.github.com/users/TechBK/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TechBK/subscriptions","organizations_url":"https://api.github.com/users/TechBK/orgs","repos_url":"https://api.github.com/users/TechBK/repos","events_url":"https://api.github.com/users/TechBK/events{/privacy}","received_events_url":"https://api.github.com/users/TechBK/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2017-09-07T11:06:05Z","updated_at":"2018-11-03T19:02:16Z","closed_at":"2017-09-09T18:29:58Z","author_association":"NONE","body":"### System information\r\n- OS Platform and Distribution: Linux Ubuntu 16.04)\r\n- TensorFlow installed from: [binary](https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp36-cp36m-linux_x86_64.whl)\r\n- TensorFlow version: v1.3.0-rc2-20-g0787eee 1.3.0\r\n- Python version: Python 3.6.2 :: Continuum Analytics, Inc.\r\n\r\n### Describe the problem\r\nWhen i read feature `label` with `float` type from tfrecord, it return not same value. Example:\r\n- `3.3 -> 3.29999995`\r\n- `3.7 -> 3.70000005`\r\n- `4.9 -> 4.9000001`\r\n\r\nHere, code make `Example`:\r\n```\r\ndef make_example(s0, s1, label):\r\n    ex = tf.train.SequenceExample()\r\n    ex.context.feature[\"len_s0\"].int64_list.value.append(len(s0))\r\n    ex.context.feature[\"len_s1\"].int64_list.value.append(len(s1))\r\n    ex.context.feature[\"label\"].float_list.value.append(label)# HERE: the feature are `float` type\r\n\r\n    for w in s0:\r\n        ex.feature_lists.feature_list[\"s0\"].feature.add().int64_list.value.append(w)\r\n    for w in s1:\r\n        ex.feature_lists.feature_list[\"s1\"].feature.add().int64_list.value.append(w)\r\n\r\n    return ex\r\n```\r\nParsing function:\r\n```\r\ndef _parse_function(example_proto):\r\n    context_features = {\r\n        \"len_s0\": tf.FixedLenFeature((), dtype=tf.int64),\r\n        \"len_s1\": tf.FixedLenFeature((), dtype=tf.int64),\r\n        \"label\": tf.FixedLenFeature((), dtype=tf.float32) # HERE: the feature are `float` type\r\n    }\r\n    sequence_features = {\r\n        \"s0\": tf.FixedLenSequenceFeature((), dtype=tf.int64),\r\n        \"s1\": tf.FixedLenSequenceFeature((), dtype=tf.int64)\r\n    }\r\n\r\n    context_parsed, sequence_parsed = tf.parse_single_sequence_example(\r\n        serialized=example_proto,\r\n        context_features=context_features,\r\n        sequence_features=sequence_features\r\n    )\r\n\r\n    len_s0 = tf.cast(context_parsed['len_s0'], dtype=tf.int8)\r\n    len_s1 = tf.cast(context_parsed['len_s1'], dtype=tf.int8)\r\n    # label = tf.convert_to_tensor(context_parsed['label'], dtype=tf.float32)\r\n    # label = tf.round(context_parsed['label'])\r\n    label = context_parsed['label']\r\n    s0 = tf.cast(sequence_parsed['s0'], dtype=tf.int32)\r\n    s1 = tf.cast(sequence_parsed['s1'], dtype=tf.int32)\r\n\r\n    return {\"len_s0\": len_s0, \"s0\": s0, \"len_s1\": len_s1, \"s1\": s1}, label\r\n```\r\nWhen i call `result = tf.contrib.learn.run_n({\"label\": label}) `, it return:\r\n```\r\n{'label': array([ 3.29999995], dtype=float32)} # right value: 3.3\r\n{'label': array([ 3.70000005], dtype=float32)} # right value: 3.7\r\n{'label': array([ 4.9000001], dtype=float32)} # right value: 4.9\r\n```\r\n\r\nSome one can help me? Plzz told me some ways to fix it."},"comment":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/comments/435612770","html_url":"https://github.com/tensorflow/tensorflow/issues/12876#issuecomment-435612770","issue_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/12876","id":435612770,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNTYxMjc3MA==","user":{"login":"Bengt","id":11575,"node_id":"MDQ6VXNlcjExNTc1","avatar_url":"https://avatars0.githubusercontent.com/u/11575?v=4","gravatar_id":"","url":"https://api.github.com/users/Bengt","html_url":"https://github.com/Bengt","followers_url":"https://api.github.com/users/Bengt/followers","following_url":"https://api.github.com/users/Bengt/following{/other_user}","gists_url":"https://api.github.com/users/Bengt/gists{/gist_id}","starred_url":"https://api.github.com/users/Bengt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bengt/subscriptions","organizations_url":"https://api.github.com/users/Bengt/orgs","repos_url":"https://api.github.com/users/Bengt/repos","events_url":"https://api.github.com/users/Bengt/events{/privacy}","received_events_url":"https://api.github.com/users/Bengt/received_events","type":"User","site_admin":false},"created_at":"2018-11-03T19:02:16Z","updated_at":"2018-11-03T19:02:16Z","author_association":"NONE","body":"I just ran into this, too. I wanted to make sure my importer code is correct, so I also implemented an exporter. Surprisingly, the results were not the same as the inputs. For my use case, the loss in precision that is described in this issue actually matters. My examples are drawn from a problem with a fractal result space. So deviating in the problem space by losing some precision actually makes my examples wrong, as @quaeler noted. This means that beyond 32 bit precision, my estimator would be learning garbage. However, I found that the work around of storing the data as strings and casting them back and forth is actually common.\r\n\r\nHere is a blog post from the future (May 2019), encoding floats as bytes:\r\n\r\nhttp://jrmeyer.github.io/machinelearning/2019/05/29/tensorflow-dataset-estimator-api.html\r\n\r\nThe money bit for serialization:\r\n\r\n```python\r\ndef _float64_feature(float64_value):\r\n    float64_bytes = [str(float64_value).encode()]\r\n    bytes_list = tf.train.BytesList(value=float64_bytes)\r\n    bytes_list_feature = tf.train.Feature(bytes_list=bytes_list)\r\n\r\n    return bytes_list_feature\r\n```\r\n\r\n... and for deserialization:\r\n\r\n```python\r\ndef deserialize(serialized_example):\r\n    features = {\r\n        'float_value': tf.FixedLenFeature([], tf.string),\r\n    }\r\n    features = tf.parse_single_example(\r\n        serialized_example,\r\n        features=features\r\n    )\r\n\r\n    features['float_value'] = tf.strings.to_number(string_tensor=features['float_value'], out_type=tf.float64)\r\n\r\n    return features['float_value']\r\n```"}}
{"actor":{"display_login":"Bengt"}}