【问题标题】:Sequence elemnt in Dict wont load properlyDict 中的序列元素无法正确加载
【发布时间】:2020-01-04 17:30:32
【问题描述】:

我的深度学习模型有一个training monitor.py,我将callbacks 称为模型。它加载带有值的json file,并循环遍历该json file 的字典中的键 Json 是这样的

"{'val_loss': [5.237191623602158, 4.616091437362744, 4.451269820260772, 
4.362860869442862, 3.815740784710904, 3.8274743541668546, 3.9138536897172513, 
3.5977005262053416, 3.613263362866152, 3.6910251097732716], 'val_acc': 
[0.08173076923076923, 0.12489967897271267, 0.15399277688603533, 
0.17726725521669343, 0.24859550561797752, 0.25451444622792935, 
0.2510032102728732, 0.29825441412520065, 0.29674959871589085, 
0.28681781701444625], 'loss': [5.8396556760644165, 4.7136322415301155, 
4.393255534172906, 4.207402438980053, 4.045142109699782, 3.929618620266169, 
3.8373178752232295, 3.7613641902812325, 3.700959263334659, 3.647130805573047], 
'acc': [0.05180921052631579, 0.11022808109550063, 0.147663613729326, 
0.17937488884936867, 0.20855192957496, 0.2304263738218033, 
0.24964431797972614, 0.26676151520540636, 0.28015516628134446, 
0.29109238840476614]}"

我在 training monitor.py 中尝试了这段代码

class TrainingMonitor(BaseLogger):
    def __init__(self, figPath, jsonPath=None, startAt=0):
        # store the output path for the figure, the path to the JSON
        # serialized file, and the starting epoch
        super(TrainingMonitor, self).__init__()
        self.figPath = figPath
        self.jsonPath = jsonPath
        self.startAt = startAt

    def on_train_begin(self, logs={}):
        # initialize the history dictionary
        self.H = {}

        # if the JSON history path exists, load the training history
        if self.jsonPath is not None:
            if os.path.exists(self.jsonPath):
                self.H = json.loads(open(self.jsonPath).read())
                print(type(self.H))

                # check to see if a starting epoch was supplied
                if self.startAt > 0:
                    # loop over the entries in the history log and
                    # trim any entries that are past the starting
                    # epoch
                    for k in self.H.keys(): # HERE it throws the ERROR
                        self.H[k] = self.H[k][:self.startAt]


它在这一行抛出错误

  for k in self.H.keys():

当我初始化上面的历史字典时,它应该在dict,但它显示了

 AttributeError: 'str' object has no attribute 'keys'

所以我尝试了类似的方法

self.H = dict(json.loads(open(self.jsonPath).read()))

为了确保它在dict 但这次它显示了

ValueError: dictionary update sequence element #0 has length 1; 2 is required

所以它可能不会以这种方式工作......我做错了什么?

【问题讨论】:

  • 那是不是一个 JSON 文件。或者,更准确地说,它不是代表object的JSON文件;它表示一个字符串,它本身是 JS 或 Python 代码,但也不是 JSON。有一些方法可以解决这个问题,但我认为你应该真正研究一下该文件是如何在上游生成的,并使其成为一个实际的 JSON 对象。
  • 问题在open(self.jsonPath).read()。它没有返回您所期望的,即 JSON 文件
  • 错误清楚地说明了原因: AttributeError: 'str' object has no attribute 'keys' 这意味着您试图在str 对象上调用.keys() 方法。为什么会这样?尝试理解为什么 json.loads(...) 返回 str 而不是您期望的 Python 对象。 (一本字典)
  • 谢谢!看了网上的json文件示例我意识到了

标签: python json python-3.x string dictionary


【解决方案1】:

您在 JSON 文件中的键(属性名称)应该用双引号括起来,而这不在您的 JSON 文件中。

例如,将'val_loss'更改为"val_loss",还有许多其他单引号括起来的键也会全部更改为双引号

演示:

用双引号将所有属性名称括起来,

{"val_loss": [5.237191623602158, 4.616091437362744, 4.451269820260772, 
4.362860869442862, 3.815740784710904, 3.8274743541668546, 3.9138536897172513, 
3.5977005262053416, 3.613263362866152, 3.6910251097732716], "val_acc": 
[0.08173076923076923, 0.12489967897271267, 0.15399277688603533, 
0.17726725521669343, 0.24859550561797752, 0.25451444622792935, 
0.2510032102728732, 0.29825441412520065, 0.29674959871589085, 
0.28681781701444625], "loss": [5.8396556760644165, 4.7136322415301155, 
4.393255534172906, 4.207402438980053, 4.045142109699782, 3.929618620266169, 
3.8373178752232295, 3.7613641902812325, 3.700959263334659, 3.647130805573047], 
"acc": [0.05180921052631579, 0.11022808109550063, 0.147663613729326, 
0.17937488884936867, 0.20855192957496, 0.2304263738218033, 
0.24964431797972614, 0.26676151520540636, 0.28015516628134446, 
0.29109238840476614]}

import json
print(type(json.loads(open('text_file_with_json_data.txt').read())))

输出:

<class 'dict'>

【讨论】:

  • 谢谢!这有帮助!看了网上的json文件示例我意识到了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 2019-11-29
相关资源
最近更新 更多