根据Udemy教程,我正在keras Spyder中实现LSTM网络。在教程视频中,代码正在运行,但是当我自己运行代码时,该步骤与教程视频代码完全相同,因此会出现以下错误:
检查目标时出错:预期density_26具有3维,但数组的形状为(1198,1)
它与Spyder的版本有关吗? x_train
的大小为(1198,60,1)
,y_train
的大小为(1198,)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Importing the training set
dataset_train = pd.read_csv('E:\\downloads_1\\Recurrent_Neural_Networks\\Google_Stock_Price_Train.csv')
training_set = dataset_train.iloc[:,1:2].values
#Feature Scaling
from sklearn.preprocessing import MinmaxScaler
sc = MinmaxScaler(feature_range=(0,1))
training_set_scaled = sc.fit_transform(training_set)
#Creating a Data Structure with 60 timesteps and 1 output
x_train = []
y_train = []
for i in range(60,1258):
x_train.append(training_set_scaled[i-60:i,0])
y_train.append(training_set_scaled[i,0])
x_train,y_train = np.array(x_train),np.array(y_train)
#Reshaping
x_train = np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))
#Part 2_Building the Rnn
#importing te keras libraries and Packages
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
#initializing the RNN
regressor = Sequential()
#Adding the first layer LSTM and some Dropout Regularisations
regressor.add(LSTM(units = 50,return_sequences= True,input_shape = (x_train.shape[1],1)))
regressor.add(Dropout(0.2))
#Adding the second layer LSTM and some Dropout Regularisations
regressor.add(LSTM(units = 50,return_sequences= True))
regressor.add(Dropout(0.2))
#Adding the third layer LSTM and some Dropout Regularisations
regressor.add(LSTM(units = 50,return_sequences= True))
regressor.add(Dropout(0.2))
#Adding the fourth layer LSTM and some Dropout Regularisations
regressor.add(LSTM(units = 50,return_sequences= True))
regressor.add(Dropout(0.2))
#Adding the output layer
regressor.add(Dense(units = 1))
#compiling the RNN
regressor.compile(optimizer= 'adam',loss = 'mean_squared_error')
#Fitting the RNN TO the training set`enter code here`
regressor.fit(x_train,y_train,epochs = '100',batch_size = 32 )