【问题标题】:Does this Keras Conv1D model correctly represent the intended architecture?这个 Keras Conv1D 模型是否正确地代表了预期的架构?
【发布时间】:2018-02-14 17:21:28
【问题描述】:

我是 Keras 的新手,正在尝试使用一维卷积神经网络 (CNN) 进行多类分类。我创建了一个简单的模型,并想检查它是否正确地代表了我想要的架构。

我的输入数据是一个形状为(number_of_samples, number of features) 的numpy 数组,其中number_of_samples = 3541number_of_features = 144。有 277 个类,我使用 one-hot 编码将目标表示为形状为 (number_of_samples,number_of_features) 的数组。我想要的架构如下图所示:

我的模型(我已经运行没有任何问题)的代码如下:

# Variables:
############
num_features = 144
num_classes = 277
units = num_classes
input_dim = 1
num_filters = 1
kernel_size = 3

# Reshape training data and labels:
###################################
# inital training_data has shape (3541, 144)
training_data_reshaped = np.atleast_3d(training_data) # (has shape 3541, 144, 1)

# inital labels vector has shape (3541, 1)
new_labels_binary = to_categorical(labels) # One-hot encoding of class labels

# Build, compile and fit model:
###############################
model = Sequential()

# A 1D convolutional layer which applies 1 output filter with a window size (length) of 3 and 
# a (default) stride length of 1
model.add(Conv1D(filters = num_filters, 
                 kernel_size = kernel_size,
                 activation = 'relu',
                 input_shape=(num_features, input_dim))) 
model.add(Flatten())
# Output layer
model.add(Dense(units=units))

sgd = optimizers.SGD()
model.compile(optimizer = sgd, 
              loss = 'categorical_crossentropy')

model.fit(x = training_data_reshaped, 
          y = new_labels_binary, 
          batch_size = batch_size)

print(model.summary())

我的代码是否正确地代表了我想要的架构?特别是:

  1. 我的目标是卷积层输出中的 142 个神经元中的每一个都连接到模型输出层中的 277 个神经元中的每一个,并且在输入样本x 上,输出层输出的向量与new_labels_binary 的行x 进行比较。根据我对 Keras 文档的了解,这个模型应该做到这一点,但我正在检查,因为我是新手,而且文档有时模棱两可!
  2. 我的意思不是含糊其辞:考虑到我想要的架构,我的模型中有什么不(完全)正确的吗?我只是想确保我没有遗漏任何东西!

提前致谢。

【问题讨论】:

    标签: python tensorflow keras conv-neural-network


    【解决方案1】:

    结构对我来说看起来不错,但如果你想解决多标签分类任务,输出层通常应该有一个 softmax 激活。

    model.add(Dense(units=units,activation='softmax'))
    

    如果您没有为密集层指定激活,则会应用线性激活。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      相关资源
      最近更新 更多