【问题标题】:InvalidArgumentError: unique expects a 1D vectorInvalidArgumentError: unique 需要一维向量
【发布时间】:2021-05-22 00:14:03
【问题描述】:

我正在尝试创建用于神经网络算法的自定义损失函数。 我的损失函数:

import keras.backend as kb
import tensorflow as tf
def sign_penalty(y_true, y_pred):
    penalty=0.5
    loss=tf.where(tf.less(y_pred/y_true, 1),
                  penalty*(1-tf.dtypes.cast((((tf.unique_with_counts(y_pred >= y_true)[2])/(kb.sum(tf.unique_with_counts(y_pred >= y_true)[2])))[0]),tf.float32))+kb.sum((y_true - y_pred)**2/y_true),
                  kb.square(kb.sum((y_true - y_pred)**2/y_true)))
    return(loss) 

使用的模型:

model_mlp = Sequential()
model_mlp.add(Dense(100, activation='relu', input_dim=X_train.shape[1]))
model_mlp.add(Dense(1))
model_mlp.compile(loss=sign_penalty, optimizer=adam)
model_mlp.summary()

当我拟合模型时:

mlp_history = model_mlp.fit(X_train.values, Y_train, validation_data=(X_valid.values, Y_valid), epochs=epochs, verbose=2)

我收到以下错误:

InvalidArgumentError: unique 需要一维向量。 [[节点 sign_penalty/UniqueWithCounts(定义在 :3) ]] [Op:__inference_train_function_773] 函数调用堆栈: train_function

我认为错误来自点球,但我不知道为什么。

【问题讨论】:

  • 在拟合数据时,为什么要传入X_train.values 而不是X_train
  • 很可能是因为它是一个数据框,.values 将其数据作为一个 numpy 数组提供
  • y_pred >= y_true 不是一维向量
  • @yudhiesh 因为 X_train 是一个数据框,所以我必须将 ir 更改为一个数组以适应模型。但是,Y_train 已经是一个数组,所以我不必更改它(使用 .values)
  • @AkshaySehgal 是的,X_train 是一个数据框,我该怎么办?没有 .values 我仍然会得到同样的错误

标签: python tensorflow keras neural-network loss-function


【解决方案1】:

您正在将 3D 矢量传递给unique_with_counts。只能通过一维。

试试这个简单的方法来计算百分比:

def sign_penalty(y_true, y_pred):
    penalty=0.5
    a = tf.cast((y_pred >= y_true)[2], tf.float32)
    s = kb.sum(a)
    percentage = a / s
    loss=tf.where(tf.less(y_pred/y_true, 1),
                  penalty*(1-percentage)+kb.sum((y_true - y_pred)**2/y_true),
                  kb.square(kb.sum((y_true - y_pred)**2/y_true)))
    return(loss) 

【讨论】:

    【解决方案2】:

    为了解决这个问题,我打印了 y_true 和 y_pred:

    tf.print(y_true)
    tf.print(y_pred)
    

    发现他们是一个热门的代表。 当我将它们从单热表示中反转时,事情开始对我有用:

    y_true_c = tf.argmax(y_true, axis=1)
    y_pred_c = tf.argmax(y_pred, axis=1)
    

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 2013-08-16
      • 2011-04-21
      相关资源
      最近更新 更多