gpt4 book ai didi

python - 从文本文件加载keras模型

转载 作者:太空宇宙 更新时间:2023-11-04 04:13:33 25 4
gpt4 key购买 nike

我将我的模型层存储在一个文本文件中,如下所示:

{

"model" : "Sequential",

"layers": [

{
"L1": "Conv2D(filters = 64, kernel_size=(3,3), strides=(1, 1), padding='valid', data_format='channels_last', dilation_rate=None, activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, input_shape=(28,28,1))",

"L2": "Conv2D(filters = 32, kernel_size=(3,3), strides=(1, 1), padding='valid', data_format='channels_last', dilation_rate=None, activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)" ,

"L3": "Flatten()",

"L4": "Dense(10, activation='softmax', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)"
}
]

}

我正在尝试像这样在 python 控制台中加载模型:

with open('model.txt','r') as fb:
con = json.load(fb, object_pairs_hook=ordereddict.OrderedDict)
from keras.models import Sequential
model = Sequential()
model.add(con['layers'][0]['L1'])

但它给出了以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ashutosh/.local/lib/python2.7/site-packages/keras/engine/sequential.py", line 132, in add
'Found: ' + str(layer))

TypeError: The added layer must be an instance of class Layer. Found: Conv2D(filters = 32, kernel_size=(3,3), strides=(1, 1), padding='valid', data_format='channels_last', dilation_rate=None, activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)

我应该怎么做才能从文本文件加载 Conv2D 层?

最佳答案

con['layers'][0]['L1'] 是一个字符串。

考虑使用 eval 执行它

model.add(eval(con['layers'][0]['L1']))

关于python - 从文本文件加载keras模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55897830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com