Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

想问下使用from tensorflow.python.kears import xxx 而非使用tensorflow.kears的原因是什么? #510

Open
jjon-boat opened this issue Feb 7, 2023 · 14 comments
Labels

Comments

@jjon-boat
Copy link

jjon-boat commented Feb 7, 2023

问这个问题的起因是我在model.compile时使用了tensorflow.kears的方法:

model.compile(optimizer="adam",
             loss=tf.keras.losses.BinaryCrossentropy(),
             metrics=[tf.keras.metrics.AUC()])

导致后续使用load_model时报错,无论是使用tensorflow.python.keras.models.load_model或是tensorflow.keras.models.load_model
都会报错:AttributeError: 'AUC' object has no attribute '_serialized_attributes'

一通查找,出错的原因应该就是 tensorflow.python.kearstensorflow.kears 混淆使用的问题。
报错的问题我解决了,将from tensorflow import kears改为deepCTR中一致的from tensorflow.python import kears即可。

我的疑问是,为什么要使用tensorflow.python.kears import而非使用tensorflow.kears?

tensorflow.python.的相关资料能查到的不多,我看到的有以下这个:
————————————————————
Anything under tf.python.
is private, intended for development only, rather than for public use.

Importing from tensorflow.python or any other modules (including import tensorflow_core...) is not supported, and can break unannounced.

So, it is suggested not to use anything with tf.python.*.
————————————————————
按照这个说法,是不建议使用的,所以想问问,以上这个说法正确吗(上面这个说法是3年前的一个回答,所以准确性我也无法确保)?以及deepCTR中使用tf.python.*的原因是什么?

@jjon-boat
Copy link
Author

jjon-boat commented Feb 7, 2023

另外我发现,使用

model.compile(optimizer="adam",
             loss='binary_crossentropy',
             metrics=['AUC'])

与使用

model.compile(optimizer="adam",
             loss=tf.keras.losses.BinaryCrossentropy(),
             metrics=[tf.keras.metrics.AUC()])

得到的AUC的评估值也会有差异,一样的数据,第一个为0.66,第二个为0.72,实际上的AUC确实是0.66。所以tensorflow.python.kearstensorflow.kears混淆使用可能会引起较大的误差。

python version 3.7.16
tensorflow version 2.9.1[gpu]
deepctr version 0.9.3

@includeno
Copy link

tensorflow.python.kears是新版本的写法,尤其是tensorflow version2.9之后,去看本地库是没有tensorflow.kears这个包的。

@miluoalbert
Copy link

看到tf社区TensorFlow Team的 @qlzh727 有回复说:

Note that tensorflow.python.keras is not a valid import, and it is accessing legacy code that is about to delete. You should never import that directly.
https://discuss.tensorflow.org/t/attributeerror-kerastensor-object-has-no-attribute-node/7323/8

所以应该不要直接使用tensorflow.python.kears?而应该直接使用tensorflow.kears

@qlzh727
Copy link

qlzh727 commented Feb 21, 2023

the proper imort of keras is tensorflow.keras, or import tensorflow as tf; tf.keras.

tensorflow.python.keras is NOT the correct way to import keras, and it points to the legacy keras code which will be deleted.

@qlzh727
Copy link

qlzh727 commented Feb 21, 2023

For the actual keras code, it stays in keras package, and the init.py under tensorflow will pick it up (rather than pickup from tensorflow/python/keras).

@jjon-boat
Copy link
Author

看到tf社区TensorFlow Team的 @qlzh727 有回复说:

Note that tensorflow.python.keras is not a valid import, and it is accessing legacy code that is about to delete. You should never import that directly.
https://discuss.tensorflow.org/t/attributeerror-kerastensor-object-has-no-attribute-node/7323/8

所以应该不要直接使用tensorflow.python.kears?而应该直接使用tensorflow.kears

我所获的的信息也是尽量不要使用tensorflow.python.kears,但是我看到deepctr -> models 下的模型,都是使用的

from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dense

https://github.com/shenweichen/DeepCTR/blob/master/deepctr/models/deepfm.py
这个正是我的疑问所在,为什么DeepCTR的开发者们选择使用tensorflow.python.keras?

@jjon-boat
Copy link
Author

tensorflow.python.kears是新版本的写法,尤其是tensorflow version2.9之后,去看本地库是没有tensorflow.kears这个包的。

这个有官方的说明吗?

@jjon-boat
Copy link
Author

the proper imort of keras is tensorflow.keras, or import tensorflow as tf; tf.keras.

tensorflow.python.keras is NOT the correct way to import keras, and it points to the legacy keras code which will be deleted.

I agree, but deepctr uses tensorflow.python.keras, so I asked this question.

@miluoalbert
Copy link

miluoalbert commented Feb 22, 2023

看到tf社区TensorFlow Team的 @qlzh727 有回复说:

Note that tensorflow.python.keras is not a valid import, and it is accessing legacy code that is about to delete. You should never import that directly.
https://discuss.tensorflow.org/t/attributeerror-kerastensor-object-has-no-attribute-node/7323/8

所以应该不要直接使用tensorflow.python.kears?而应该直接使用tensorflow.kears

我所获的的信息也是尽量不要使用tensorflow.python.kears,但是我看到deepctr -> models 下的模型,都是使用的

from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dense

https://github.com/shenweichen/DeepCTR/blob/master/deepctr/models/deepfm.py 这个正是我的疑问所在,为什么DeepCTR的开发者们选择使用tensorflow.python.keras?

I guess it is a compatibility problem. DeepCTR want to compatible with both tf 1.x and tf 2.x. Under tf 1.x (such as 1.4), using tensorflow.keras will get a ModuleNotFoundError

@miluoalbert
Copy link

https://github.com/shenweichen/DeepCTR/blob/master/deepctr/models/deepfm.py 这个正是我的疑问所在,为什么DeepCTR的开发者们选择使用tensorflow.python.keras?

I guess it is a compatibility problem. DeepCTR want to compatible with both tf 1.x and tf 2.x. Under tf 1.x (such as 1.4), using tensorflow.keras will get a ModuleNotFoundError

@qlzh727 So, is there any best practice to handle those problem for projects which need to compatible with both tf 1.x and tf 2.x ?

@qlzh727
Copy link

qlzh727 commented Feb 22, 2023

tf.keras was always the public API to access.

For TF 1.4, I don't think keras was part of the TF API at the time, so you can't even access tensorflow.python.keras at the time. If you need the last TF 1.X (eg tf.1.15), you can still use tf.keras API since we never change the public API between tf 1/2.

@archersama
Copy link

tensorflow.python.kears是新版本的写法,尤其是tensorflow version2.9之后,去看本地库是没有tensorflow.kears这个包的。

并不是,tensorflow.python.keras不是正规的写法

@xiahouzuoxin
Copy link

It will raise error using tensorflow.python.keras now, tensorflow/tensorflow#61215

@SyDudulu
Copy link

SyDudulu commented Nov 7, 2023

现在tensorflow 2.14 必须要将tensorflow.python.keras换成tensorflow.keras 否则会报错;另外tensorflow和keras这两个包的关系真是有点混乱。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants