I have installed tf v2.2 (cuda 10.1) to run tutorial_keras.py by tensorlayer, and an error occured. THANKS for possible help!
New Issue Checklist
- [ ] I have read the Contribution Guidelines
- [ ] I searched for existing GitHub issues
Issue Description
Traceback (most recent call last):
File "d:\Test_Programs\PYTHON\tensorlayer_keras.py", line 31, in
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)
Compatibility between tensorflow.keras and Keras. With tensorflow2.0.0 this problem did not occur.
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!
I tested tutorial_keras.py on tensorflow2.2,keras2.4.3 , the code works.
I suspect its due to lower keras and/or tensorflow versions.
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:
It works.
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.
@MoscowskyAnton I'm very happy my solution helps you with your problem, and also thanks for your unique contribution!