我正在尝试keras
从另一个训练有素的keras
模型创建一个新模型
来自以下方面的模型训练样本代码:
#TF version 2.2.0
from tensorflow.python.keras import models, layers
from tensorflow.python.keras.models import Sequential
from tensorflow.python import keras
import tensorflow as tf
from tensorflow.python.keras.layers import Dense
from tensorflow.keras.datasets import boston_housing
(x_train,y_train), (x_test,y_test) = boston_housing.load_data()
model = Sequential()
model.add(Dense(2, activation='relu', input_shape=(13,)))
model.add(Dense(1, activation='softmax'))
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train, y_train,batch_size= 64,epochs= 1,validation_split=0.2)
将模型另存为 json
json_obj = model.to_json()
new_model = keras.models.model_from_json(json_obj)
但是创建后new_model
的weights
不同:
model.get_weights() != new_model.get_weights()
如果new_model
使用创建,则情况相同from_config()
。现在的问题是,不应该的重量是上是相同的model
,并new_model
为我创建new_model
的model
或我的理解是错误的?任何建议都是有帮助的
to_json
不会节省模型的权重,而只会节省体系结构。
检查这里:to_json方法
我建议您使用save_model方法。
如果要直接将模型复制到另一个模型,请执行以下操作:
new_model = tf.keras.models.clone_model(model)
new_model.set_weights(model.get_weights())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句