AttributeError: 'function' 对象没有属性 'fit' [英] AttributeError: 'function' object has no attribute 'fit'
问题描述
我刚刚开始使用深度学习和 Python,当我尝试训练模型时,我已经遇到了这个错误.我认为将基本构建块放在一起会是一个简单的开始项目,但显然我还没有掌握一些基础知识.我的目标是在包含 5 个列值 '1ex','2ex','3ex','4ex','5ex'
的数据集上训练模型并预测 5 个值的序列.>
我正在从我生成的 csv 文件中读取数据集,它按预期显示.
你能帮我理解我错过了什么吗?一如既往地非常感谢.这是我到目前为止编写的代码:
将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt将熊猫导入为 pd导入操作系统从 sklearn.preprocessing 导入 MinMaxScaler从集合导入双端队列随机导入# 加载数据集df = pd.read_csv('DataSet.csv',delimiter=',',usecols=['Wheel','Date','1ex','2ex','3ex','4ex','5ex'])# 把它分成几部分times = sorted(df.index.values) # 获取时间last_10pct = sorted(df.index.values)[-int(0.1*len(times))] # 获取最后 10% 的次数last_20pct = sorted(df.index.values)[-int(0.2*len(times))] # 获取最后 20% 的次数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)] # 现在 train_df 是最后 20% 的所有数据# 删除日期"列train_df.drop(columns=["Date"], inplace=True)validation_df.drop(columns=["Date"], inplace=True)test_df.drop(columns=["Date"], inplace=True)# 该模型从 tensorflow.keras.models 导入顺序从 tensorflow.keras.layers 导入密集# 从 tensorflow.keras.layers 导入 LSTM从 tensorflow.keras.wrappers.scikit_learn 导入 KerasRegressor从 sklearn.model_selection 导入 cross_val_score从 sklearn.model_selection 导入 KFold从 sklearn.preprocessing 导入 StandardScaler从 sklearn.pipeline 导入管道# 定义基础模型定义基线模型():# scale = StandardScaler()# 创建模型模型 = 顺序()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))# 编译模型# model.compile(loss='mean_absolute_error', optimizer='adam')model.compile(loss='mean_squared_error', 优化器='adam')#model.fit(train_df, epochs = 5)回报模式# 训练模型基线模型.fit(train_df,batch_size=1,epochs=200,verbose=1)
如果你写baseline_model
,它返回的是函数,而不是结果.
因此baseline_model.fit
不能被调用,因为'function'对象没有属性'fit'
您必须执行该函数以获取其结果,使用括号 - baseline_model()
- 然后将对结果执行 fit
.;)
tl;博士:
baseline_model.fit(
-> baseline_model().fit(
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)
If you write baseline_model
, it returns the function, not the result.
Therefore baseline_model.fit
can't be called because 'function' object has no attribute 'fit'
You must execute the function to get its result, using parentheses - baseline_model()
- and then fit
will be performed on the result. ;)
tl;dr:
baseline_model.fit(
-> baseline_model().fit(
这篇关于AttributeError: 'function' 对象没有属性 'fit'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!