2016-03-17 31 views
12

Tôi tiếp tục nhận được lỗi input_shape từ mã sau.Tại sao tôi gặp lỗi input_shape của Keras LSTM RNN?

from keras.models import Sequential 
from keras.layers.core import Dense, Activation, Dropout 
from keras.layers.recurrent import LSTM 

def _load_data(data): 
    """ 
    data should be pd.DataFrame() 
    """ 
    n_prev = 10 
    docX, docY = [], [] 
    for i in range(len(data)-n_prev): 
     docX.append(data.iloc[i:i+n_prev].as_matrix()) 
     docY.append(data.iloc[i+n_prev].as_matrix()) 
    if not docX: 
     pass 
    else: 
     alsX = np.array(docX) 
     alsY = np.array(docY) 
     return alsX, alsY 

X, y = _load_data(dframe) 
poi = int(len(X) * .8) 
X_train = X[:poi] 
X_test = X[poi:] 
y_train = y[:poi] 
y_test = y[poi:] 

input_dim = 3 

Tất cả những điều trên đều diễn ra suôn sẻ. Đây là nơi nó đi sai.

in_out_neurons = 2 
hidden_neurons = 300 
model = Sequential() 
#model.add(Masking(mask_value=0, input_shape=(input_dim,))) 
model.add(LSTM(in_out_neurons, hidden_neurons, return_sequences=False, input_shape=(len(full_data),))) 
model.add(Dense(hidden_neurons, in_out_neurons)) 
model.add(Activation("linear")) 
model.compile(loss="mean_squared_error", optimizer="rmsprop") 
model.fit(X_train, y_train, nb_epoch=10, validation_split=0.05) 

Nó trả về lỗi này.

Exception: Invalid input shape - Layer expects input ndim=3, was provided with input shape (None, 10320) 

Khi tôi kiểm tra the website nó nói để chỉ định một bộ "(ví dụ: 100,) cho đầu vào 100 chiều)."

Điều đó đang được nói, tập dữ liệu của tôi bao gồm một cột có độ dài 10320. Tôi cho rằng điều đó có nghĩa là tôi nên đặt (10320,) vào làm input_shape, nhưng tôi sẽ gặp lỗi. Có ai có giải pháp không?

+0

bất kỳ may mắn với điều này? – Radix

+0

Không có anh chàng. Đó là một bi kịch. Tôi phải đi tư vấn vì điều này. Tôi đã phá vỡ. – NickTheInventor

+0

Tìm thấy điều này - có vẻ như bạn phải tự thực hiện các kỷ nguyên (bit cuối cùng của mã): https://github.com/fchollet/keras/blob/master/examples/stateful_lstm.py – Radix

Trả lời

0

Cố gắng sử dụng lớp LSTM mà không chỉ định hình dạng đầu vào. Hãy để Keras làm công việc cho bạn. Tôi nghĩ rằng bạn đã nhận xét mặt nạ cũng vì bạn nhận được vấn đề tương tự. Tôi phải đối mặt với nó trước và nó quay ra input_shape = (time_steps, input_dim). Tôi nghĩ điều này xảy ra do suy luận hình dạng tự động mới trong Keras.

4

Sự hiểu biết của tôi là cả đầu vào và đầu ra của bạn là các vectơ một chiều. Bí quyết là để định hình lại cho họ theo yêu cầu Keras:

from keras.models import Sequential 
from keras.layers.core import Dense, Activation, Dropout 
from keras.layers.recurrent import LSTM 
import numpy as np 

X= np.random.rand(1000) 
y = 2*X 

poi = int(len(X) * .8) 
X_train = X[:poi] 
y_train = y[:poi] 

X_test = X[poi:] 
y_test = y[poi:] 

# you have to change your input shape (nb_samples, timesteps, input_dim) 
X_train = X_train.reshape(len(X_train), 1, 1) 
# and also the output shape (note that the output *shape* is 2 dimensional) 
y_train = y_train.reshape(len(y_train), 1) 


#in_out_neurons = 2 
in_out_neurons = 1 

