我在下面的json文件中定义了一个模型定义
{
"model": "Sequential",
"layers": [
{
"L1": "Conv2D(filters = '8', kernel_size=(3,3), strides=(1, 1), padding='valid', data_format='channels_last', activation='relu', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.), input_shape=(28,28,1))",
"L2": "Conv2D(filters = '8', kernel_size=(3,3), strides=(1, 1), padding='valid', data_format='channels_last', activation='relu', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))",
"L3": "Flatten()",
"L4": "Dense(10, activation='softmax', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer='regularizers.l1(0.)', bias_regularizer='regularizers.l1(0.)', activity_regularizer='regularizers.l1(0.)', kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))"
}
]
}
当我将其加载到模型中时,它将引发以下错误:
TypeError: cannot perform reduce with flexible type
要从json加载模型,我需要以下代码
with open('model_0.json','r') as fb:
con = json.load(fb)
print(con['layers'][0]['L1'])
model = Sequential()
model.add(eval(con['layers'][0]['L1']))
import pdb; pdb.set_trace()
model.add(eval(con['layers'][0]['L2']))
model.add(eval(con['layers'][0]['L3']))
model.add(eval(con['layers'][0]['L4']))
有人有什么主意吗?
答案 0 :(得分:1)
您的数据不应为字符串格式。如果它是字符串格式,则将其更改为数字类型。
import numpy as np
np.array(your_array).astype(np.float)