【问题标题】:Keras multi-label classification: Failed to convert a NumPy array to a Tensor (Unsupported object type int)Keras 多标签分类:无法将 NumPy 数组转换为张量(不支持的对象类型 int)
【发布时间】:2021-07-10 10:16:33
【问题描述】:

我正在尝试使用 Keras 进行多标签分类。我从 kaggle 获得了我的数据集。

数据集链接:https://www.kaggle.com/dadajonjurakuziev/movieposter

我在尝试拟合模型时遇到错误。

代码:

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=20, test_size=0.3)


model = Sequential()

model.add(Conv2D(filters=16, kernel_size=(5, 5), activation="relu", input_shape=(SIZE,SIZE,3)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(filters=32, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.2))

model.add(Conv2D(filters=64, kernel_size=(5, 5), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.2))

model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.2))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(25, activation='sigmoid'))

model.summary()

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
#print(X_train, y_train, X_test, y_test)
history = model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test)) #This is the line where I am getting an error
​

错误:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

以下是 X_train 和 y_train 的一些值:

X_train[0]:

array([[[0.15294118, 0.1764706 , 0.3019608 ],
        [0.15294118, 0.1764706 , 0.3019608 ],
        [0.15686275, 0.18039216, 0.30588236],
        ...,
        [0.69803923, 0.69411767, 0.75686276],
        [0.69803923, 0.69411767, 0.75686276],
        [0.69803923, 0.69411767, 0.75686276]],

       [[0.11764706, 0.14509805, 0.25882354],
        [0.11764706, 0.14509805, 0.25882354],
        [0.12156863, 0.14509805, 0.27058825],
        ...,
        [0.69411767, 0.6901961 , 0.7529412 ],
        [0.69411767, 0.6901961 , 0.7529412 ],
        [0.69411767, 0.6901961 , 0.7529412 ]],

       [[0.10588235, 0.13333334, 0.24313726],
        [0.10588235, 0.13333334, 0.24313726],
        [0.10980392, 0.13725491, 0.2509804 ],
        ...,
        [0.6901961 , 0.6862745 , 0.7490196 ],
        [0.6901961 , 0.6862745 , 0.7490196 ],
        [0.6901961 , 0.6862745 , 0.7490196 ]],

       ...,

       [[0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        ...,
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943]],

       [[0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        ...,
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943]],

       [[0.1764706 , 0.31764707, 0.62352943],
        [0.1764706 , 0.31764707, 0.62352943],
        [0.1764706 , 0.31764707, 0.62352943],
        ...,
        [0.1764706 , 0.31764707, 0.62352943],
        [0.1764706 , 0.31764707, 0.62352943],
        [0.1764706 , 0.31764707, 0.62352943]]], dtype=float32)

y_train[0]:

array(['https://m.media-amazon.com/images/M/MV5BY2IyODNmMjctMjNlNi00NmU0LTkxYTEtMTBjNzJiY2U0ZTI2XkEyXkFqcGdeQXVyMjI3MTc2MzU@._V1_UX256,0,256,256_AL_.jpg',
       'The Lawyer', 2020, 7.1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
      dtype=object)

我不确定对象 dtype 是什么。我对这些话题很陌生。有人可以帮我吗?谢谢!

【问题讨论】:

  • 听起来你的 numpy 数组是 int 类型,而 keras 需要 floats。尝试X = X.astype('float32')Y = Y.astype('float32'),然后再对它们进行任何操作。

标签: python numpy tensorflow keras


【解决方案1】:

错误与问题Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) on ImageDataGenerator in Keras相同。

函数model.fit 期望X_trainy_trainnp.ndarrays 的数字类型,如np.float32np.int32。这里,y_train 的类型改为 np.object,因为该数组包含混合类型(字符串和整数)的元素。

您可能应该处理y_train 中的标签并删除字符串,因为您不能使用字符串来计算损失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2020-05-04
    • 2021-02-23
    • 2021-09-20
    • 2020-07-05
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多