我必须将结果上传到新文件(json,txt ...)中,该结果的数据类型为字典,其中包含(tf.Variables)(即dict = {'str1':tf.Variables,'str2 ',tf.Variables}),但是错误显示为“ RefVariable类型的对象不可JSON序列化”。
import json
import tensorflow as tf
var1=tf.Variable(tf.random.normal(shape=[1]))
var2=tf.Variable(tf.random.normal(shape=[1]))
sess=tf.compat.v1.Session()
dict={1: var1, 2: var2}
print(dict)
with open('result.json', 'w', encoding='utf-8') as f:
json.dump(dict, f, ensure_ascii=True, indent=4)
f.close()
我想要的结果是“ 1:变量值,2:变量值”。 但是实际结果是
{1: <tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>, 2: <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>}
TypeError: Object of type RefVariable is not JSON serializable
更严重的是,将结果转换为json文件的代码无法识别“ <”。
您能找出我的代码有什么问题吗?