Keras神经网络断言错误

时间:2017-02-19 13:00:02

标签: python machine-learning scikit-learn neural-network keras

我遇到了Keras断言错误的问题,想问一下是否有人可以请求帮助:

我之前使用2D卷积运行了Keras nn并且从未见过这个错误。

#-----------------BEGIN FUNCTION 1-----------------
def create_model(input_size1, num_labels, conv1_num_filters, conv1_filter_size1, conv2_num_filters, conv2_filter_size1, pool1_1, dropout1, pool2_1, dropout2, neurons1, reg_l2, neurons2, reg_l2_2):

    model = Sequential()
    model.add(Convolution1D(conv1_num_filters, conv1_filter_size1, init = 'glorot_uniform', border_mode='same',
       input_shape=(1, input_size1),
       activation = 'relu'))
    model.add(MaxPooling1D(pool_length=(pool1_1),border_mode='same'))
    model.add(BatchNormalization(epsilon=0.001, mode=0, axis=1, momentum=0.99, weights=None, beta_init='zero', gamma_init='one', gamma_regularizer=None, beta_regularizer=None))
    model.add(Convolution1D(conv2_num_filters, conv2_filter_size1, init = 'glorot_uniform', activation = 'relu', border_mode='same'))
    model.add(MaxPooling1D(pool_length=(pool1_1),border_mode='same'))
    model.add(Dropout(dropout1))
    model.add(Flatten())
    model.add(BatchNormalization(epsilon=0.001, mode=0, axis=1, momentum=0.99, weights=None, beta_init='zero', gamma_init='one', gamma_regularizer=None, beta_regularizer=None))
    model.add(Dense(neurons1, W_regularizer=l2(reg_l2), init = 'glorot_uniform', activation = 'relu'))
    model.add(Dropout(dropout2))
    model.add(BatchNormalization(epsilon=0.001, mode=0, axis=1, momentum=0.99, weights=None, beta_init='zero', gamma_init='one', gamma_regularizer=None, beta_regularizer=None))
    model.add(Dense(neurons2, W_regularizer=l2(reg_l2_2), init = 'glorot_uniform', activation = 'relu'))
    model.add(Dense(num_labels, init = 'glorot_uniform', activation = 'tanh'))

    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) #0.01
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

    print(model.summary())
    #exit()

    return model
#-----------------END FUNCTION 1-----------------

model2 = create_model(input_size1, num_labels, conv1_num_filters,
                         conv1_filter_size1, conv2_num_filters,
                         conv2_filter_size1, pool1_1, dropout1, pool2_1,
                         dropout2, neurons1, reg_l2, neurons2, reg_l2_2);

x_train_ex = np.expand_dims(x_train, 1)
x_test_ex = np.expand_dims(x_test, 1)

from keras.utils.np_utils import to_categorical
y_train_ex = to_categorical(y_train, len(np.unique(y_train)))
y_test_ex = to_categorical(y_test, len(np.unique(y_train)))

model2.fit(x_train_ex, y_train_ex, batch_size=batch_size, nb_epoch=nb_epoch,
           verbose=1, validation_data=(x_test_ex, y_test_ex)

我收到错误说:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-41-c4780c441db5> in <module>()
     26 
     27 model2.fit(x_train_ex, y_train_ex, batch_size=batch_size, nb_epoch=nb_epoch,
---> 28            verbose=1, validation_data=(x_test_ex, y_test_ex))
     29 #print(model2.score(x_train_ex, y_train))
     30 #print(model2.score(x_test_ex, y_test))

.........(Lots more error messages)


 AssertionError: 

非常感谢!

1 个答案:

答案 0 :(得分:0)

当我从Keras 1.1.1升级到1.2.0时,问题似乎消失了。可能是版本问题。