IndexError: list index out of range when doing model.fit on keras

1,809 views
Skip to first unread message

emnaj...@gmail.com

unread,
Sep 7, 2018, 4:07:30 AM9/7/18
to Keras-users

Hi everyone,

I have written my own custom Loss function. I wanted to be calculated per batch. 
def calculate_additional_loss(y_true,y_pred):
    #additional loss
    x_decoded_normalized = tf.round(original_dim* y_pred)
    error = tf.constant(0, dtype= tf.float32)
    additional_loss= tf.constant(0, dtype= tf.float32)
    final_loss = 0 * mse(inputs,outputs)
        for k in range(batch_size):
        #add padding
        reshaped_elem_1 = K.reshape(x_decoded_normalized[k], [DIM,DIM])
        
        a = K.reshape(reshaped_elem_1[:,DIM-1], [DIM,1])
        b = K.reshape(reshaped_elem_1[:,1], [DIM,1])

        reshaped_elem_1 = tf.concat ([b,reshaped_elem_1], axis= 1)
        reshaped_elem_1 = tf.concat ([reshaped_elem_1,a], axis= 1)
        #print('here', reshaped_elem_1.shape)
        
        c= K.reshape(reshaped_elem_1[DIM-1,:], [1,DIM+2])
        d= K.reshape(reshaped_elem_1[1,:], [1,DIM+2])
        reshaped_elem_1 = tf.concat ([d,reshaped_elem_1],axis=0)
        reshaped_elem_1 = tf.concat ([reshaped_elem_1,c],axis=0)

        for (i,j) in range(reshaped_elem_1.shape[0],reshaped_elem_1.shape[1]):
            error = tf.add(error, tf.pow((reshaped_elem_1[i,j]-reshaped_elem_1[i,j+1]),-2), tf.pow((reshaped_elem_1[i,j]-reshaped_elem_1[i,j-1]),-2), tf.pow((reshaped_elem_1[i,j]-reshaped_elem_1[i-1,j]),-2), tf.pow((reshaped_elem_1[i,j]-reshaped_elem_1[i+1,j]),-2))
                        #print('error', error)
        #print('additional_loss', K.get_value(additional_loss))
        additional_loss = tf.add(additional_loss, tf.divide(error, original_dim))
      final_loss += tf.divide(additional_loss, batch_size)
    #sess.close()
    return final_loss

For training the model, I am using this code :
sess = tf.Session()
K.set_session(sess)
K.set_learning_phase(0)

models = (encoder, decoder)
vae.compile(loss=[calculate_additional_loss], optimizer='adam', metrics= ['acc'])

vae.summary()

plot_model(vae,to_file='vae_mlp.png',show_shapes=True)
vae.fit(x_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, None), verbose = 1, callbacks=[CustomMetrics()])
vae.save_weights('vae_mlp_mnist.h5')

My model is variational autoencoder. The encoder architecture is this one :

vae_mlp_encoder (1).png


The decoder architecture is this one:

The error I am getting while running the model is :
Train on 20060 samples, validate on 2000 samples
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-84-9ba5be4c3154> in <module>()
     36 
     37 plot_model(vae,to_file='vae_mlp.png',show_shapes=True)
---> 38 vae.fit(x_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, None), verbose = 1)
     39 #metrics = vae.evaluate(x=x_train, y=None, batch_size=batch_size, verbose=1)
     40 #print(metrics)

/opt/aiml4it/anaconda/3-5.2.0-generic/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1040                                         initial_epoch=initial_epoch,
   1041                                         steps_per_epoch=steps_per_epoch,
