Ad

TypeError: Cannot Perform Reduce With Flexible Type Keras

- 1 answer

I have a model definition defined in a json file like below

{
    "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.))"
        }
    ]
}

When I load it in a model it throws the following error:

TypeError: cannot perform reduce with flexible type

For loading the model from json, I have the following code

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

Does anyone have any idea ?

Ad

Answer

Your data should not be in string format. If it's in string format then change it to a numeric type.

import numpy as np

np.array(your_array).astype(np.float)
Ad
source: stackoverflow.com
Ad