大数据集和cnnlstm模型。我有一些问题。为什么我的模型不运行?

2024-09-28 16:23:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据集是温度数据 所有365件事 尺寸为33013301

import cv2
import pandas as pd
import numpy as np
import tensorflow as tf
import keras
from keras import layers
from keras.layers import Dense, Input, Reshape, Conv2DTranspose, Conv2D, Conv1D, Flatten, Lambda, LSTM, RepeatVector, TimeDistributed, MaxPooling2D, MaxPooling1D, UpSampling2D, UpSampling1D, Dropout, GlobalAveragePooling2D
from keras.models import Model, Sequential
from keras import backend as K
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#from keras import Model ,models, layers, optimizers, utils
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

def generateX(seq_in, n_in, imh=3301, imw=3301):
    x_train = []
    y_train = []
    for i in range(len(seq_in)):
        x = np.array(seq_in[i:(i + n_in)])
        y = np.array(seq_in[i + 1:(i + 1 + n_in)])
        ximgs = np.zeros_like(x.reshape(imh, imw, n_in))
        yimgs = np.zeros_like(y.reshape(imh, imw, n_in))
        for zx in range(n_in):
            ximgs[:,:,zx] = x[zx]
            yimgs[:,:,zx] = y[zx]
        if (i + n_in) < len(seq_in)-1:
            x_train.append(x)
            y_train.append(y)
        else:
            break
        return np.array(x_train), np.array(y_train)

def load_data(seq_len, n_in):
    seq_in = []
    dpath = '0.ori_data/'
    for i in range(seq_len):
        num = i+1
        load_data = np.loadtxt(dpath+'coms_mi_le2_sst_data'+str(num)+'.txt', delimiter=",")
        print('load coms_mi_le2_sst_data'+str(num))
        seq_in.append(load_data)
    x_train, y_train = generateX(seq_in, n_in)
    imh = x_train.shape[2]
    imw = x_train.shape[3]
    x_train = x_train.reshape((-1, n_in, imh, imw, 1))
    y_train = y_train.reshape((-1, n_in, imh, imw, 1))
    return x_train, y_train, imh, imw


# define input sequence
seq_in = []

width = 3301 # input image width
height = 3301 # input image height
n_in = 2 # input sequence length
n_out = n_in
channel = 1 # image channel
seq_len = 4


dpath = 'image/'

train_x, train_y, imh, imw = load_data(seq_len, n_in)
test_x = train_x[-1].reshape((-1, n_in, imh, imw, 1))
print("x_train.shape : ", train_x.shape)
print("y_train.shape : ", train_y.shape)
print("x_test.shape : ", test_x.shape)

我想读取所有数据,然后预测一些数据。 但是python spyder不断关闭


def seq_cnnlstm():
    model = Sequential()
    # after having Conv2D...
    model.add(TimeDistributed(Conv2D(8, (30,30), activation='relu', padding="same"), input_shape=(n_in, imw, imh, channel))) # 5 images...))
    model.add(TimeDistributed(MaxPooling2D(pool_size=(30, 30), strides=110, padding='same')))
    model.add(TimeDistributed(Conv2D(8, (30,30), activation='relu', padding="same")))
    model.add(TimeDistributed(MaxPooling2D(pool_size=(110, 110), strides=110, padding='same')))
    # We need to have only one dimension per output
    # to insert them to the LSTM layer - Flatten or use Pooling
    model.add(TimeDistributed(GlobalAveragePooling2D())) # Or Flatten()))
    model.add(TimeDistributed(Dense(32, activation='relu'))) # Or Flatten()))
    # previous layer gives 5 outputs, Keras will make the job
    # to configure LSTM inputs shape (5, ...)
    model.add(LSTM(16, activation='relu', return_sequences=False))
    # and then, common Dense layers... Dropout...
    # up to you
    model.add(Dense(16, activation='relu'))
    model.add(Dropout(.2))
    model.add(Dense(n_out * imw * imh * channel))
    model.add(Reshape((n_out, imw, imh, channel)))
    model.compile(optimizer='adam', loss='mse')
    model.summary()
    # fit model
    model.fit(train_x, train_y, epochs=10, verbose=1)
    model.save('seq_cnnlstm.h5')
    # predict
    yhat = model.predict(test_x)
    print("np.array(yhat).shape : ", np.array(yhat).shape)
    output1 = np.array(yhat[-1][0]).reshape((height, width))
    fig, ax = plt.subplots()
    im = ax.imshow(output1)
    ax.set_title('seq_cnnlstm')
    ax.set_ylim(ax.get_ylim()[::-1])
    plt.savefig('seq_cnnlstm.png', dpi=300)
    plt.close()
    np.savetxt('seq_cnnlstm.txt', load_data2, delimiter=',')
    return model

我设置了一个模型。 为什么我的python关闭了?内存不足?我的硬盘容量为1.5TB。 我变小了因子大小。但同样的错误也会发生

enter image description here

我的模型摘要

我应该怎么做才能运行我所有的代码