我正在尝试使用LSTM预测股价,但遇到以下错误
这是我的代码:
public class BookDetailsBean {
private int bookingid;
private String name;
private String email;
private Long mobilenumber;
private String address;
private String evnets;
private int nooftickets;
public int getBookingid() {
return bookingid;
}
public void setBookingid(int bookingid) {
this.bookingid = bookingid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getMobilenumber() {
return mobilenumber;
}
public void setMobilenumber(Long mobilenumber) {
this.mobilenumber = mobilenumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEvnets() {
return evnets;
}
public void setEvnets(String evnets) {
this.evnets = evnets;
}
public int getNooftickets() {
return nooftickets;
}
public void setNooftickets(int nooftickets) {
this.nooftickets = nooftickets;
}
}```
But booking id is not incrementing, why?
这是我遇到的错误:
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import lstm, time
X_train, X_test, Y_train, Y_test = lstm.load_data('tata.csv', 50, True)
#build model
model = Sequential()
mode.add(LSTM(
input_dim=1,
output_dim=50,
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
100,
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=1,))
model.add(Activation('linear'))
start = time.time()
model.compile(loss='mse', optimizer='rmsprop')
print("compilation time: ", time.time - start)
#train the model
model.fit(
X_train,
Y_train,
batch_size=512,
nb_epoch=1,
validation_split=0.05)
#predicting the prices
predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50)
lstm.plot_results_multiple(predictions, Y_test, 50)
我无法理解问题出在哪里。请查看我链接的图片,以更好地理解。
答案 0 :(得分:0)
当模型输入形状和数据传递形状不同时,会发生此错误。
下面是一个示例,其中我使用了您的代码,但是使用了不同的输入文件(因为我无法import lstm
),并且使用MinMaxScaler
进行了一些预处理以缩放输入,将数据集拆分为{{ 1}}和Xtrain
,最后将Ytrain
转换为list
。
我在代码中添加了np.array
以适应模型中的X_train = X_train[:,:,np.newaxis]
形状。如果没有此行,模型将抛出X_train
。
完整代码-
ValueError: Error when checking input: expected lstm_13_input to have 3 dimensions, but got array with shape (1975, 50)
输出-
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import time
url = 'https://raw.githubusercontent.com/mwitiderrick/stockprice/master/NSE-TATAGLOBAL.csv'
dataset_train = pd.read_csv(url)
training_set = dataset_train.iloc[:, 1:2].values
dataset_train.head()
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0,1))
training_set_scaled = sc.fit_transform(training_set)
X_train = []
y_train = []
for i in range(60, 2035):
X_train.append(training_set_scaled[i-50:i, 0])
y_train.append(training_set_scaled[i, 0])
X_train, Y_train = np.array(X_train), np.array(y_train)
X_train = X_train[:,:,np.newaxis]
#build model
model = Sequential()
model.add(LSTM(
input_dim=1,
output_dim=50,
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
100,
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=1,))
model.add(Activation('linear'))
#model.summary()
start = time.time()
model.compile(loss='mse', optimizer='rmsprop')
print("compilation time: ", time.time() - start)
#train the model
model.fit(
X_train,
Y_train,
batch_size=512,
nb_epoch=1,
validation_split=0.05)
希望这能回答您的问题。谢谢。