inverse_transform 一个张量流变量:TypeError: __array__() 接受 1 个位置参数,但给出了 2 个
Posted
技术标签:
【中文标题】inverse_transform 一个张量流变量:TypeError: __array__() 接受 1 个位置参数,但给出了 2 个【英文标题】:inverse_transform a tensorflow variable: TypeError: __array__() takes 1 positional argument but 2 were given 【发布时间】:2021-09-27 09:11:22 【问题描述】:尝试在我的 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
将其恢复为可解释的值,但我一直收到此错误。对我来说,看起来我只传递了 1 个参数。我是不是用错了?
这可能是因为我传递了 self AND 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 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
【问题讨论】:
【参考方案1】:x_optimal
是 tf.Variable
,而 MinMaxScaler.inverse_transform
需要 numpy 数组。
您可以使用numpy()
方法将tf.Variable
转换为numpy 数组:
x_optimal_tr = PredictorScalerFit.inverse_transform(x_optimal.numpy())
【讨论】:
以上是关于inverse_transform 一个张量流变量:TypeError: __array__() 接受 1 个位置参数,但给出了 2 个的主要内容,如果未能解决你的问题,请参考以下文章