jinze huang
jinze huang

Reputation: 31

how to calculate the derivate value of Multi-input models in keras by with tensorflow backend

here is my nueral network. Use LSTM output as dense layer input.

My question is: I want to calculate the derivation of "time input" and "dense_input". Before asking question, I search the soluatoin of calculaing jacobian matrix by keras function.

After running it, But I got this error:

File "\keras\backend\tensorflow_backend.py", line 2614, in _call dtype=tensor.dtype.base_dtype.name)) AttributeError: 'list' object has no attribute 'dtype'

Here is my simple version:

from keras.models import *
from keras.layers import *
import keras.backend as K
import pandas as pd
from keras import optimizers
def get_model(timestamp, features):
    time_input = Input(shape=(timestamp, features,), name='time_input')
    lstm_out = LSTM(4)(time_input)

    dense_hidden_units = 2

    dense_input_layer = Input(shape=(dense_length,), name='dense_input_layer')
    final_input_layer = concatenate([lstm_out, dense_input_layer])

    # Disable biases in the hidden layer
    dense_1 = Dense(units=dense_hidden_units, use_bias=False, activation='sigmoid')(final_input_layer)
    # Disable bias in output layer
    output_layer = Dense(units=1, use_bias=False, name='final_output')(dense_1)

    model = Model(
        inputs=[time_input, dense_input_layer],
        outputs=output_layer
    )
    print(model.summary())

    return model
if __name__ == '__main__':
    timestamp = 3
    features = 1
    dense_length = 3

    temp_data = pd.DataFrame([
        [1, 2, 3, 2, 3, 4],
    ])

    time_data = temp_data.values.reshape(-1, timestamp, features)
    dense_data = temp_data.values.reshape(-1, dense_length)
    target_data = np.array([1, 2])
    print(time_data.shape)
    print(dense_data.shape)
    print(target_data.shape)

    model = get_model(
        timestamp, features
    )

    Ada = optimizers.Adagrad(lr=0.09, epsilon=1e-04)
    model.compile(loss='mse', optimizer=Ada, metrics=['mse'])

    model.fit(
        {
            'time_input': time_data,
            'dense_input_layer': dense_data,
        },
        {
            'final_output': target_data
        },
        epochs=1, batch_size=1
    )

    time_input = model.get_layer('time_input').input
    GPP_input_layer = model.get_layer('dense_input_layer').input

    J = K.gradients(model.output, [time_input, GPP_input_layer])
    jacobianTime = K.function([[time_input, GPP_input_layer], K.learning_phase()], J)

    deriRes = jacobianTime([time_data, dense_data]) # this line throw exception

    print(deriRes[0])

Thanks for help!

Upvotes: 1

Views: 120

Answers (1)

VegardKT
VegardKT

Reputation: 1246

You have an extra set of brackets.

jacobianTime = K.function([[time_input, GPP_input_layer], K.learning_phase()], J)

to

jacobianTime = K.function([time_input, GPP_input_layer, K.learning_phase()], J)

I was able to run your code like this at least.

Upvotes: 1

Related Questions