预期density_1具有形状(1176,),但获得具有形状的数组(1288,)

时间:2019-07-08 22:09:32

标签: python tensorflow keras

我很难让它运行,我正在尝试预测库存,但是我真的不知道如何提出任何建议

import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.callbacks import ModelCheckpoint
from keras.utils import np_utils
seq_length = 10
random.seed(42)
dataset = pd.read_csv('GOOG.csv')
dataset = dataset.dropna()
dataset = dataset[['Close']]
dataset = dataset.values
x_train = []
y_train = []


for i in range(len(dataset)-(seq_length)):
    x_train.append(dataset[i:i+seq_length])
    y_train.append(dataset[i+seq_length])

x_train = np.array(x_train)
y_train = np.array(y_train)


x_test = x_train[(int((0.75)*len(x_train))):]
x_train = x_train[0:(int((0.75)*len(x_train)))]
y_test = y_train[(int((0.75)*len(y_train))):]
y_train = y_train[0:(int((0.75)*len(y_train)))]



x_train = np.reshape(x_train, (x_train.shape[0], seq_length, 1))

y = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
def Model():


    model = Sequential()
    model.add(LSTM(512, input_shape=(x_train.shape[1], x_train.shape[2]),return_sequences = True))
    model.add(Dropout(0.2))
    model.add(LSTM(512))
    model.add(Dropout(0.2))
    model.add(Dense(1176, activation='softmax'))


    model.compile(loss='categorical_crossentropy', optimizer='adam')


    filepath="weights-improvement-{epoch:02d}-{loss:.4f}.hdf5"
    checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
    callbacks_list = [checkpoint]
    print(x_train.shape)
    print(y.shape)
    print(y_test.shape)
    model.summary()
    model.fit(x_train, y, validation_data = (x_test, y_test), epochs=1000, batch_size=128, callbacks = callbacks_list)


Model()

它应该开始拟合数据集,但是验证数据正在抛弃它。我在这个主题上没有太多的经验,任何改进和/或解释将不胜感激。

0 个答案:

没有答案