【问题标题】:TypeError: cannot perform reduce with flexible type KerasTypeError:无法使用灵活类型 Keras 执行 reduce
【发布时间】:2019-05-23 07:27:37
【问题描述】:

我在 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']))

有人知道吗?

【问题讨论】:

    标签: python tensorflow keras python-3.6


    【解决方案1】:

    您的数据不应为字符串格式。如果它是字符串格式,则将其更改为数字类型。

    import numpy as np
    
    np.array(your_array).astype(np.float)
    

    【讨论】:

    • 谢谢!!搞错了。
    猜你喜欢
    • 2016-09-13
    • 2017-09-12
    • 1970-01-01
    • 2014-02-23
    • 2015-04-08
    • 2020-10-08
    • 2020-10-03
    • 2013-12-02
    • 1970-01-01
    相关资源
    最近更新 更多