【发布时间】:2018-02-14 17:21:28
【问题描述】:
我是 Keras 的新手,正在尝试使用一维卷积神经网络 (CNN) 进行多类分类。我创建了一个简单的模型,并想检查它是否正确地代表了我想要的架构。
我的输入数据是一个形状为(number_of_samples, number of features) 的numpy 数组,其中number_of_samples = 3541 和number_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())
我的代码是否正确地代表了我想要的架构?特别是:
- 我的目标是卷积层输出中的 142 个神经元中的每一个都连接到模型输出层中的 277 个神经元中的每一个,并且在输入样本
x上,输出层输出的向量与new_labels_binary的行x进行比较。根据我对 Keras 文档的了解,这个模型应该做到这一点,但我正在检查,因为我是新手,而且文档有时模棱两可! - 我的意思不是含糊其辞:考虑到我想要的架构,我的模型中有什么不(完全)正确的吗?我只是想确保我没有遗漏任何东西!
提前致谢。
【问题讨论】:
标签: python tensorflow keras conv-neural-network