我只是从深度学习和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)
答案 0 :(得分:0)
如果您写baseline_model
,它将返回函数,而不是结果。
因此baseline_model.fit
因为'function' object has no attribute 'fit'
不能被调用
必须使用括号-baseline_model()
-来执行该函数以获取结果,然后对结果执行fit
。 ;)
tl; dr:
baseline_model.fit(
-> baseline_model().fit(