pythondeep-learningneural-networkvalueerrorloss

What's causing the ValueError: could not interpret identifier: loss?


Within the code below, please help me understand why the loss metric is not being parsed

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from keras.models import Sequential
from keras.layers import Dense, Input
from scikeras.wrappers import KerasRegressor

# Load data
train_data = pd.read_csv('data/data_train.csv')
test_data = pd.read_csv('data/data_test.csv')

# Define features and target
target = 'SalePrice'
features = train_data.drop(columns=[target]).columns

X = train_data[features]
y = train_data[target]

# Split data into training and validation sets
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

# Identify numerical and categorical columns
numerical_cols = [cname for cname in X_train.columns if X_train[cname].dtype in ['int64', 'float64']]
categorical_cols = [cname for cname in X_train.columns if X_train[cname].dtype == 'object']

# Preprocess numerical data
numerical_transformer = SimpleImputer(strategy='mean')
X_train_num = pd.DataFrame(numerical_transformer.fit_transform(X_train[numerical_cols]), columns=numerical_cols)
X_val_num = pd.DataFrame(numerical_transformer.transform(X_val[numerical_cols]), columns=numerical_cols)

scaler = StandardScaler()
X_train_num = scaler.fit_transform(X_train_num)
X_val_num = scaler.transform(X_val_num)

# Preprocess categorical data
categorical_transformer = SimpleImputer(strategy='most_frequent')
X_train_cat = pd.DataFrame(categorical_transformer.fit_transform(X_train[categorical_cols]), columns=categorical_cols)
X_val_cat = pd.DataFrame(categorical_transformer.transform(X_val[categorical_cols]), columns=categorical_cols)

onehot_encoder = OneHotEncoder(handle_unknown='ignore')
X_train_cat = onehot_encoder.fit_transform(X_train_cat)
X_val_cat = onehot_encoder.transform(X_val_cat)

# Combine numerical and categorical data
X_train_preprocessed = np.hstack([X_train_num, X_train_cat.toarray()])
X_val_preprocessed = np.hstack([X_val_num, X_val_cat.toarray()])

def build_model():
    input_shape = X_train_preprocessed.shape[1]
    model = Sequential([
        Input(shape=(input_shape,)),
        Dense(128, activation='relu'),
        Dense(64, activation='relu'),
        Dense(1)
    ])
    model.compile(optimizer='adam', loss='mean_squared_error')
    return model

# Define model
model = KerasRegressor(model=build_model, epochs=100, batch_size=10, verbose=0, metrics=['mean_squared_error'])


# Fit the model
model.fit(X_train_preprocessed, y_train)

# Predictions and evaluation
preds = model.predict(X_val_preprocessed)
rmse = np.sqrt(mean_squared_error(y_val, preds))
print('RMSE:', rmse)`

I haven't been able to try anything, I'm stuck on this. Is there something I'm not seeing? this is the entire traceback:

`Traceback (most recent call last):
  File "C:\Users\USER\Desktop\Kaggle\house-prices\house.py", line 67, in <module>
    model.fit(X_train_preprocessed, y_train)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\scikeras\wrappers.py", line 735, in fit
    self._fit(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\scikeras\wrappers.py", line 900, in _fit
    self._fit_keras_model(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\scikeras\wrappers.py", line 515, in _fit_keras_model
    raise e
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\scikeras\wrappers.py", line 510, in _fit_keras_model

    key = metric_name(key)
          ^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\scikeras\utils\__init__.py", line 109, in metric_name
    fn_or_cls = keras_metric_get(metric)
                ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\metrics\__init__.py", line 204, in get
    raise ValueError(f"Could not interpret metric identifier: {identifier}")
ValueError: Could not interpret metric identifier: loss`

Solution

  • Try this:

    from keras.wrappers.scikit_learn import KerasClassifier
    

    use Keras wrappers instead.
    Here is a helpful example for your reference.