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

I have installed tf v2.2 (cuda 10.1) to run tutorial_keras.py by tensorlayer, and an error occured. THANKS for possible help! #1092

Open
2 tasks
suntaochun opened this issue Jun 26, 2020 · 7 comments

Comments

@suntaochun
Copy link

New Issue Checklist

Issue Description

Traceback (most recent call last):
File "d:\Test_Programs\PYTHON\tensorlayer_keras.py", line 31, in
network = tl.models.Model(inputs=ni, outputs=nn)
File "D:\Anaconda\lib\site-packages\tensorlayer\models\core.py", line 213, in init
if isinstance(check_argu, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(check_argu):
AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'

Reproducible Code

  • Which OS are you using ? Windows 10
  • Please provide a reproducible code of your issue. Without any reproducible code, you will probably not receive any help.

My code is :

# keras layers
layers = [
    tf.keras.layers.Dropout(0.8),
    tf.keras.layers.Dense(800, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(800, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(10, activation='linear')
]
keras_block = tf.keras.Sequential(layers)
# in order to compile keras model and get trainable_variables of the keras model
_ = keras_block(np.random.random([batch_size, 784]).astype(np.float32))

# build tl model using keras layers
ni = Input([None, 784], dtype=tf.float32)
nn = Lambda(fn=keras_block, fn_weights=keras_block.trainable_variables)(ni)
network = tl.models.Model(inputs=ni, outputs=nn)
print(network)
@Laicheng0830
Copy link
Member

Compatibility between tensorflow.keras and Keras.
With tensorflow2.0.0 this problem did not occur.

@suntaochun
Copy link
Author

Hi. Is there any future updated version to solve this problem of compatibility? Or is there any possibility to use TensorFlow.Keras instead of Keras or vice versa to modify the code to use in TensorFlow v2.2 (because I realy love the convenience of using TensorLayer)? Big THANKS!

@Laicheng0830
Copy link
Member

I tested tutorial_keras.py on tensorflow2.2,keras2.4.3 , the code works.

@Laicheng0830
Copy link
Member

I suspect its due to lower keras and/or tensorflow versions.

@suntaochun
Copy link
Author

Hi @Laicheng0830 . Thanks for your help. I have already tried your solution (upgrading Keras to v2.4.3) but failed.
However, I find what might be the crux of this problem. I track the problem and find that the basic for Model to generically be used in Network is that the core.py introduces Layer class, and in the 213 line of that file the Model class tries to identify whether the Layer is the instance of a tensor of Tensorflow using tf_ops._TensorLike or tf_ops._is_dense_tensor_like where tf_ops is tensorflow.python.ops.
However the tensorflow.python.ops, or tf_ops doesn't include _TensorLike attribute in version 2.20 (thanks to Kite's AI search I search for all the possible method to identify the type of tensor), so I modify the code in core.py such that:

if tf_ops.is_dense_tensor_like(check_argu): pass

instead of the original one. And the code works.
I don't know whether this is the root of compatibility for this problem or just a bug. However I think maybe for user of v2.2 this is a universal problem because of the change in its core ops APIs. And I may be happy if it helps and you helped me with this issue.

Appendix:
image
It works.

@MoscowskyAnton
Copy link

Hi @suntaochun!
Super thanks for your solution, it helps me too.
Just to clarify:
Need to change tensorlayers core.py file, which is (for me)
/home/user/.local/lib/python3.8/site-packages/tensorlayer/models/
213 line: change on if tf_ops.is_dense_tensor_like(check_argu):
223 line: change on if not tf_ops.is_dense_tensor_like(check_argu[idx]):
It allowed me to use tensorlayer 2.2.3 with tensorflow-gpu 2.4.0 and cuda 11.0. Tested on RL examples.

@suntaochun
Copy link
Author

@MoscowskyAnton I'm very happy my solution helps you with your problem, and also thanks for your unique contribution!

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

No branches or pull requests

3 participants