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?