Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

Simple LSTM ValueError Python

I want to LSTM with 3 Dimension data(train_value ) , 1 Dimensiton labeled data(train_label_list). But It has Error(ValueError). I cant fix it

from keras.models import Sequential
from keras.layers import Dense, LSTM, RepeatVector, TimeDistributed

train_value = [[[0.4, 0.1, 0.2],[0.2, 0.1,0.4]], [[0.4, 0.1, 0.2],[0.2, 0.1,0.4]]]
train_label_list = [1.0, 2.0]

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

x_train = x_train.reshape((x_train.shape[0], x_train.shape[1], x_train.shape[2]))
y_train = y_train.reshape(y_train.shape[0])

model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(2, 3)))
model.add(RepeatVector(1))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epochs=1000, verbose=0)

test_value = [[0.2, 0.1, 0.2],[0.2, 0.3,0.4]]

x_test = np.array(test_value)
x_test = x_test.reshape((x_test.shape[0], x_test.shape[1], 1))

yhat = model.predict(x_test)

print(yhat)

Error

ValueError: Input 0 is incompatible with layer sequential: expected shape=(None, None, 3), found shape=[None, 3, 1]
question from:https://stackoverflow.com/questions/65937289/simple-lstm-valueerror-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1st thing:

Your x_train and x_test should have same dimension. You have x_train is 3d array but your x_test is 2d array which is the reason for error. Check it by using print(x_train.shape) and print(x_test.shape)

You should add extra [] to your x_test to make it 3d array.

test_value =[[[0.2, 0.1, 0.2],[0.2, 0.3,0.4]]]

Also you dont need to reshape x_test

So comment this line,

x_test = x_test.reshape((2, x_test.shape[1], x_test.shape[2]))

Your full code should look like this

from keras.models import Sequential
from keras.layers import Dense, LSTM, RepeatVector, TimeDistributed

train_value = [[[0.4, 0.1, 0.2],[0.2, 0.1,0.4]], [[0.4, 0.1, 0.2],[0.2, 0.1,0.4]]]
train_label_list = [1.0, 2.0]

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

x_train = x_train.reshape((x_train.shape[0], x_train.shape[1], x_train.shape[2]))
y_train = y_train.reshape(y_train.shape[0])

model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(2, 3)))
model.add(RepeatVector(1))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epochs=1000, verbose=0)

test_value =[[[0.2, 0.1, 0.2],[0.2, 0.3,0.4]]

print(x_train.shape) #shape of x_train
x_test = np.array(test_value)
# x_test = x_test.reshape((2, x_test.shape[1], x_test.shape[2]))
print(x_test.shape) #shape of x_test
yhat = model.predict(x_test)

print(yhat)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...