【问题标题】:Error when using JSON derived data in Tensorflow在 TensorFlow 中使用 JSON 派生数据时出错
【发布时间】:2017-11-18 01:04:08
【问题描述】:

我正在使用以下代码,源自文档:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import json
from pprint import pprint

with open('/root/ml/2017110508.training.json') as text:
    data = json.load(text)
    features = np.array(data['input']['values'])
    labels = np.array(data['output']['values'])
    pprint(features.shape)
    pprint(labels.shape)
    pprint(features[0:3])
    pprint(labels[0:3])

# Assume that each row of `features` corresponds to the same row as `labels`.
assert features.shape[0] == labels.shape[0]

dataset = tf.data.Dataset.from_tensor_slices((features, labels))

data['input']['values'] 和 data['output']['values'] 中的数据只是浮点数行,但我得到:

TypeError: 预期的二进制或 unicode 字符串,得到 [0.6, 0.0, 0.6, 0.0, 0.0、0.0、0.0、0.3、0.6、1.5、0.0、0.4、7.7、-8.5、158.0、6.2、55.3、203.4、205.7、156.5、-8.5、7.3、-8.8、53.5、-0.9、-31.2、15.3 , -1.9, -87.6, 21.3, -21.6, -34.7, -17.1, -85.0, 28.6, -19.1]

from_tensor_slices需要什么格式?

谢谢。

pprint 调用的输出:

(58502,)

(58502, 5)

array([ list([0.6, 0.0, 0.6, 0.0, 0.0, 0.0, 0.0, 0.3, 0.6, 1.5, 0.0, 0.4、7.7、-8.5、158.0、6.2、55.3、203.4、205.7、156.5、-8.5、7.3、-8.8、53.5、-0.9、-31.2、15.3、-1.9、-87.6、21.3、-21.6、-34.7 , -17.1, -85.0, 28.6, -19.1]), list([1.3, 0.0, 1.2, 0.0, 0.0, 0.0, 0.0, 0.6, 1.0, 2.3, 0.0, 0.6, 7.7, -8.5, 158.0, 6.2, 55.3, 203.4, 205.7, 156.4, -8.5, 7.5, - 8.8、53.4、-0.9、-31.2、15.3、-1.9、-87.6、21.3、-21.6、-34.7、-17.0、-85.0、28.6、-19.1]), list([2.0, 0.0, 1.6, 0.0, 0.0, 0.0, 0.2, 0.8, 1.1, 2.9, 0.0, 0.9, 8.0, -8.5, 158.2, 6.2, 55.3, 203.4, 205.7, 156.3, -8.5, 8.0, - 8.8、53.3、-0.9、-31.2、15.1、-1.9、-87.6、21.3、-21.6、-34.8、-16.8、-84.9、28.6、-19.1])]、dtype=object)

数组([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])

【问题讨论】:

  • 你能把features.shapelabels.shape的输出加上每个的前几行吗?
  • 嗨斯蒂芬,我更新了上面的帖子,回答了你的问题。

标签: json tensorflow


【解决方案1】:

看起来问题实际上出在您的 JSON 文件中。 labels 变量的输出很好; features 变量的输出应该具有相同的结构,但其中有很多次 list。如果您的 JSON 文件有“列表”作为属性多次显示,您应该删除它们以及它们的圆括号(可能还有一些额外的方括号)。如果您自己生成 JSON,那么在那里进行更改可能更容易。

一个不相关的问题是你的标签都是 0;如果这不是您所期望的,那么您可能在某处丢失了一些数据。

【讨论】:

    猜你喜欢
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多