【问题标题】:Is there any way to inverse tensorflow predicted variable to origin value?有没有办法将张量流预测变量反转为原始值?
【发布时间】:2021-02-24 11:44:52
【问题描述】:

我已经运行了 tensorflow 2.0 算法,其中我的因变量是标签格式的 y

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0])

同时预测模型从 tensorflow 获得缩放值。

# compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy',f1_m,precision_m, recall_m])
# fit the model
model.fit(X_train, y_train, epochs=150, batch_size=32, verbose=0)

y_pred = model.predict(X_test)

y_pred[0:10]

array([[1.8975088e-02, 9.2321676e-01, 5.7808172e-02],
       [2.1689970e-03, 1.1041342e-02, 9.8678964e-01],
       [9.7219455e-01, 2.1523101e-02, 6.2822714e-03],
       [8.9549669e-04, 9.9892455e-01, 1.7989198e-04],
       [5.9214713e-06, 9.9999106e-01, 2.9540893e-06],
       [1.5215098e-05, 9.9994588e-01, 3.8917195e-05],
       [3.2570759e-05, 9.9996614e-01, 1.3605905e-06],
       [2.5089746e-03, 9.9069571e-01, 6.7953388e-03],
       [2.3909420e-02, 9.6926796e-01, 6.8226634e-03],
       [2.9210409e-04, 9.9955446e-01, 1.5343193e-04]], dtype=float32)

它必须是0,1和2之间的数字类别,但显示一些其他值。我们如何才能达到原始值?

【问题讨论】:

    标签: python-3.x encoding tensorflow2.0


    【解决方案1】:

    使用tf.argmax() 获取索引:

    ind = tf.argmax(y_pred, -1)
    

    【讨论】:

      猜你喜欢
      • 2017-11-01
      • 2021-03-13
      • 1970-01-01
      • 2018-10-23
      • 2020-03-24
      • 2011-05-09
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      相关资源
      最近更新 更多