我正在尝试使用过去的价格预测未来7天的价格。我正在使用tensorflow 1.15和python 2.7。我知道这些是旧版本,但是在安装tensorflow最新版本时遇到问题,因此我正在使用这些版本。我的问题是我无法训练模型,因为它显示错误:
ValueError: Error when checking target: expected dense_1 to have shape (7, 7) but got array with shape (7, 1)
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
dataset_train = pd.read_excel('C:/Users/Shashank/Desktop/Neural Network/potatoTrainData.xlsx')
training_set = dataset_train.iloc[:,1:2].values
print(len(training_set))
from sklearn.preprocessing import MinMaxScaler
print(training_set)
sc=MinMaxScaler(feature_range = (0,1))
training_set_scale = sc.fit_transform(training_set)
X_train=[]
Y_train=[]
previous=7
next=7
for i in range(previous , len(training_set_scale)-next):
X_train.append(training_set_scale[i-previous:i,0])
Y_train.append(training_set_scale[i:i+next,0])
X_train,Y_train = np.array(X_train),np.array(Y_train)
X_train = np.reshape(X_train, (X_train.shape[0],X_train.shape[1],1))
print("X train")
print(X_train)
print("Y train")
print(Y_train)
X_train=X_train.reshape(X_train.shape[0],X_train.shape[1],1)
Y_train = Y_train[:, :, None]
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
regressor = Sequential()
regressor.add(LSTM(units = 200, activation='relu', return_sequences = True, input_shape=(X_train.shape[1:]) ))
regressor.add(Dense(7))
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
regressor.fit(X_train,Y_train,epochs=200,batch_size=32)
dataset_test= pd.read_excel('C:/Users/Shashank/Desktop/Neural Network/potatoTestData.xlsx')
real_price = dataset_test.iloc[:,1:2].values
dataset_total=pd.concat((dataset_train['price'] , dataset_test['test_price']),axis = 0)
print(len(dataset_total))
print(dataset_total)
inputs=dataset_total[len(dataset_total)-len(dataset_test)-previous:].values
print(inputs)
inputs=inputs.reshape(-1,1)
inputs=sc.transform(inputs)
X_test=[]
Y_test=[]
for i in range(previous,len(inputs)-next):
X_test.append(inputs[i-previous:i,0])
Y_test.append(inputs[i:i+next,0])
print(X_test)
X_test=np.array(X_test)
X_test=np.reshape(X_test,(X_test.shape[0],X_test.shape[1],1))
predicted_price=regressor.predict(X_test)
predicted_price=sc.inverse_transform(predicted_price)
print(real_price)
plt.plot(real_price , color='red' , label='Real Price')
plt.plot(predicted_price , color='blue' , label='Predicted Price')
plt.title('Price Prediction')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
plt.show()
X_train的形状为(308,7,1),形状Y_train为(308,7,1)。如果我需要在Y_train中添加任何图层,请告诉我该怎么做。
答案 0 :(得分:1)
您通过了inner
,因此LSTM返回每个时间步的输出。因为您设置了7个输出,所以形状将是7x7。您有两种选择:
return_sequences = True
return_sequences = False
层对不起,我目前无法对其进行测试,因此也许还有其他一些细微之处。