簡體   English   中英

KERAS 分類僅使用 Mnist 數據集上的一些數字

[英]KERAS Classification only use some of the digits on Mnist dataset

# Creating a Sequential Model and adding the layers
input_img = Input(shape=(28, 28, 1))

#63 kernels - Conv of 3X3
conv_1 = Conv2D(63, kernel_size=(3,3),activation='relu', padding='same')(input_img)
#Then pooling of 2X2
encoded = MaxPooling2D((2, 2), padding='same')(conv_1)
#model.add(Dropout(0.2))

###Classification###
# Flattening the 2D arrays for fully connected layers
flatten = Flatten()(encoded)
# Adding dense layer
fc = Dense(1000, activation='relu')(flatten)
fc1 = (Dropout(0.2))(fc)
#A6 = model.add(Dropout(0.2),name = 'A6')  #Combat Overfitting, drop random elements  
#Softmax layer must have neurons = range of labels, 0-9 for this case
softmax = Dense(5, activation='softmax', name='classification')(fc1)

model = Model(inputs=input_img, outputs=[softmax])

當我運行 model.fit model 時,出現以下錯誤:

ValueError: Data cardinality is ambiguous:
  x sizes: 30703
  y sizes: 30703, 51660
Please provide data which shares the same first dimension.

我想要實現的是,我正在嘗試在 mnist 數據集上運行 keras 分類,並且我已經刪除了一些數字,只剩下 0、1、2、3、9,總共 5 個整數,我需要索引整數,這樣我就可以 output 一個 5 個輸出的密集層,而不必堅持 10 個(覆蓋 integer 9)。 我已經完成了以下操作,但出現上述錯誤,請告知謝謝

  # Transform y_train (and similarly y_test).
    uniquetrain, index = np.unique(y_train, return_inverse=True)
    y_train = np.arange(len(uniquetrain))[index]
    # To get back the original labels, just index into the unique values.
    unique[y_train]
    
    # Transform y_train (and similarly y_test).
    uniquetest, index1 = np.unique(y_test, return_inverse=True)
    y_test = np.arange(len(uniquetest))[index1]
    # To get back the original labels, just index into the unique values.
    unique[y_test]

您可以創建掩碼以保留所需標簽的實例,還可以創建從舊標簽到新標簽的映射。 請看下面的代碼:

X_train = ... # Tensor of shape [ELEMS, 28, 28, 1]
y_train = ... # Tensor of shape [ELEMS]

X_test = ... # Tensor of shape [TEST_ELEMS, 28, 28, 1]
y_test = ... # Tensor of shape [TEST_ELEMS]

# Labels you want to keep
keep_labels = [0, 1, 2, 3, 9]

# Map old labels to new labels, for instance the label 9 on the new set of labels,
# is going to be 4
labels_to_index = {l: i for i,l in enumerate(keep_labels)}

# Masks to keep training and test instance. Trues keeps the instances
train_keep_mask = np.zeros(y_train.shape[0], dtype=np.bool)
test_keep_mask = np.zeros(y_test.shape[0], dtype=np.bool)

for l in keep_labels:
  train_keep_mask |= y_train == l
  test_keep_mask |= y_test == l

# Apply masks to filter the training and test instances
X_train = X_train[train_keep_mask]
y_train = y_train[train_keep_mask]
X_test = X_test[test_keep_mask]
y_test = y_test[test_keep_mask]

# From now on X_train, y_train, X_test, y_test only contain the desired labels
# which are defined in `keep_labels`

# Map old labels to new ones
new_y_train = np.array([labels_to_index[l] for l in y_train.tolist()])
new_y_test = np.array([labels_to_index[l] for l in y_test.tolist()])

# To invert the labels use `keep_labels[new_label]`
again_old_y_train = np.array([keep_labels[l] for l in new_y_train.tolist()])
again_old_y_test = np.array([keep_labels[l] for l in new_y_test.tolist()])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM