inverse_transform一个tensorflow变量:TypeError:__array__()接受1个位置参



当我试图在我的tf变量上调用inverse_transform时,我一直收到这个错误

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

最初,我使用MinMaxScaler为y和X 训练NN

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)

现在,我想将模型传递给优化器,以找到将最小化NN函数的X向量,为此,我将X初始化为初始猜测,这只是我的数据点之一(我的数据是表格(

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]])>

这是传递给优化器的。

@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]])>

我得到的结果可能是正确的,然而,很难判断,因此我想inverse_transform将其恢复到可解释的值,但我一直得到这个错误。对我来说,我似乎只通过了一个论点。我只是用错了这个吗?

这可能是因为我正在通过自我和x_optimal,然而,如果是这样的话,我不知道该如何处理。

回溯:

---------------------------------------------------------------------------
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 supportedn"
~/.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

x_optimal是一个tf.Variable,而MinMaxScaler.inverse_transform需要一个numpy数组。

您可以使用numpy()方法将tf.Variable转换为numpy数组:

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

相关内容

  • 没有找到相关文章

最新更新