用于多类分类的 ANN 模型
Posted
技术标签:
【中文标题】用于多类分类的 ANN 模型【英文标题】:ANN model for multiclass classification 【发布时间】:2021-12-20 08:23:52 【问题描述】:我不知道问题是什么以及为什么会出现此错误:
ValueError: in user code:
ValueError: Shapes (None, 1) and (None, 6) are incompatible
谁能帮我处理这段代码?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation,Dropout
from sklearn.preprocessing import MinMaxScaler
%matplotlib inline
df = pd.read_csv('test.csv')
dft = pd.read_csv('train.csv')
X_train = df.drop('label',axis=1).values
y_train = df['label'].values
X_test = dft.drop('label',axis=1).values
y_test = dft['label'].values
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
model = Sequential()
model.add(Dense(units=30, activation='relu'))
model.add(Dense(units=15, activation='relu'))
model.add(Dense(6, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=X_train, y=y_train, epochs=15, batch_size=10, validation_data=(X_test, y_test))
【问题讨论】:
您能否编辑您的问题以提供一些您的训练和测试数据样本 【参考方案1】:问题是目标数组(y_train
和 y_test
)的第二维长度等于 1,而模型预期为 6,假设输出层的神经元数量已设置等于 6。要解决此问题,您需要对目标进行一次热编码(您可以使用 scikit-learn OneHotEncoder
)。如果您的目标确实有 6 个类,那么您的模型将起作用。
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.datasets import make_classification
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler
from sklearn.model_selection import train_test_split
tf.random.set_seed(0)
# generate the data
X, y = make_classification(n_classes=6, n_samples=1000, n_features=10, n_informative=10, n_redundant=0, random_state=42)
print(y.shape)
# (1000, )
# split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# one-hot encode the target
enc = OneHotEncoder(sparse=False, handle_unknown='ignore')
enc.fit(y_train.reshape(-1, 1))
y_train = enc.transform(y_train.reshape(-1, 1))
y_test = enc.transform(y_test.reshape(-1, 1))
print(y_train.shape, y_test.shape)
# (750, 6) (250, 6)
# scale the features
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
# define the model
model = Sequential()
model.add(Dense(units=30, activation='relu'))
model.add(Dense(units=15, activation='relu'))
model.add(Dense(6, activation='softmax'))
# fit the model
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=X_train, y=y_train, epochs=3, batch_size=10, validation_data=(X_test, y_test))
# Epoch 1/3
# 75/75 [==============================] - 1s 2ms/step - loss: 1.7872 - accuracy: 0.2427 - val_loss: 1.7719 - val_accuracy: 0.2600
# Epoch 2/3
# 75/75 [==============================] - 0s 781us/step - loss: 1.7660 - accuracy: 0.2547 - val_loss: 1.7549 - val_accuracy: 0.2720
# Epoch 3/3
# 75/75 [==============================] - 0s 768us/step - loss: 1.7528 - accuracy: 0.2587 - val_loss: 1.7408 - val_accuracy: 0.3280
【讨论】:
非常感谢,先生。我对这件事一无所知。非常感谢。 @Md.ImrulKayes 如果答案解决了您的问题,请接受 - 请参阅 What should I do when someone answers my question?以上是关于用于多类分类的 ANN 模型的主要内容,如果未能解决你的问题,请参考以下文章