tensorlayer / TensorLayer

Deep Learning and Reinforcement Learning Library for Scientists and Engineers
http://tensorlayerx.com
Other
7.31k stars 1.61k forks source link

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 suntaochun opened 4 years ago

suntaochun commented 4 years ago

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

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 commented 4 years ago

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

suntaochun commented 4 years ago

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 commented 4 years ago

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

Laicheng0830 commented 4 years ago

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

suntaochun commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

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