tensorflow: TypeError: 'Not JSON Serializable' while doing tf.keras.Model.save and using keras variable in loss_weights in tf.keras.Model.compile

System information

  • Have I written custom code: NA
  • OS Platform and Distribution: Ubuntu 16.04 LTS
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: NA
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): 1.12.0
  • Python version: 3.5.2
  • Bazel version (if compiling from source): NA
  • GCC/Compiler version (if compiling from source): NA
  • CUDA/cuDNN version: release 9.0, V9.0.176
  • GPU model and memory: Tesla K80, 12 GB

Describe the current behavior When I try to save my model using model.save() where model is a tf.keras.Model instance, it throws a TypeError: (‘Not JSON Serializable:’, <tf.Variable ‘Variable:0’ shape=() dtype=float32>) . I am using a tf.keras.backend.variable() in loss_weights in model.compile. Optimizer: tf.keras.optimizers.Adam Interestingly, when I try to save my model weights only using model.save_weights where model is a tf.keras.Model instance, it works fine, NO ERROR.

Describe the expected behavior It should not throw any type of error during training.

Code to reproduce the issue import tensorflow as tf import numpy as np import os import sys

input_layer = tf.keras.layers.Input(shape=([4,3]), batch_size=1) layer1 = tf.keras.layers.Dense(20)(input_layer) layer2 = tf.keras.layers.Dense(1,name="output1")(layer1) layer3 = tf.keras.layers.Dense(1,name="output2")(layer1) model = tf.keras.Model(inputs=input_layer, outputs=[layer2,layer3]) alpha = tf.keras.backend.variable(0.25) model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001),loss={"output1":tf.keras.metrics.binary_crossentropy,"output2":tf.keras.metrics.binary_crossentropy},loss_weights=[1.0,alpha])

# model.fit() is skipped just to get straight to error. model.save("./weights.h5")

Just execute this code as it is, It will throw same error!!

Other info / logs Traceback (most recent call last): File “main_latest.py”, line 45, in <module> max_queue_size=10) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py”, line 2177, in fit_generator initial_epoch=initial_epoch) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/training_generator.py”, line 216, in fit_generator callbacks.on_epoch_end(epoch, epoch_logs) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/callbacks.py”, line 214, in on_epoch_end callback.on_epoch_end(epoch, logs) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/callbacks.py”, line 601, in on_epoch_end self.model.save(filepath, overwrite=True) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/network.py”, line 1363, in save save_model(self, filepath, overwrite, include_optimizer) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/saving.py”, line 134, in save_model default=serialization.get_json_type).encode(‘utf8’) File “/usr/lib/python3.5/json/init.py”, line 237, in dumps **kw).encode(obj) File “/usr/lib/python3.5/json/encoder.py”, line 198, in encode chunks = self.iterencode(o, _one_shot=True) File “/usr/lib/python3.5/json/encoder.py”, line 256, in iterencode return _iterencode(o, 0) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/util/serialization.py”, line 64, in get_json_type raise TypeError(‘Not JSON Serializable:’, obj) TypeError: (‘Not JSON Serializable:’, <tf.Variable ‘Variable:0’ shape=() dtype=float32>)

TypeError

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 10
  • Comments: 28 (7 by maintainers)

Most upvoted comments

I encountered this issue in my project and have fixed it with @Janesefor’s solution.

The reason for this is that I was using tf function when building the model:

num_identity = tf.cast(tf.math.ceil(num_input_channels * split), tf.int32)

Append .numpy() at the end like this:

num_identity = tf.cast(tf.math.ceil(num_input_channels * split), tf.int32).numpy()

Then the model could be saved as saved_model format.

This might be related: https://keras.io/guides/serialization_and_saving/

I had a similar issue when trying to save a model with a custom layer. I had to add .numpy() to the new variable in get_config function as follows:

class rand_rotate_layer(tf.keras.layers.Layer):
    def __init__(self, batchsize=None, seg_len=None, dim=None, **kwargs):
        super(rand_rotate_layer, self).__init__(**kwargs)
        self.signal_v = tf.Variable(tf.zeros((batchsize, seg_len, dim)), trainable=False)


    def get_config(self): 
        config = super().get_config().copy()
        config.update({
            'signal_v': self.signal_v.numpy()
        })
        return config

