InvalidArgumentError: You must feed a value for placeholder tensor 'time_distributed_1_target' with dtype float and shape

Viewed 61

I am trying to run a code. Some parts of the code are provided below:

def conditional(x_true, x_pred):
    #most_likely = K.argmax(x_true)
    most_likely = tf.argmax(x_true)
    
    most_likely = tf.reshape(most_likely,[-1]) # flatten most_likely
    ix2 = tf.expand_dims(tf.gather(ind_of_ind_K, most_likely),1) # index ind_of_ind with res
    ix2 = tf.cast(ix2, tf.int32) # cast indices as ints 
    M2 = tf.gather_nd(masks_K, ix2) # get slices of masks_K with indices
    M3 = tf.reshape(M2, [-1,MAX_LEN,DIM]) # reshape them
    P2 = tf.math.multiply(tf.math.exp(x_pred),tf.cast(M3,tf.float32)) # apply them to the exp-predictions
    P2 = tf.math.divide(P2,tf.math.reduce_sum(P2,axis=-1,keepdims=True)) # normalize predictions
    return P2

def vae_loss(x, x_decoded_mean):
    x_decoded_mean = conditional(x, x_decoded_mean)
    x = tf.keras.layers.Flatten()(x)
    x_decoded_mean = tf.keras.layers.Flatten()(x_decoded_mean)
    
    xent_loss = max_length * objectives.binary_crossentropy(x, x_decoded_mean)
    
    kl_loss = - 0.5 * tf.reduce_mean(1 + z_log_var - tf.math.square(z_mean) - tf.math.exp(z_log_var), axis = -1)
    kl_loss = - 0.5 * tf.reduce_mean(1 + z_log_var - tf.math.square(z_mean) - tf.math.exp(z_log_var), axis = -1)
    
    return xent_loss + kl_loss

But it rises an error which refers to the line:

x_decoded_mean = conditional(x, x_decoded_mean)

The error is as below:

tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'time_distributed_1_target' with dtype float and shape [?,?,?]
[[{{node time_distributed_1_target}}]]

It seems that there must be value for x_decoded_mean , but I do not know why this error appears and how to solve it?

After dealing with the code, I realized that when I comment the line x_decoded_mean = conditional(x, x_decoded_mean), the code starts running, but the accuracy will not be correct. In addition, commenting the line P2=tf.math.divide(P2,tf.math.reduce_sum(P2,axis=-1,keepdims=True)) # normalize predictions, does not remove the error. But replacing P2 = tf.math.multiply(tf.math.exp(x_pred),tf.cast(M3,tf.float32)) # apply them to the exp-predictions with P2=tf.math.exp(x_pred), removes the error.

Hence, it means that the error rises from the "conditional" function and M3. I do not know exactly what this function does. Could you please help me for solving this bug?

By doing further investigation, I realized that this error is probably because of the graph architecture. The variables which are provided below are defined at the beginning of the code:

import eq_grammar as G

masks_K      = tf.Variable(G.masks)
ind_of_ind_K = tf.Variable(G.ind_of_ind)

Then, the RNN is built, but the function conditional is defined in the middle of the code. In this function, the line ix2 = tf.expand_dims(tf.gather(ind_of_ind_K, most_likely),1) # index ind_of_ind with res adds another graph to the current RNN graph which rises this error. To solve it, I added the following lines, before creating the RNN network:

tf.reset_default_graph()
tf.compat.v1.disable_eager_execution()

But, now, the error is changed as below:

ValueError: Tensor ("loss/time_distributed_1_loss/Reshape_0") must be from the same graph as Tensor ("Variable_1:0")

I really need the conditional function in the code, and I cannot remove it from the graph. How can I solve it?

1 Answers

As it is described here: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Lambda, using tf.keras.layers with other tf commands like tf.Variable can easily lead to bugs. Therefore, to solve the bugs, using subclasses are encouraged. So, we can write it as below:

class ConditionalLayer(tf.keras.layers.Layer):
    def __init__(self):
        super(ConditionalLayer, self).__init__(name='conditional_layer')
        self.masks_K = tf.Variable(G.masks)
        self.ind_of_ind_K = tf.Variable(G.ind_of_ind)
    
    

    def call(self, inputs):
        x_true = inputs[0]
        x_pred = inputs[1]

        most_likely = tf.math.argmax(x_true,-1)
        most_likely = tf.reshape(most_likely,[-1])
        ix2 = tf.expand_dims(tf.gather(self.ind_of_ind_K, most_likely),1) # index ind_of_ind with res
        ix2 = tf.cast(ix2, tf.int32) # cast indices as ints 
        M2 = tf.gather_nd(self.masks_K, ix2) # get slices of masks_K with indices
        M3 = tf.reshape(M2, [-1,MAX_LEN,DIM])#+tf.cast(tf.convert_to_tensor(0.01),tf.float64) # reshape them
        P2 = tf.math.multiply(tf.math.exp(x_pred),tf.cast(M3,tf.float32)) # apply them to the exp-predictions

        P2 = tf.math.divide(P2,tf.math.reduce_sum(P2,axis=-1,keepdims=True)) # normalize predictions
       
        return P2   

Then we can call the subclass as below:

conditional = ConditionalLayer()
x_decoded_mean = conditional([x, x_decoded_mean])

This method solved the error.

Related