shenweichen/DeepCTR

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

jjon-boat opened this issue · 14 comments

问这个问题的起因是我在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.*的原因是什么?

另外我发现,使用

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

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

看到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

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.

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).

看到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?

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

这个有官方的说明吗?

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.

看到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

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 ?

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.

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

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

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

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