Skip to main content

AttributeError: 'NoneType' object has no attribute 'outputs'

How do you save a tensorflow keras model to disk h5 format when the model is trained in the scikit learn pipeline fashion? Am trying to follow this example but not having any luck.

This works to train the models:

import numpy as np
import pandas as pd
from tensorflow import keras
from tensorflow.keras import models
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from scikeras.wrappers import KerasRegressor
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
import os
import joblib


ELECTRIC_POINT = 'total_main_kw'


# clean dataset
def clean_dataset(df):
    assert isinstance(df, pd.DataFrame), "df needs to be a pd.DataFrame"
    df.dropna(inplace=True)
    indices_to_keep = ~df.isin([np.nan, np.inf, -np.inf]).any(1)
    cleaner = (f'dataset has been cleaned')
    print(cleaner)
    return df[indices_to_keep].astype(np.float64)

def my_model(input_shape):
    # create model
    model = Sequential()
    model.add(Dense(22, input_shape=input_shape, kernel_initializer='normal', activation='relu'))
    model.add(Dense(14, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, kernel_initializer='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model

# load data
df = pd.read_csv('./all_data.csv', index_col=[0], parse_dates=True)
df = clean_dataset(df)


# shuffle the DataFrame rows
df = df.sample(frac=1)

X = np.array(df.drop([ELECTRIC_POINT], 1))
Y = np.array(df[ELECTRIC_POINT])

# set the input shape
input_shape = (X.shape[1],)
print(f'Feature shape: {input_shape}')

# define the Keras model
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=my_model, input_shape=input_shape, epochs=100, batch_size=5, verbose=0)))
pipeline = Pipeline(estimators)

# define number of models to train
num_models = int(np.sqrt(input_shape))
#num_models = 2
print(f'Number of models: {num_models}')

# define k-fold cross-validation
kfold = KFold(n_splits=num_models)

# define early stopping and model checkpoint callbacks
callbacks = [EarlyStopping(monitor='val_loss', patience=10), 
             ModelCheckpoint(filepath=os.path.join(os.path.curdir, 'model.h5'), 
             monitor='val_loss', save_best_only=True)]

# evaluate the model using cross-validation with callbacks
results = []
for train_idx, test_idx in kfold.split(X, Y):
    X_train, X_test = X[train_idx], X[test_idx]
    y_train, y_test = Y[train_idx], Y[test_idx]
    pipeline.fit(X_train, y_train, mlp__validation_data=(X_test, y_test), mlp__callbacks=callbacks)
    mse = pipeline.score(X_test, y_test)
    print(f'MSE this round: {mse}')
    results.append(mse)


# report performance
print("MSE: %.2f (%.2f)" % (np.mean(results), np.std(results)))

# compare report performance to electricity summary stats
print(df[ELECTRIC_POINT].agg([np.min,np.max,np.mean,np.median]))

If I print the model_step model_step = pipeline.steps.pop(-1)[1] this will return:

KerasRegressor(
    model=None
    build_fn=<function my_model at 0x0000029156028CA0>
    warm_start=False
    random_state=None
    optimizer=rmsprop
    loss=None
    metrics=None
    batch_size=5
    validation_batch_size=None
    verbose=0
    callbacks=None
    validation_split=0.0
    shuffle=True
    run_eagerly=False
    epochs=100
    input_shape=(27,)
)

And then I can save the pipeline to a pickel file which works I get the pipeline.pkl in my current dir:

# save best trained model to file
joblib.dump(pipeline, os.path.join(os.path.curdir, 'pipeline.pkl'))

But trying to run keras save_model:

models.save_model(model_step.model, os.path.join(os.path.curdir, 'model.h5'))

I get an error:

AttributeError: 'NoneType' object has no attribute 'outputs'

Any tips appreciated



source https://stackoverflow.com/questions/75989817/attributeerror-nonetype-object-has-no-attribute-outputs

Comments

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype