我想构建一个CNN模型,我的代码是:
model = Sequential([
Embedding(len(dict) + 1,
emb,
weights=[embedding_matrix],
input_length=maxlen,
trainable=False),
Conv1D(128,5,activation='relu'),
MaxPooling1D(5),
Conv1D(128,5,activation='relu'),
MaxPooling1D(5),
Conv1D(128,5,activation='relu'),
MaxPooling1D(35),
Flatten(),
Dense(128,activation='relu'),
Dense(2,activation='softmax'),
])
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['acc'])
print("model fitting - simplified convolutional neural network")
model.summary()
model.fit(x_train, y_train, validation_data=(x_val, y_val),
nb_epoch=10, batch_size=128)
我收到此错误:
Traceback (most recent call last):
File "/raid/daiy/PycharmProjects/SA/sa_cnn.py", line 122, in <module>
Dense(1,activation='softmax'),
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 273, in __init__
self.add(layer)
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 332, in add
output_tensor = layer(self.outputs[0])
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 546, in __call__
self.build(input_shapes[0])
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 798, in build
constraint=self.W_constraint)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 418, in add_weight
weight = initializer(shape, name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/initializations.py", line 65, in glorot_uniform
s = np.sqrt(6. / (fan_in + fan_out))
ZeroDivisionError: float division by zero
我在https://github.com/fchollet/keras/issues/2527上看到了这个问题。 我尝试各种形式的函数API,但仍然得到错误。然后我使用另一个数据,没有问题。但两个数据之间的差异只是维度。