philipperemy / cond_rnn

Conditional RNNs for Tensorflow / Keras.
MIT License
225 stars 32 forks source link

No more than 10 conditional features #39

Closed hsinyuku closed 1 year ago

hsinyuku commented 1 year ago

I got "IndexError: list index out of range" when there is more than 10 conditional features. In the following code, as long as c11 is included, the model will throw the IndexError. image

hsinyuku commented 1 year ago

The following is the complete code modified by the test_con_rnn.py.

def create_conditions(NUM_SAMPLES, NUM_CLASSES):
    conditions = np.zeros(shape=[NUM_SAMPLES, NUM_CLASSES])
    for i, kk in enumerate(conditions):
             kk[i % NUM_CLASSES] = 1
    return conditions
def main():
    i = Input(shape=[time_steps, input_dim], name='input_0')
    c1 = Input(shape=[num_classes], name='input_1')
    c2 = Input(shape=[num_classes], name='input_2')
    c3 = Input(shape=[num_classes], name='input_3')
    c4 = Input(shape=[num_classes], name='input_4')
    c5 = Input(shape=[num_classes], name='input_5')
    c6 = Input(shape=[num_classes], name='input_6')
    c7 = Input(shape=[num_classes], name='input_7')
    c8 = Input(shape=[num_classes], name='input_8')
    c9 = Input(shape=[num_classes], name='input_9')
    c10 = Input(shape=[num_classes], name='input_10')
    # c11 = Input(shape=[num_classes], name='input_11')
        # add the condition tensor here.
    x = ConditionalRecurrent(LSTM(num_cells, return_sequences=True, name='cond_rnn_0'))([i,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10])
        # and here too.
    x = ConditionalRecurrent(LSTM(num_cells, return_sequences=False, name='cond_rnn_1'))([x,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10])
    x = Dense(units=num_classes, activation='softmax')(x)
    model = Model(inputs=[i,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10], outputs=[x])

        # Define data.
    test_inputs = np.random.uniform(size=(num_samples, time_steps, input_dim))
    test_con = test_targets = create_conditions(num_samples, num_classes)
    #print(test_con)

    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    model.fit(verbose=2, x = [test_inputs,test_con,test_con,test_con,test_con
                              ,test_con,test_con,test_con,test_con,test_con,test_con], y=test_targets, epochs=10)

    pred1 = model.predict([test_inputs,test_con,test_con,test_con,test_con
                           ,test_con,test_con,test_con,test_con,test_con,test_con])
    #print(pred1)
num_samples = 10
    input_dim = 1
    num_classes = 3
    time_steps = 10
    num_cells = 6

if __name__ == '__main__':
    main()
philipperemy commented 1 year ago

@hsinyuku oh yeah in the code I put 10 as the max number of conditions. I can increase it to 20.

hsinyuku commented 1 year ago

Thank you so much for replying so fast! May I ask what is the reason to limit it to a certain amount? Would too much condition hinder the performance? Thank you.

If possible, it would be great to increase it to 20, as I am trying out different combination of data Than you. :)

Philippe Rémy @.***>於 2022年12月19日 週一,13:28寫道:

@hsinyuku https://github.com/hsinyuku oh yeah in the code I put 10 as the max number of conditions. I can increase it to 20.

— Reply to this email directly, view it on GitHub https://github.com/philipperemy/cond_rnn/issues/39#issuecomment-1357597817, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANRHJLCZ2JZ6YHOT44J76KTWOBIGZANCNFSM6AAAAAATDFRCUI . You are receiving this because you were mentioned.Message ID: @.***>

-- Viel Grüße, Hsin-Yu, Ku

philipperemy commented 1 year ago

@hsinyuku it's purely arbitrary. I will increase it now. Hang on.

philipperemy commented 1 year ago

I pushed in in version 3.2.0 ;)

philipperemy commented 1 year ago

https://github.com/philipperemy/cond_rnn/commit/53a23fb8e990bbc05cbcd2b980a02915c070385e

philipperemy commented 1 year ago

Max is now 20.

hsinyuku commented 1 year ago

A stupid question: How can I get this new version. I am using !pip install in Colab. I re-installed but nothing changed

philipperemy commented 1 year ago
!pip install --upgrade cond-rnn

should work.

hsinyuku commented 1 year ago

I tried first uninstall and install with upgrade as you suggested already. In the log it shows the following. You can see in the last 3 lines that it started by using 3.2.0 and then going back to 3.1.1. In the end, the installed version is still 3.1.1 when I checked by !pip show cond-rnn

Found existing installation: cond-rnn 3.1.1 Uninstalling cond-rnn-3.1.1: Would remove: /usr/local/lib/python3.8/dist-packages/cond_rnn-3.1.1.dist-info/ /usr/local/lib/python3.8/dist-packages/cond_rnn/ Proceed (y/n)? y Successfully uninstalled cond-rnn-3.1.1 Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting cond-rnn Using cached cond_rnn-3.2.0-py3-none-any.whl (7.1 kB) Requirement already satisfied: protobuf<=3.20 in /usr/local/lib/python3.8/dist-packages (from cond-rnn) (3.19.6) Using cached cond_rnn-3.1.1-py3-none-any.whl

philipperemy commented 1 year ago

@hsinyuku my bad I think I know why. Give me a few minutes.

philipperemy commented 1 year ago

Okay I pushed 3.2.1. Make sure your version is not python 3.11. I tested it with python3.8 and it works great.

!pip install --upgrade cond-rnn
hsinyuku commented 1 year ago

Success !!! Thank you. I really appreciate your prompt response <3 Thank you also for the great package with clear explanation, example and easy to be used design. Love it.

philipperemy commented 1 year ago

@hsinyuku Happy to hear that!! Thank you!!