[Python] AttributeError: 'function' object has no attribute 'fit'

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm just starting with deep learning and python and I'm already stuck with this error when I try to train the model. I thought it would be an easy starting project to get together the basic building blocks, but I obviously haven't grasped some basics.. My goal is to train a model on a data set of 5 column values '1ex','2ex','3ex','4ex','5ex' and predict sequences of 5 values.

    I'm reading the dataset from a csv file i generated and it's displaying as expected.

    Can you help me understanding what I'm missing out? Thank you very much as always. This is the code I've written so far:

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    import os
    from sklearn.preprocessing import MinMaxScaler

    from collections import deque
    import random

    # load the data set
    df = pd.read_csv('DataSet.csv',delimiter=',',usecols=['Wheel','Date','1ex','2ex','3ex','4ex','5ex'])

    # divide it into portions

    times = sorted(df.index.values) # get the times
    last_10pct = sorted(df.index.values)[-int(0.1*len(times))] # get the last 10% of the times
    last_20pct = sorted(df.index.values)[-int(0.2*len(times))] # get the last 20% of the times

    test_df = df[(df.index >= last_10pct)]
    validation_df = df[(df.index >= last_20pct) & (df.index < last_10pct)]
    train_df = df[(df.index < last_20pct)] # now the train_df is all the data up to the last 20%

    # drop 'Date' column
    train_df.drop(columns=["Date"], inplace=True)
    validation_df.drop(columns=["Date"], inplace=True)
    test_df.drop(columns=["Date"], inplace=True)

    # the model

    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    # from tensorflow.keras.layers import LSTM
    from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
    from sklearn.model_selection import cross_val_score
    from sklearn.model_selection import KFold
    from sklearn.preprocessing import StandardScaler
    from sklearn.pipeline import Pipeline

    # define base model

    def baseline_model():
    # scale = StandardScaler()
    # create model
    model = Sequential()
    model.add(Dense(5, input_dim=5, kernel_initializer='normal', activation='relu'))
    model.add(Dense(15, kernel_initializer='normal', activation='relu'))
    model.add(Dense(15, kernel_initializer='normal', activation='relu'))
    model.add(Dense(5, kernel_initializer='normal', activation = softmax))
    # Compile model
    # model.compile(loss='mean_absolute_error', optimizer='adam')
    model.compile(loss='mean_squared_error', optimizer='adam')
    # model.fit(train_df, epochs = 5)
    return model

    # train the model

    baseline_model.fit(train_df, batch_size=1, epochs=200, verbose=1)

    Continue reading...

Compartilhe esta Página