分类形状中的keras ValueError
keras ValueError in classification shape
这是我的代码:
# data consists of 1 dimensional data (3277 elements). Number of data is 439
train_data = .... # numpy.ndarray
# I would like to classify data into 5 classes.
train_labels = .... # numpy.ndarray
print(train_data.shape) # -> Shape of train_data: (439, 3277)
print('Shape of train_labels:', train_labels.shape) # -> Shape of train_labels: (439,)
# prepare 5 one hot encoding array
categorical_labels = to_categorical(train_labels, 5)
print('Shape of categorical_labels:', categorical_labels.shape) # -> Shape of categorical_labels: (439, 5)
# I make a model to have 3277-elements data and classify data into 5 labels.
model = keras.Sequential([
keras.layers.Dense(30, activation='relu', input_shape=(3277,)),
keras.layers.Dense(30, activation='relu'),
keras.layers.Dense(5, activation='softmax')
])
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(data, categorical_labels, epochs=5, verbose=1) # A
#model.fit(data, train_labels, epochs=5, verbose=1) # B
当我尝试使用标记为 'A' 的行时,出现此错误
ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (5,)
使用'B',运行正常(无明显错误,机器returns高分)
显然,错误与形状的差异有关...当我想使用 keras.utils.to_categorical
时,我该如何修改我的代码?
另一个问题是为什么这个案例有效(https://github.com/keras-team/keras/blob/master/examples/mnist_mlp.py)而我的案例不...
结构看起来很像……对我来说。
因为sparse_categorical_crossentropy
不希望使用one-hot编码格式的标签,所以你应该使用loss='categorical_crossentropy'
。
简而言之,关于你的情况:
train_labels
=> loss='sparse_categorical_crossentropy'
categorical_labels
=> loss='categorical_crossentropy'
这是我的代码:
# data consists of 1 dimensional data (3277 elements). Number of data is 439
train_data = .... # numpy.ndarray
# I would like to classify data into 5 classes.
train_labels = .... # numpy.ndarray
print(train_data.shape) # -> Shape of train_data: (439, 3277)
print('Shape of train_labels:', train_labels.shape) # -> Shape of train_labels: (439,)
# prepare 5 one hot encoding array
categorical_labels = to_categorical(train_labels, 5)
print('Shape of categorical_labels:', categorical_labels.shape) # -> Shape of categorical_labels: (439, 5)
# I make a model to have 3277-elements data and classify data into 5 labels.
model = keras.Sequential([
keras.layers.Dense(30, activation='relu', input_shape=(3277,)),
keras.layers.Dense(30, activation='relu'),
keras.layers.Dense(5, activation='softmax')
])
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(data, categorical_labels, epochs=5, verbose=1) # A
#model.fit(data, train_labels, epochs=5, verbose=1) # B
当我尝试使用标记为 'A' 的行时,出现此错误
ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (5,)
使用'B',运行正常(无明显错误,机器returns高分)
显然,错误与形状的差异有关...当我想使用 keras.utils.to_categorical
时,我该如何修改我的代码?
另一个问题是为什么这个案例有效(https://github.com/keras-team/keras/blob/master/examples/mnist_mlp.py)而我的案例不...
结构看起来很像……对我来说。
因为sparse_categorical_crossentropy
不希望使用one-hot编码格式的标签,所以你应该使用loss='categorical_crossentropy'
。
简而言之,关于你的情况:
train_labels
=>loss='sparse_categorical_crossentropy'
categorical_labels
=>loss='categorical_crossentropy'