我试着建立一个只有一层的CNN,但我有一些问题。 事实上,编者告诉我
ValueError: Error when checking model input: expected conv1d_1_input to have 3 dimensions, but got array with shape (569, 30)
这是密码
import numpy
from keras.models import Sequential
from keras.layers.convolutional import Conv1D
numpy.random.seed(7)
datasetTraining = numpy.loadtxt("CancerAdapter.csv",delimiter=",")
X = datasetTraining[:,1:31]
Y = datasetTraining[:,0]
datasetTesting = numpy.loadtxt("CancereEvaluation.csv",delimiter=",")
X_test = datasetTraining[:,1:31]
Y_test = datasetTraining[:,0]
model = Sequential()
model.add(Conv1D(2,2,activation='relu',input_shape=X.shape))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, epochs=150, batch_size=5)
scores = model.evaluate(X_test, Y_test)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
如果无法看到更多细节,则预处理后的数据形状不正确。
将X重塑为具有3个维度:
我在其他帖子中也提到过:
要将形状
(nrows, ncols)
的常用特征表数据输入到路缘石的Conv1d,需要执行以下两个步骤:例如,以iris数据集的前4个特征为例:
要查看常用格式及其形状:
输出显示常用格式及其形状:
以下代码更改格式:
上述代码数据格式的输出及其形状:
这对凯拉斯的Conv1d很有效。对于
input_shape (4,1)
是必需的。td;lr您需要重塑数据的形状,使其具有空间维度,以便
Conv1d
有意义:从本质上重塑数据集,如下所示:
致:
说明和示例
通常卷积作用于空间维度。内核在维度上“卷积”产生一个张量。在Conv1D的情况下,内核被传递到每个示例的“steps”维度上。
您将看到在NLP中使用的Conv1D,其中
steps
是句子中的单词数(填充到某个固定的最大长度)。这些单词可能被编码为长度为4的向量。下面是一个例子:
在这种情况下,我们设置conv输入的方式是:
在您的例子中,您将把特征视为空间维度,每个特征的长度为1。(见下文)
下面是您的数据集中的一个示例
我们将Conv1D示例设置为:
正如您所看到的,您的数据集必须在(569,30,1)中重新调整 使用:
下面是一个可以运行的完整示例(我将使用Functional API)
相关问题 更多 >
编程相关推荐