0

I have MLP weights trained in keras. The weights are list of 2D array. How do I save the weights in a json file? My goal is to send the stored weights to another server. Please let me know, if you can find anything. I have looked everywhere, but couldn't find the solution. Here is my code:

# Create first network with Keras
from keras.models import Sequential
from keras.layers import Dense
from bottle import route, run
import socket
import numpy 
import h5py
import cPickle
import httplib
import urllib2
import urllib
import codecs
import simplejson as json 

def main():
    seed = 7
    numpy.random.seed(seed)
    # load pima indians dataset
    dataset = numpy.loadtxt('pima-indians-diabetes.data', delimiter=",")
    # split into input (X) and output (Y) variables
    X = dataset[:,0:8]
    Y = dataset[:,8]
    # create model
    model = Sequential()
    model.add(Dense(20, input_dim=8, init='uniform', activation='relu'))
    model.add(Dense(9, init='uniform', activation='relu'))
    model.add(Dense(1, init='uniform', activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    # Fit the model
    model.fit(X, Y, nb_epoch=150, batch_size=20, verbose = False)
    # evaluate the model
    scores = model.evaluate(X, Y)
    # print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    weights = model.get_weights()
    with open('data.txt', 'w') as outfile:
         json.dump(weights, outfile)
main()

The error I am getting is this :

Using TensorFlow backend.
 32/768 [>.............................] - ETA: 0sTraceback (most recent call last):
  File "test.py", line 38, in <module>
    main()
  File "test.py", line 37, in main
    json.dump(weights, outfile)
  File "/usr/local/lib/python2.7/dist-packages/simplejson/__init__.py", line 276, in dump
    for chunk in iterable:
  File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 665, in _iterencode
    for chunk in _iterencode_list(o, _current_indent_level):
  File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 515, in _iterencode_list
    for chunk in chunks:
  File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 697, in _iterencode
    o = _default(o)
  File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 268, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: array([[ 0.11374838, -0.02957115, -0.23768419,  0.25968653, -0.09367259,
        ... , -0.062304  , -0.05544948]], dtype=float32) is not JSON serializable
  • Have you tried using [`json.dump`](https://docs.python.org/3.6/library/json.html#json.dump)? – mkrieger1 Feb 11 '17 at 21:29
  • Yes, but that gives me the error -- .json is not serializable – user1595115 Feb 11 '17 at 21:30
  • Please include the code you have tried in the question. – mkrieger1 Feb 11 '17 at 21:35
  • @mkrieger1 - I have updated the question and also posted the error log – user1595115 Feb 11 '17 at 21:47
  • Aha, now we are getting closer to the solution. You are trying to serialize NumPy arrays. See [this](http://stackoverflow.com/questions/26646362/numpy-array-is-not-json-serializable) question or [this one](http://stackoverflow.com/questions/3488934/simplejson-and-numpy-array) for an explanation. – mkrieger1 Feb 11 '17 at 21:52
  • Theres no need to use json for that, you can save the whole model, and use "get_weights()" once you loaded the model anywhere else... :) – Nassim Ben Feb 12 '17 at 09:15

0 Answers0