Keras Regressor: Cannot use predict or save methods

391 views
Skip to first unread message

Francisco Vallejo Luna

unread,
Aug 8, 2017, 6:43:22 PM8/8/17
to Keras-users
Hello,

I'm trying to perform regression using KerasRegressor but I'm getting errors when trying to save it for future predictions.
I tried too to predict just before training (fit) in order to avoid the save issues, but I got error too :(

This is the error I get:


Traceback (most recent call last):
  File "train_and_predict.py", line 258, in <module>
    a=estimator.predict(params)
  File "/usr/local/lib/python3.5/dist-packages/keras/wrappers/scikit_learn.py", line 315, in predict
    return np.squeeze(self.model.predict(x, **kwargs))
AttributeError: 'KerasRegressor' object has no attribute 'model'



Here my regression network code, I always get error less than 0.0004 but I'm not able to predict.
I tried to use both predict and save methods. I tried too to use pickle functions.... no sucesss :(

==================================================

seed = 7
numpy.random.seed(seed)
n_splits = 10

def get_reg_model():
    hidden_layer_width = int ( (x_width + y_width) /2)
    activation = 'relu'

    # create model
    model = Sequential()
    model.add(Dense(hidden_layer_width, input_dim=x_width, kernel_initializer='normal', activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(hidden_layer_width, activation=activation))
    model.add(Dense(1, kernel_initializer='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


from keras.wrappers.scikit_learn import KerasRegressor
estimator = KerasRegressor(build_fn=get_reg_model, nb_epoch=epochs, batch_size=batch_size, verbose=verbosity)
kfold = KFold(n_splits=n_splits, random_state=seed)
results = cross_val_score(estimator, X, Y, cv=kfold)
print("Results: %.10f (%.10f) MSE" % (results.mean(), results.std()))



===========================================================================
Reply all
Reply to author
Forward
0 new messages