【问题标题】:KERAS Classification only use some of the digits on Mnist datasetKERAS 分类仅使用 Mnist 数据集上的一些数字
【发布时间】:2020-10-15 20:11:25
【问题描述】:
# 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 模型时,出现以下错误:

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 个整数,我需要对整数进行索引,这样我就可以输出 5 个输出的密集层,而不必坚持 10(覆盖整数 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]

【问题讨论】:

    标签: python keras classification


    【解决方案1】:

    您可以创建掩码来保留所需标签的实例,还可以创建从旧标签到新标签的映射。请看下面的代码:

    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()])
    

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 2021-08-10
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      相关资源
      最近更新 更多