Skip to main content
 首页 » 编程设计

python之ValueError : labels shape must be [batch_size, labels_dimension], 得到 (128, 2)

2025年02月15日752php

在 Python 3.5.2 中使用 TensorFlow 1.3.0 版。我试图模仿 TensorFlow 网站上 Iris 数据教程中 DNNClassifier 的功能,但遇到了困难。我正在导入一个包含大约 155 行数据和 15 列的 CSV 文件,将数据分解为训练数据和测试数据(我尝试在其中对正向或负向运动进行分类),并在我开始训练分类器时收到错误消息.下面是数据的设置方式

    #import values from csv 
    mexicof1 = pd.read_csv('Source/mexicoR.csv') 
 
    #construct pandas dataframe 
    mexico_df = pd.DataFrame(mexicof1) 
    #start counting from mexico.mat.2.nrow.mexico.mat...1. 
    mexico_dff = pd.DataFrame(mexico_df.iloc[:,1:16]) 
    mexico_dff.columns = ['tp1_delta','PC1','PC2','PC3','PC4','PC5','PC6','PC7', \ 
                  'PC8', 'PC9', 'PC10', 'PC11', 'PC12', 'PC13', 'PC14'] 
 
 
    #binary assignment for positive/negative values 
    for i in range(0,155): 
        if(mexico_dff.iloc[i,0] > 0): 
            mexico_dff.iloc[i,0] = "pos" 
        else: 
            mexico_dff.iloc[i,0] = "neg" 
 
    #up movement vs. down movement classification set up 
    up = np.asarray([1,0]) 
    down = np.asarray([0,1]) 
    mexico_dff['tp1_delta'] = mexico_dff['tp1_delta'].map({"pos": up, "neg": down}) 
 
 
    #Break into training and test data 
    #data: independent values 
    #labels: classification 
    mexico_train_DNN1data = mexico_dff.iloc[0:150, 1:15] 
    mexico_train_DNN1labels = mexico_dff.iloc[0:150, 0] 
    mexico_test_DNN1data = mexico_dff.iloc[150:156, 1:15] 
    mexico_test_DNN1labels = mexico_dff.iloc[150:156, 0] 
 
    #Construct numpy arrays for test data 
    temptrain = [] 
    for i in range(0, len(mexico_train_DNN1labels)): 
        temptrain.append(mexico_train_DNN1labels.iloc[i]) 
    temptrainFIN = np.array(temptrain, dtype = np.float32) 
 
    temptest = [] 
    for i in range(0, len(mexico_test_DNN1labels)): 
        temptest.append(mexico_test_DNN1labels.iloc[i]) 
    temptestFIN = np.array(temptest, dtype = np.float32) 
 
    #set up NumPy arrays 
    mTrainDat = np.array(mexico_train_DNN1data, dtype = np.float32) 
    mTrainLab = temptrainFIN 
    mTestDat = np.array(mexico_test_DNN1data, dtype = np.float32) 
    mTestLab = temptestFIN 

这样做会给我如下所示的数据:

    #Independent value output 
    mTestDat 
    Out[289]:  
    array([[-0.08404002, -3.07483053,  0.41106853, ..., -0.08682428, 
     0.32954004, -0.36451185], 
   [-0.31538665, -2.23493481,  1.97653472, ...,  0.35220796, 
     0.09061374, -0.59035355], 
   [ 0.44257978, -3.04786181, -0.6633662 , ...,  1.34870672, 
     0.43879321,  0.26306254], 
   ...,  
   [ 2.38574553,  0.09045095, -0.09710167, ...,  1.20889878, 
     0.00937434, -0.06398607], 
   [ 1.68626559,  0.65349185,  0.23625408, ..., -1.16267788, 
     0.45464727, -1.14916229], 
   [ 1.58263958,  0.1223636 , -0.12084256, ...,  0.7947616 , 
    -0.47359121,  0.28013545]], dtype=float32) 
 
    #Classification labels (up or down movement) output 
    mTestLab 
    Out[290]:  
    array([[ 0.,  1.], 
   [ 0.,  1.], 
   [ 0.,  1.], 
   [ 1.,  0.], 
   [ 0.,  1.], 
   [ 1.,  0.], 
    ........ 
   [ 1.,  0.], 
   [ 0.,  1.], 
   [ 0.,  1.], 
   [ 0.,  1.]], dtype=float32) 

按照此给定设置的教程进行操作后,我可以将代码运行到 classifier.train() 函数,然后它停止运行并出现以下错误:

    # Specify that all features have real-value data 
    feature_columns = [tf.feature_column.numeric_column("x", shape=[mexico_train_DNN1data.shape[1]])] 
 
    # Build 3 layer DNN with 10, 20, 10 units respectively. 
    classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns, 
                                    hidden_units=[10, 20, 10], 
                                    optimizer = tf.train.AdamOptimizer(0.01), 
                                    n_classes=2) #representing either an up or down movement 
 
 
    train_input_fn = tf.estimator.inputs.numpy_input_fn( 
    x = {"x": mTrainDat}, 
    y = mTrainLab, 
    num_epochs = None, 
    shuffle = True) 
 
    #Now, we train the model 
    classifier.train(input_fn=train_input_fn, steps = 2000) 
 
 
      File "Source\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\estimator\canned\head.py", line 174, in _check_labels 
(static_shape,)) 
 
    ValueError: labels shape must be [batch_size, labels_dimension], got (128, 2). 

我不确定为什么会遇到此错误,感谢任何帮助。

请您参考如下方法:

当 DNNClassifier 需要类标签(即 0 或 1)时,您正在使用单热([1, 0] 或 [0, 1])编码标签。在最后一个轴上解码一个单热编码,使用

class_labels = np.argmax(one_hot_vector, axis=-1) 

注意二进制可能会更快

class_labels = one_hot_vector[..., 1].astype(np.int32) 

尽管性能差异不会很大,而且我可能会使用更通用的版本以防您稍后添加另一个类。

在您的情况下,在生成 numpy 标签后,只需添加

mTrainLab = np.argmax(mTrainLab, axis=-1) 
mTestLab = np.argmax(mTestLab, axis=-1)