Are you saving just the weights or the model? If just the weights, then model.save_weights should suffice.

Not resolved for me. So can we say that as of now there is no way of saving a model (model.save()) compiled using variables (tf.keras.backend.variable) in loss_weights ? @jvishnuvardhan

System information

  • Have I written custom code: NA
  • OS Platform and Distribution: Ubuntu 16.04 LTS
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: NA
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): 1.12.0
  • Python version: 3.5.2
  • Bazel version (if compiling from source): NA
  • GCC/Compiler version (if compiling from source): NA
  • CUDA/cuDNN version: release 9.0, V9.0.176
  • GPU model and memory: Tesla K80, 12 GB

Describe the current behavior When I try to save my model using model.save() where model is a tf.keras.Model instance, it throws a TypeError: (‘Not JSON Serializable:’, <tf.Variable ‘Variable:0’ shape=() dtype=float32>) . I am using a tf.keras.backend.variable() in loss_weights in model.compile. Optimizer: tf.keras.optimizers.Adam Interestingly, when I try to save my model weights only using model.save_weights where model is a tf.keras.Model instance, it works fine, NO ERROR.

Describe the expected behavior It should not throw any type of error during training.

Code to reproduce the issue import tensorflow as tf import numpy as np import os import sys

input_layer = tf.keras.layers.Input(shape=([4,3]), batch_size=1) layer1 = tf.keras.layers.Dense(20)(input_layer) layer2 = tf.keras.layers.Dense(1,name="output1")(layer1) layer3 = tf.keras.layers.Dense(1,name="output2")(layer1) model = tf.keras.Model(inputs=input_layer, outputs=[layer2,layer3]) alpha = tf.keras.backend.variable(0.25) model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001),loss={"output1":tf.keras.metrics.binary_crossentropy,"output2":tf.keras.metrics.binary_crossentropy},loss_weights=[1.0,alpha])

# model.fit() is skipped just to get straight to error. model.save("./weights.h5")

Just execute this code as it is, It will throw same error!!

Other info / logs Traceback (most recent call last): File “main_latest.py”, line 45, in max_queue_size=10) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py”, line 2177, in fit_generator initial_epoch=initial_epoch) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/training_generator.py”, line 216, in fit_generator callbacks.on_epoch_end(epoch, epoch_logs) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/callbacks.py”, line 214, in on_epoch_end callback.on_epoch_end(epoch, logs) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/callbacks.py”, line 601, in on_epoch_end self.model.save(filepath, overwrite=True) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/network.py”, line 1363, in save save_model(self, filepath, overwrite, include_optimizer) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/saving.py”, line 134, in save_model default=serialization.get_json_type).encode(‘utf8’) File “/usr/lib/python3.5/json/init.py”, line 237, in dumps **kw).encode(obj) File “/usr/lib/python3.5/json/encoder.py”, line 198, in encode chunks = self.iterencode(o, _one_shot=True) File “/usr/lib/python3.5/json/encoder.py”, line 256, in iterencode return _iterencode(o, 0) File “/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/util/serialization.py”, line 64, in get_json_type raise TypeError(‘Not JSON Serializable:’, obj) TypeError: (‘Not JSON Serializable:’, <tf.Variable ‘Variable:0’ shape=() dtype=float32>)

TypeError

add .numpy() after alpha ,like the flowing: model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001),loss={“output1”:tf.keras.metrics.binary_crossentropy,“output2”:tf.keras.metrics.binary_crossentropy},loss_weights=[1.0,alpha.numpy()])

@hno1993 I see you’re using Conda to manage your packages, which may mean your Tensorflow installation doesn’t contain all the latest fixes.

That error should be fixed by https://github.com/tensorflow/tensorflow/commit/39bc7bcf94983261a3ee8a72802f5de056728a9c#diff-8eb7e20502209f082d0cb15119a50413.

Try using the latest RC or nightly.