tensorlayer / TensorLayer

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

RuntimeError using SubpixelConv1d: SubpixelConv1d._PS is a private method #1024

Closed Rafagd closed 5 years ago

Rafagd commented 5 years ago

New Issue Checklist

Issue Description

The SubpixelConv1d class decorates the method _PS as @private_method, but when its own forward method calls the _PS function, the decorator for some reason gets confused and can't detect it's a valid use of the method and raises the exception.

Reproducible Code

#!/usr/bin/python3

import tensorflow as tf
import tensorlayer as tl
import numpy as np

inputs = tl.layers.Input((1, 2, 2))
prev   = tl.layers.SubpixelConv1d(2, in_channels=2)(inputs)
model = tl.models.Model(inputs, prev)

train_batch = np.array([1, 2, 3, 4])
train_batch = train_batch.reshape((1,2,2))
valid_batch = train_batch

tl.utils.fit(model,
    train_op=tf.optimizers.Adam(learning_rate=0.0001),
    cost=tl.cost.cross_entropy,
    X_train=train_batch, y_train=train_batch,
    acc=tf.compat.v1.metrics.accuracy, batch_size=len(train_batch), n_epoch=20, X_val=valid_batch, y_val=valid_batch, eval_train=True,
)
ChrisWu1997 commented 5 years ago

Hi, @Rafagd, thanks for questions!

I found this might be caused by the incompatibility of @tf.function and @private_method. The function tl.utils.fit internally calls the function tl.utils._train_step which is by default decorated with tf.function for speedup. I tried two things:

The reason why @tf.function conflicts with @private_method is still unclear to me. Currently, I think the simplest way to solve this issue is to remove the @private_method decorator of tl.layers.SubpixelConv1d. @zsdonghao @JingqingZ Is it OK to do this? What's the initial reason to decorate it as private_method?

BTW, the above script is not runnable, try this one:

#!/usr/bin/python3

import tensorflow as tf
import tensorlayer as tl
import numpy as np

inputs = tl.layers.Input((1, 2, 2))
prev   = tl.layers.SubpixelConv1d(2, in_channels=2)(inputs)
model = tl.models.Model(inputs, prev)

train_batch = np.array([1, 2, 3, 4]).reshape((1,2,2)).astype(np.float)
train_batch_y = train_batch.reshape((1, 4, 1))
valid_batch = train_batch
valid_batch_y = train_batch_y

tl.utils.fit(model,
    train_op=tf.optimizers.Adam(learning_rate=0.0001),
    cost=tf.losses.MeanSquaredError(),
    X_train=train_batch, y_train=train_batch_y,
    batch_size=len(train_batch), n_epoch=20, X_val=valid_batch, y_val=valid_batch_y, eval_train=True,
)
JingqingZ commented 5 years ago

I can't see any problem to remove the @private_method of SubpixelConv1d.

Rafagd commented 5 years ago

Thanks for the correction! I was in a bit of a hurry when I was writing that minimal example.

ChrisWu1997 commented 5 years ago

@Rafagd The problem is fixed by PR #1025. Try install the latest version of TensorLayer:

pip3 install https://github.com/tensorlayer/tensorlayer/archive/master.zip