inverse_transform a tensorflow variable: TypeError: __array__() takes 1 positional argument but 2 were given

Issue

I keep getting this error when trying to call inverse_transform on my tf variable:

x_optimal_tr = PredictorScaler.inverse_transform(x_optimal)
~/.pyenv/versions/3.8.2/envs/python3env/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     81 
     82     """
---> 83     return array(a, dtype, copy=False, order=order)
     84 
     85 

TypeError: __array__() takes 1 positional argument but 2 were given

Initially, I trained a NN using MinMaxScaler both for y and X

from sklearn.preprocessing import MinMaxScaler
PredictorScaler=MinMaxScaler()
TargetVarScaler=MinMaxScaler()

PredictorScalerFit=PredictorScaler.fit(X)
TargetVarScalerFit=TargetVarScaler.fit(y)

X=PredictorScalerFit.transform(X)
y=TargetVarScalerFit.transform(y)

Now, I wanted to pass the model into the optimizer to find the X vector that would minimize the NN function, for that I initialized X as an initial guess which is just one of my data points (my data is tabular)

x_optimal = tf.Variable(X[600:601, :])
x_optimal
<tf.Variable 'Variable:0' shape=(1, 10) dtype=float64, numpy=
array([[0.88945694, 0.80417011, 0.17859964, 0.5655523 , 0.32113059,
        0.72886897, 0.6622883 , 0.66402494, 0.83960838, 0.66773621]])>

This was the pass to the optimizer.

@tf.function
def loss_fn():
    return tf.squeeze(model(x_optimal))

loss_history = tfp.math.minimize(
    loss_fn=loss_fn,
    num_steps=1000,
    optimizer=tf.optimizers.Adam(1e-4),
    trainable_variables=[x_optimal,]
)
x_optimal
x_optimal
<tf.Variable 'Variable:0' shape=(1, 10) dtype=float64, numpy=
array([[0.86916902, 0.79157565, 0.25678628, 0.48365721, 0.27728148,
        0.80987712, 0.71283698, 0.74139257, 0.92826077, 0.52938306]])>

The result that I am getting is possibly correct, however, it is hard to judge therefore I want to inverse_transform it back to the interpretable value, but I keep getting this error. To me, it looks like I am only passing 1 argument. Am I just using this incorrectly?

It could be because I am passing self AND x_optimal, however, if this is the case I am not sure how to treat that.

The traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-31-8d60fd7478a0> in <module>
----> 1 x_optimal_tr = PredictorScaler.inverse_transform(x_optimal)

~/.pyenv/versions/3.8.2/envs/python3env/lib/python3.8/site-packages/sklearn/preprocessing/_data.py in inverse_transform(self, X)
    456         check_is_fitted(self)
    457 
--> 458         X = check_array(X, copy=self.copy, dtype=FLOAT_DTYPES,
    459                         force_all_finite="allow-nan")
    460 

~/.pyenv/versions/3.8.2/envs/python3env/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

~/.pyenv/versions/3.8.2/envs/python3env/lib/python3.8/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
    614                     array = array.astype(dtype, casting="unsafe", copy=False)
    615                 else:
--> 616                     array = np.asarray(array, order=order, dtype=dtype)
    617             except ComplexWarning as complex_warning:
    618                 raise ValueError("Complex data not supported\n"

~/.pyenv/versions/3.8.2/envs/python3env/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     81 
     82     """
---> 83     return array(a, dtype, copy=False, order=order)
     84 
     85 

TypeError: __array__() takes 1 positional argument but 2 were given

Solution

x_optimal is a tf.Variable while MinMaxScaler.inverse_transform expects a array.

You can convert the tf.Variable to a numpy array using the numpy() method:

x_optimal_tr = PredictorScalerFit.inverse_transform(x_optimal.numpy())

Answered By – Lescurel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published