gpascualg / CrossGrad

Tensorflow: Generalizing Across Domains via Cross-Gradient Training
15 stars 0 forks source link

One question #1

Open BX-xb opened 3 years ago

BX-xb commented 3 years ago

Hi, thank you for your code! I want to ask a question,what is the "latent_space_dimensions"? I did not find this variable in the paper.

gpascualg commented 3 years ago

Hello,

It's been a while since I did this, so I don't have the details that clear. In the paper they extracted $\theta^1_d$ from the latent space, but they don't define its dimensionality. The parameter latent_space_dimensions control exactly that, the shape of this variable. It's also the dimension of the "embedding", the very last layer before the logits.

See this line here: https://github.com/gpascualg/CrossGrad/blob/master/crossgrad.py#L21 The output of the latent space is [batch, latent_space_dimensions]

Likwise, it is used again in the domain transformation: https://github.com/gpascualg/CrossGrad/blob/master/crossgrad.py#L28

I chose to make the domain and label classifiers both work with the same dimensionality via this parameter, so that when concatenating (features = tf.concat((features, latent), axis=-1) in the .ipynb inside label_fn) the final size if [batch, latent_space_dimensions * 2].

Either case, that is was my choice, I don't recall the paper specifically mentioning dimensions. I hope this clears a bit the param.

BX-xb commented 3 years ago

Thank you for your help! When I run your example CrossGrad.ipynb,the result only has loss, but does not have accuracy, so how can I get the accuracy? And can the code only apply to the domain number is 2? Best wishes!

发送自 Windows 10 版邮件应用

发件人: Guillem Pascual Guinovart 发送时间: 2020年12月23日 20:14 收件人: gpascualg/CrossGrad 抄送: BX-xb; Author 主题: Re: [gpascualg/CrossGrad] One question (#1)

Hello, It's been a while since I did this, so I don't have the details that clear. In the paper they extracted $\theta^1_d$ from the latent space, but they don't define its dimensionality. The parameter latent_space_dimensions control exactly that, the shape of this variable. It's also the dimension of the "embedding", the very last layer before the logits. See this line here: https://github.com/gpascualg/CrossGrad/blob/master/crossgrad.py#L21 The output of the latent space is [batch, latent_space_dimensions] Likwise, it is used again in the domain transformation: https://github.com/gpascualg/CrossGrad/blob/master/crossgrad.py#L28 I chose to make the domain and label classifiers both work with the same dimensionality via this parameter, so that when concatenating (features = tf.concat((features, latent), axis=-1) in the .ipynb inside label_fn) the final size if [batch, latent_space_dimensions * 2]. Either case, that is was my choice, I don't recall the paper specifically mentioning dimensions. I hope this clears a bit the param. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

gpascualg commented 3 years ago

I'm not using TF 1.x anymore (switched to 2.x), so I don't know the exact steps, but it would be something along the lines of using these outputs here: https://github.com/gpascualg/CrossGrad/blob/master/crossgrad.py#L75

The variable d1 has the predicted classes according to the domain classifier, while l1 has the predicted classes according to the labels classifier. You can compare either of them with labels and obtain the classification accuracy on that batch, ie, tf.reduce_sum(tf.cast(tf.equal(d1, labels), tf.float32)) / tf.cast(tf.shape(labels)[0], tf.float32)

I don't understand what you mean by the domain number is 2, but the code should be generic enough to admit any/all combinations of input shapes, outputs and number of classes.

BX-xb commented 3 years ago

I am confused of the relationship between d1 and d2 , which is the predicted classes according to the domain classifier, can you explain it a little bit more clearly?

BX-xb commented 3 years ago

https://github.com/gpascualg/CrossGrad/blob/2cb478371db4b35632d1436dccb54de7eb1928f6/crossgrad.py#L125 And when I test the new domain samples, the result of d1 must be wrong, why return d1?

gpascualg commented 3 years ago

I'm sorry but it's been too long since I read that paper and implemented the code. If I'm not wrong, which I might be, d1 is the original forward pass with the input data, while d2 is the forward pass with the modified input data taking into account the gradient on d1.

As for the return, I return all the data I deem relevant for inspection. Returning it allows you to manually retrieve the values, summarize them, plot, or whatever you deem necessary. There is no real implication in retuning it.