hidden_neurons = 300 
model = Sequential() 
#model.add(Masking(mask_value=0, input_shape=(input_dim,))) 
model.add(LSTM(hidden_neurons, return_sequences=False, batch_input_shape=X_train.shape)) 
# only specify the output dimension 
model.add(Dense(in_out_neurons)) 
model.add(Activation("linear")) 
model.compile(loss="mean_squared_error", optimizer="rmsprop") 
model.fit(X_train, y_train, nb_epoch=10, validation_split=0.05) 

# calculate test set MSE 
preds = model.predict(X_test).reshape(len(y_test)) 
MSE = np.mean((preds-y_test)**2) 

Dưới đây là những điểm mấu chốt:

  • khi bạn thêm lớp đầu tiên của bạn, bạn được yêu cầu để xác định số nút ẩn, và đầu vào của bạn hình dạng. lớp hậu quả không yêu cầu hình dạng đầu vào như họ có thể suy ra nó từ các nút ẩn của lớp trước
  • Tương tự như vậy, đối với lớp đầu ra của bạn, bạn chỉ xác định số nút đầu ra

Hope this helps .

1

Một số thông tin khác: khi sử dụng RNN (như LSTM) với các chuỗi có độ dài thay đổi, bạn phải thực hiện định dạng dữ liệu của mình.

Khi bạn nhóm các chuỗi để chuyển nó đến phương pháp phù hợp, keras sẽ cố gắng xây dựng một ma trận mẫu, ngụ ý rằng tất cả các chuỗi đầu vào phải có cùng kích thước, nếu không bạn sẽ không có ma trận đúng kích thước.

Có nhiều giải pháp khả thi:

  1. đào tạo mạng của bạn sử dụng mẫu từng cái một (sử dụng fit_generator ví dụ)
  2. pad tất cả dữ liệu của bạn để họ có cùng kích thước
  3. chuỗi nhóm theo kích thước (cuối cùng đệm chúng) và đào tạo nhóm mạng của bạn theo nhóm (một lần nữa sử dụng máy phát điện dựa trên phù hợp)

Giải pháp thứ ba tương ứng với chiến lược phổ biến nhất với va kích thước đáng tin cậy. Và nếu bạn có trình tự pad (giải pháp thứ hai hoặc thứ ba), bạn có thể muốn thêm một lớp mặt nạ làm đầu vào.

Nếu bạn không chắc chắn, hãy thử để in các hình dạng của dữ liệu của bạn

Bạn có thể cần phải nhìn vào (sử dụng thuộc tính hình dạng của mảng NumPy.): https://keras.io/preprocessing/sequence/ (pad_sequences) và https://keras.io/layers/core/#masking

0

Dưới đây là phiên bản làm việc với Keras 2.0.0, sửa đổi mã radix của

from keras.models import Sequential 
from keras.layers.core import Dense, Activation, Dropout 
from keras.layers.recurrent import LSTM 
import numpy as np 

X= np.random.rand(1000) 
y = 2 * X 

poi = int(len(X) * .8) 
X_train = X[:poi] 
y_train = y[:poi] 

X_test = X[poi:] 
y_test = y[poi:] 

# you have to change your input shape (nb_samples, timesteps, input_dim) 
X_train = X_train.reshape(len(X_train), 1, 1) 
# and also the output shape (note that the output *shape* is 2 dimensional) 
y_train = y_train.reshape(len(y_train), 1) 

# Change test data's dimension also. 
X_test = X_test.reshape(len(X_test),1,1) 
y_test = y_test.reshape(len(y_test),1) 


#in_out_neurons = 2 
in_out_neurons = 1 

hidden_neurons = 300 
model = Sequential() 
# model.add(Masking(mask_value=0, input_shape=(input_dim,))) 
# Remove batch_input_shape and add input_shape = (1,1) - Imp change for Keras 2.0.0 
model.add(LSTM(hidden_neurons, return_sequences=False, input_shape=(X_train.shape[1],X_train.shape[2]))) 
# only specify the output dimension 
model.add(Dense(in_out_neurons)) 
model.add(Activation("linear")) 
model.compile(loss="mean_squared_error", optimizer="rmsprop") 
model.summary() 
model.fit(X_train, y_train, epochs=10, validation_split=0.05) 

# calculate test set MSE 
preds = model.predict(X_test).reshape(len(y_test)) 
print(preds) 
MSE = np.mean((preds-y_test)**2) 
print('MSE ', MSE) 
Các vấn đề liên quan