2

I am building a RNN to predict a many to one question.

#Input_X:
[
[1,2,3,4,5,6,7,8,9,10],
[2,3,4,5,6,7,8,9,10,11]
]

#Input_Y:
[
11,
12
]
#Each number represent a category

X=np.reshape(Input_X,(len(Input_X), 10, 1))

y=np.utils.to_catgeorical(Input_Y)  #one hot encode,

My model setup:

#####This works 
model=Sequential()
model.add(LSTM(256, input_shape(X.shape[1], X.shape[2])))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentrophy', optimizer='adam', metrics=['accuracy'])
model.fit(X,y, ........) 

I want to try the TimeDistributed(Dense) layer instead, for example: https://keras.io/layers/wrappers/. So I changed above to below:

model=Sequential()
model.add(LSTM(256, input_shape(X.shape[1], X.shape[2])))
model.add(TimeDistributed(Dense(y.shape[1], activation='softmax')))
model.compile(loss='categorical_crossentrophy', optimizer='adam', metrics=['accuracy'])
model.fit(X,y, ........) 

I am getting a AssertionError. Which report the matrix size is not what expected. What steps I missed?

2
  • 1
    time distributed dense is really for many to many Commented Aug 2, 2017 at 19:53
  • if I modify my my input_y to be length of 2 for each of the output. I still get the AssertionError Commented Aug 3, 2017 at 15:06

2 Answers 2

1

I think you need to add return_sequences=True to the LSTM cell

```
model=Sequential()
model.add(LSTM(256, return_sequences=True, input_shape(X.shape[1], X.shape[2])))
model.add(TimeDistributed(Dense(y.shape[1], activation='softmax')))
model.compile(loss='categorical_crossentrophy', optimizer='adam', metrics=['accuracy'])
model.fit(X,y, ........) 

```

Sign up to request clarification or add additional context in comments.

1 Comment

Nope, when using return_sequences=True I get ValueError: Error when checking target: expected time_distributed_11 to have 3 dimensions, but got array with shape (2, 50)
0

return_sequences=True works for me.

In the OP's question, y.shape is (2,1) which has 2 samples and only 1 feature , so it's not suited for Many to Many model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.