Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network.to_json should handle numpy.ndarray correctly. #10754

Merged
merged 1 commit into from
Jul 23, 2018

Conversation

yanboliang
Copy link
Contributor

@yanboliang yanboliang commented Jul 23, 2018

Summary

Similar as #10727, when we serialize a Network by to_json, we should handle numpy.ndarray correctly if there is. For example:

model = Sequential()
model.add(Dense(2, input_shape=(3,), kernel_initializer=Constant(np.ones((3, 2)))))
model.add(Dense(3))
model.compile(loss='mse', optimizer='sgd', metrics=['acc'])
model.to_json()

It throws the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "keras/engine/network.py", line 1205, in to_json
    return json.dumps(model_config, default=get_json_type, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "keras/engine/network.py", line 1196, in get_json_type
    return obj.item()
ValueError: can only convert an array of size 1 to a Python scalar

Note: tf.keras should have the same issue, will send a fix if this get approved.

Related Issues

PR Overview

  • This PR requires new unit tests [y/n] (make sure tests are included)
  • This PR requires to update the documentation [y/n] (make sure the docs are up-to-date)
  • This PR is backwards compatible [y/n]
  • This PR changes the current API [y/n] (all API changes need to be approved by fchollet)

Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

I believe tf.keras would have the same issue. Please consider creating a PR in the TensorFlow repository as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants