【问题标题】:AttributeError: 'function' object has no attribute 'fit'AttributeError: 'function' 对象没有属性 'fit'
【发布时间】:2019-10-07 11:55:20
【问题描述】:

我刚开始使用深度学习和 python,当我尝试训练模型时,我已经遇到了这个错误。 我认为将基本构建块放在一起是一个简单的开始项目,但我显然还没有掌握一些基础知识.. 我的目标是在 5 列值'1ex','2ex','3ex','4ex','5ex' 的数据集上训练模型并预测 5 个值的序列。

我正在从我生成的 csv 文件中读取数据集,它按预期显示。

你能帮我理解我错过了什么吗? 非常感谢你一如既往。 这是我到目前为止写的代码:

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)

【问题讨论】:

    标签: python tensorflow2.0


    【解决方案1】:

    如果你写baseline_model,它会返回函数,而不是结果。

    因此无法调用baseline_model.fit,因为'function' object has no attribute 'fit'

    您必须执行该函数以获取其结果,使用括号 - baseline_model() - 然后将在结果上执行 fit。 ;)


    tl;博士:

    baseline_model.fit( -> baseline_model().fit(

    【讨论】:

    • 谢谢..我真的看不到它哈哈面团显然出现了各种错误..但至少现在我可以继续调查和学习:)
    • 有时很难发现此类错误,因为视觉上一切都在那里。如果有人习惯了其他语言,他们并不总是期望函数是一个对象或期望类型检查。
    • 当然,我知道的另一种语言是 Swift,我仍然必须习惯 Python 范式,在某些方面我实际上更喜欢,但是 Swift 中的这种东西会被 swift 检查本身,这对拼写错误或与类型相关的错误有很大帮助。好吧..这只能通过没有自动检查系统使我成为更好的开发人员..面团这意味着我会经常寻求帮助..现在模型运行了,我已经看到我必须摆脱数据集中的另一列,因为它是一个字符串,但我没有成功..hahah..缺少一些索引..
    • 一些 IDE 会为更简单的事情做一些类型检查。您还可以注释您的函数或添加带有参数及其类型的文档字符串,以跟踪应该是什么 - 并帮助您的 IDE 进行类型检查。 ;)
    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 2022-01-26
    • 2019-05-17
    • 2017-07-04
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多