【发布时间】:2020-01-18 00:05:49
【问题描述】:
我正在研究一个简单的卷积神经网络 (CNN) 来执行分类。该网络的目标是将 224 x 256 标量数组分类为三种不同离散状态之一。这些数组中的每个值都代表从the Nintaco emulator 中提取的整数像素值。
我有一个相当简单的模型,构造如下:
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, models
import numpy as np
model = models.Sequential()
model.add(layers.Conv2D(32, 16, strides=8, activation='relu', input_shape=(224, 256, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(3, activation='softmax'))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
data_points = [
[33, 15, ... 25, 16]
[33, 15, ... 25, 16]
...
[33, 15, ... 25, 16]
[33, 15, ... 25, 16]
]
labels = [[0.1, 0.2, 0.3] for i in range(len(data_points))]
train_set, test_set, train_labels, test_labels = train_test_split(data_points, labels)
train_labels = np.expand_dims(train_labels, -1)
test_labels = np.expand_dims(test_labels, -1)
model.fit(
training_set,
training_labels,
epochs=10,
validation_data=(testing_set, testing_labels)
)
尝试适应这会导致失败并出现以下错误:
InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [32,3] and labels shape [96] [[{{node loss/dense_1_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
我很清楚我为这个函数构造了不正确的标签数据,但我不确定它应该如何正确格式化。
目前,我只专注于让 fit 功能发挥作用;我对网络的功效或构造完全漠不关心(尽管如果有人发现我的总体方法有任何缺陷,请毫不犹豫地提出它们)。
如何正确格式化训练数据的标签?
【问题讨论】:
-
什么是
training_labels.shape,应该是一维向量,阅读更多here -
我将
labels = [[0.1, 0.2, 0.3] for i in range(len(data_points))]更改为[random.randint(0, 2) for i in range(len(data_points))]并且能够使有限的案例工作。非常感谢! -
作为答案添加,很高兴我能提供帮助,如果可以关闭,请accept_the_answer
标签: python numpy tensorflow keras conv-neural-network