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>)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 10
- Comments: 28 (7 by maintainers)
I encountered this issue in my project and have fixed it with @Janesefor’s solution.
The reason for this is that I was using
tffunction when building the model:Append
.numpy()at the end like this:Then the model could be saved as
saved_modelformat.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:
Are you saving just the weights or the model? If just the weights, then
model.save_weightsshould 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
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.