question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keras load_from_json JsonError

See original GitHub issue

I’m loading a net architecture from a json, here is the json: ` { “backend”:“tensorflow”, “keras_version”:“2.0.3”, “config”:[ { “config”:{ “batch_input_shape”:[ null, 6, 6, 9 ], “kernel_constraint”:null, “name”:“dense_1”, “dtype”:“float32”, “units”:24, “bias_constraint”:null, “kernel_initializer”:{ “config”:{ “scale”:1.0, “distribution”:“uniform”, “mode”:“fan_avg”, “seed”:null }, “class_name”:“VarianceScaling” }, “use_bias”:true, “kernel_regularizer”:null, “bias_regularizer”:null, “activation”:“relu”, “trainable”:true, “bias_initializer”:{ “config”:{

           },
           "class_name":"Zeros"
        },
        "activity_regularizer":null
     },
     "class_name":"Dense"
  },
  {
     "config":{
        "rate":0.25,
        "name":"dropout_1",
        "trainable":true
     },
     "class_name":"Dropout"
  },
  {
     "config":{
        "kernel_constraint":null,
        "name":"dense_2",
        "units":24,
        "bias_constraint":null,
        "kernel_initializer":{
           "config":{
              "scale":1.0,
              "distribution":"uniform",
              "mode":"fan_avg",
              "seed":null
           },
           "class_name":"VarianceScaling"
        },
        "use_bias":true,
        "kernel_regularizer":null,
        "bias_regularizer":null,
        "activation":"relu",
        "trainable":true,
        "bias_initializer":{
           "config":{

           },
           "class_name":"Zeros"
        },
        "activity_regularizer":null
     },
     "class_name":"Dense"
  },
  {
     "config":{
        "rate":0.5,
        "name":"dropout_2",
        "trainable":true
     },
     "class_name":"Dropout"
  },
  {
     "config":{
        "name":"flatten_1",
        "trainable":true
     },
     "class_name":"Flatten"
  },
  {
     "config":{
        "kernel_constraint":null,
        "name":"dense_3",
        "units":1,
        "bias_constraint":null,
        "kernel_initializer":{
           "config":{
              "scale":1.0,
              "distribution":"uniform",
              "mode":"fan_avg",
              "seed":null
           },
           "class_name":"VarianceScaling"
        },
        "use_bias":true,
        "kernel_regularizer":null,
        "bias_regularizer":null,
        "activation":"sigmoid",
        "trainable":true,
        "bias_initializer":{
           "config":{

           },
           "class_name":"Zeros"
        },
        "activity_regularizer":null
     },
     "class_name":"Dense"
  }

], “class_name”:“Sequential” } `

and this is the error: Traceback (most recent call last): File “D:\workspace2\NN\src\ModelLoader.py”, line 19, in <module> model2 = model_from_json(‘ModelNoComp2Dense24Nueuroni.json’) File “C:\Users\emanuele\python3.5\lib\site-packages\keras-2.0.3-py3.5.egg\keras\models.py”, line 332, in model_from_json config = json.loads(json_string.encode().decode()) File “C:\Users\emanuele\python3.5\lib\json_init_.py”, line 319, in loads return _default_decoder.decode(s) File “C:\Users\emanuele\python3.5\lib\json\decoder.py”, line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File “C:\Users\emanuele\python3.5\lib\json\decoder.py”, line 357, in raw_decode raise JSONDecodeError(“Expecting value”, s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

i’m loading the model with:

model2 = model_from_json(‘ModelNoComp2Dense24Nueuroni.json’)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

13reactions
WVmfcommented, Mar 5, 2019

For anyone who ends up here due to the json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) issue (as I did): The model_from_json() method expects a JSON string as its first parameter, not the name of a JSON file. So the following worked for me:

import json
from keras.models import model_from_json

with open('model_architecture.json', 'r') as json_file:
    model = model_from_json(json_file.read())
9reactions
harpalsahotacommented, Nov 1, 2018

I had the same issue and came up with the following workaround:

import json

from keras.models import model_from_json

with open(model_architecture, 'r') as json_file:
    architecture = json.load(json_file)
    model = model_from_json(architecture)
Read more comments on GitHub >

github_iconTop Results From Across the Web

can not load_model() or load_from_json() if my model ...
this is my code: `from keras import backend as K from keras.engine.topology import Layer from keras.models import load_model
Read more >
tf.keras.models.model_from_json | TensorFlow v2.11.0
Parses a JSON model configuration string and returns a model instance.
Read more >
Keras won't load json file - Stack Overflow
I ran my code and saved saved my model using the following code: model_json = model.to_json() with open(inFilePath+".json", ...
Read more >
"import keras.models" error json - Google Groups
json file in .keras folder. It was after that, the "json error" occurred when I tried to import keras in python. I guess...
Read more >
[Example code]-how to use pip in python - appsloveworld.com
I'm trying to use python-telegram-bot. (The version of python is 3) I installed it using pip install python-telegram-bot. I run a file with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found