-> 1042                                         validation_steps=validation_steps)
   1043 
   1044     def evaluate(self, x=None, y=None,

/opt/aiml4it/anaconda/3-5.2.0-generic/lib/python3.6/site-packages/keras/engine/training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
    135     indices_for_conversion_to_dense = []
    136     for i in range(len(feed)):
--> 137         if issparse(ins[i]) and not K.is_sparse(feed[i]):
    138             indices_for_conversion_to_dense.append(i)
    139 

IndexError: list index out of range

I cannot find an explanation for this error. It doesn't show as well in which line. Does anyone have an idea about it please ?
Thank you in advance.

jeetu...@gmail.com

unread,
Sep 20, 2018, 3:56:01 PM9/20/18
to Keras-users

Any progress with this ??. I am also getting error for this same thing

Ted Yu

unread,
Sep 20, 2018, 4:06:47 PM9/20/18
to emnaj...@gmail.com, keras...@googlegroups.com
Is it possible to show your encoder / decoder code ?

Thanks

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/37b3ddf4-82f6-42c3-aec4-45a53d647b60%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

emnaj...@gmail.com

unread,
Sep 21, 2018, 7:39:57 AM9/21/18
to Keras-users
I tried to rewrite the additional loss function and it worked out for me.
This is the new code.
i = tf.Variable(0)
k = tf.Variable(0)
l = tf.Variable(0)
error = tf.Variable(initial_value=0,dtype=tf.float32)
error_1 = tf.Variable(initial_value=0,dtype=tf.float32)
one = tf.constant(1, dtype=tf.float32)
final_loss = tf.Variable(0)


def cond_1(k,l, sol, error):
    return tf.logical_and((tf.less(k, 13)),(tf.less(l, 13)))
def body_1(k,l, sol,error):
    elem_1 = tf.abs(sol[k,l]-sol[k,l+1])
    elem_2 = tf.abs(sol[k,l]-sol[k,l-1])
    sum_1 = tf.add(elem_1, elem_2)
    elem_3 = tf.abs(sol[k,l]-sol[k-1,l])
    elem_4 = tf.abs(sol[k,l]-sol[k+1,l])
    sum_2 = tf.add(sum_1, elem_3)
    sum_3 = tf.add(sum_2,elem_4)
    error = tf.add(error,tf.add(sum_3,one))
    k = tf.add(k, 1)
    l = tf.add(l, 1)
    return [k,l, sol, error] 


def cond(i, sol, error_1):
    return tf.less(i, 10)
def body(i, sol,error_1):
    
    original_reshaped_elem = sol[i]
    original_reshaped_elem = tf.reshape(original_reshaped_elem, [DIM,DIM])
    
    a = tf.reshape(original_reshaped_elem[:,DIM-1], [DIM,1])
    b = tf.reshape(original_reshaped_elem[:,1], [DIM,1])

    original_reshaped_elem = tf.concat ([b,original_reshaped_elem], axis= 1)
    original_reshaped_elem = tf.concat ([original_reshaped_elem,a], axis= 1)

    c= tf.reshape(original_reshaped_elem[DIM-1,:], [1,DIM+2])
    d= tf.reshape(original_reshaped_elem[1,:], [1,DIM+2])
    original_reshaped_elem = tf.concat ([d,original_reshaped_elem],axis=0)
    reshaped_elem_extended = tf.concat ([original_reshaped_elem,c],axis=0)
    result = tf.while_loop(cond_1, body_1, [k,l, reshaped_elem_extended, error])
    error_1 = tf.divide(result[3], 144)
    i = tf.add(i, 1)
    return [i, sol, error_1]



and this is where I am calling it in the keras model

result = tf.while_loop(cond, body, [i, outputs, error_1])

final_loss = tf.divide(result[2], 10)

emnaj...@gmail.com

unread,
Sep 21, 2018, 7:41:16 AM9/21/18
to Keras-users
Encoder code :
# build encoder model

inputs = Input(shape=input_shape, name='encoder_input')

x = Dense(intermediate_dim, activation='relu')(inputs)

z_mean = Dense(latent_dim, name='z_mean')(x)

z_log_var = Dense(latent_dim, name='z_log_var')(x)

Decoder part:
latent_inputs = Input(shape=(latent_dim,), name='z_sampling')

x = Dense(intermediate_dim, activation='relu')(latent_inputs)

outputs = Dense(original_dim, activation='relu')(x)
Reply all
Reply to author
Forward
0 new messages