keras 为包含448个文件的文件夹中的每个csv构建LSTM模型[已关闭]

csga3l58  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(107)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

3天前关闭。
Improve this question
我有包含448个csv文件的数据,每个csv具有相同的格式,8个功能和一个目标作为输出。我需要为每个文件建立LSTM和MLP模型。数据如下。每个文件的天数是6939 data。我将不胜感激的任何建议。
首先,我写了以下代码:

source = 'folder path'
files =  glob.glob(source)
def BatchGenerator(files):
    for file in files:
        df = pd.read_csv(file, index_col=None)
        df = df.set_index(['Date'])
        x_train = df.iloc[:,0:8]
        y_train = df.iloc[:,10]
        yield (x_train,y_train)
    
for (x_train,y_train) in BatchGenerator(files):
    #data split and scaling
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(x_train)
    x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.2, random_state=20)
        
     
for (x_train, y_train) in BatchGenerator(files):    
    def get_model():
        model = Sequential()
        model.add(LSTM(units = 128, input_shape=(1,features_number),return_sequences=True))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50))
        model.add(Dropout(0.2))
        outputs = model.add(Dense(8, activation='relu'))
        model.add(Dense(1, activation='sigmoid'))
        
        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['mape','mse', 'mae','accuracy'])  
        return model
    model = get_model()
    history= model.fit(x_train, y_train, epochs=5, batch_size=100, verbose=2,
                   validation_split= 0.1,validation_data=(x_test,y_test))

我需要为每个文件定义、缩放、拆分数据到x_train、y_train中,然后为x_test预测y_hat。(测试大小=0.2)。层“sequential_31”的输入0与层不兼容:预期形状=(无,1,8),找到的形状=(无,8)

vojdkbi0

vojdkbi01#

假设您的代码包含您想要执行的所有操作,并且所有CSV文件都具有相同的结构,则可以为每个CSV文件训练单独的模型,如下所示:

n = 1
source = 'folder path'
files =  glob.glob(source)

for file in files:
    print(f'Starting work on model #{n}.')
    df = pd.read_csv(file, index_col=None)
    df = df.set_index(['Date'])
    X_train = df.iloc[:,0:8]
    y_train = df.iloc[:,10]
    # Scaler
    scaler = MinMaxScaler(feature_range=(0, 1))
    # Train test split
    X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.2, random_state=20)
    # Scaling train and test data
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)
    
    def get_model():
    '''Create the LSTM model'''
        model = Sequential()
        model.add(LSTM(units = 128, input_shape=(1, features_number),return_sequences=True))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50))
        model.add(Dropout(0.2))
        outputs = model.add(Dense(8, activation='relu'))
        model.add(Dense(1, activation='sigmoid'))
        
        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['mape','mse', 'mae','accuracy'])  
        return model
    model = get_model()
    history= model.fit([X_train_scaled], y_train, epochs=5, batch_size=100, verbose=2, validation_split= 0.1,validation_data=(X_test_scaled ,y_test))
    # Taking predictions from the model
    y_hat_test = model.predict([X_test_scaled])
    # Evaluation metrics can be calculated using y_hat_test and y_test
    # The trained model can be saved here if you want
    # Include any other operations you want here.
    n += 1

在这里,我将所有代码移到了一个for循环中,以简化代码并避免多次调用BatchGenerator。
此外,我还更改了缩放变换和训练测试拆分的顺序。作为一种实践,您只需要在训练集数据上拟合缩放器,否则您可能会将整个数据集的细节泄漏到模型中。因此,在此代码中,首先完成训练和测试拆分,然后使用训练集拟合缩放器,最后使用拟合的缩放器转换训练和测试特征。
对于您所面临的问题,您在LSTM模型的输入层中提到的输入形状之间似乎存在不匹配:model.add(LSTM(units = 128, input_shape=(1,features_number),return_sequences=True))由于错误指出您的输入具有(None, 8)的形状,我修改了模型的输入形状,方法是将X_train_scaled和X_test_scaled Package 在列表中,使它们具有与模型所需的输入形状匹配的维度。

相关问题