猿问

ValueError:分类指标无法处理预训练 CNN 上的多标签指标混合

在 tensorflow 中,我打算针对图像分类任务调整预训练 CNN 中的超参数。为此,我使用了预训练模型vgg16来提取特征,并将提取的嵌入特征用作卷积神经网络 (CNN) 的输入。基本上,我把CNN放在预训练模型的上面进行训练。我正在尝试使用 优化超参数batch_size, epochs, drop-rate,GridSeatchCV但出现以下类型错误:


TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got array([200, 201, 202, 203,...

我也试过这样的:


grid_search = grid_search.fit(np.array(df_train_tf), np.array(labels_tr_tf[1:1001]))

但现在我遇到以下错误:


ValueError:分类指标无法处理多标签指标和多类目标的混合


我查看了这个错误,SO但无法消除上面的错误。如何解决这个问题?


在我的 CNN 中,我将 flatten dim 张量作为输入传递给 CNN,并且从预训练模型中提取的嵌入特征是 1 个 dim 特征向量,我将其转换为张量。当我尝试运行网格搜索以进行超参数优化时,出现了上述类型错误。我试图理解为什么我有这样的错误。谁能指出我发生了什么事?谢谢


我的尝试:


from keras.wrappers.scikit_learn import KerasClassifier

from sklearn.model_selection import GridSearchCV


model = KerasClassifier(build_fn=myCNN)

parameters = {'dim': [256,512, 784,1024, 2048],

              'epochs': [25,50,75,100,125,150,200],

              'batch_size':[32,64,128,192, 256],

              'drop_rate': [0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],

              'opt': ['rmsprop', 'adam', 'sgd'],

              'actv_func': ['relu', 'tanh']}


grid_search = GridSearchCV(estimator=model,

                           param_grid=parameters,

                           scoring='accuracy',

                           cv=5)


grid_search = grid_search.fit(df_train_tf, labels_tr_tf[1:1001])

其中df_train_tf是预训练嵌入特征labels_tr_tf的张量,是单热编码标签的张量。这是df_train_tf, labels_tr_tf看起来的样子。


df_train_tf.shape:

TensorShape([1000, 2048])


labels_tr_tf[1:1001].shape:

TensorShape([1000, 100])


type(labels_tr_tf[1:1001]):

tensorflow.python.framework.ops.EagerTensor


type(df_train_tf):

tensorflow.python.framework.ops.EagerTensor


df_train_tf:


<tf.Tensor: shape=(1000, 2048), dtype=float32, numpy=

array([[ 2.3664525 ,  6.4614077 , 22.128284  , ...,  2.8993628 ,

         7.6006427 ,  4.022856  ],

       [ 2.8110769 ,  0.        , 21.861437  , ...,  2.8580594 ,

         3.8210764 ,  3.4176886 ],...]

我没有找到任何线索为什么我会收到此错误。谁能指出我如何做到这一点?解决上述类型错误的任何解决方案?任何想法?谢谢


回首忆惘然
浏览 166回答 1
1回答

慕娘9325324

对于与 sklearn 一起使用GridSearchCV的多类标签,标签不应进行单热编码。它们应该是包含两个以上离散值的一维或列向量。检查文档以获取表示。所以我们必须将单热编码目标转换为一维,而这又需要我们将损失函数更改为sparse_categorical_crossentropy示例代码:X = np.random.randn(1000, 2048)y = np.array([i for i in range(100)]*10) # <- 1D array with target labelsdef myModel():&nbsp; model = keras.models.Sequential()&nbsp; model.add(keras.layers.Dense(100, input_dim=2048, activation='softmax'))&nbsp; model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])&nbsp; return model&nbsp;&nbsp;model = KerasClassifier(build_fn=myModel)parameters = { 'epochs': [10, 20, 30],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'batch_size':[1, 2, 3, 4, 5, 6, 7,8] }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;grid_search = GridSearchCV(estimator=model,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;param_grid=parameters,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scoring='accuracy',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cv=2)grid_search = grid_search.fit(X, y)print (grid_search.best_params_)输出:Epoch 1/10500/500 [==============================] - 2s 3ms/step - loss: 5.6664 - accuracy: 0.0100Epoch 2/10500/500 [==============================] - 1s 3ms/step - loss: 0.0066 - accuracy: 1.0000Epoch 3/10500/500 [==============================] - 1s 3ms/step - loss: 9.9609e-04 - accuracy: 1.0000------ output truncated ------{'batch_size': 3, 'epochs': 20}
随时随地看视频慕课网APP

相关分类

Python
我要回答