Я делаю простой двоичный классификатор, но что-то случилось, и почему?
примечание: я переопределил соответствующие слои для поддержки mask_zero
подробности следующие ...
моя модель следующая:
def mymodel_v1(conf_dt):
input_ = Input(shape=(conf_dt['sent_len'],), dtype='int32')
embedding = Embedding(input_dim=conf_dt['vocab_size']+1, output_dim=conf_dt['embed_size'], input_length=conf_dt['sent_len'], mask_zero=True)(input_)
flat = FlattenWithMasking()(embedding)
dropout = Dropout(rate=conf_dt['dropout'])(flat)
dense = Dense(300)(dropout)
out = Dense(1, activation='sigmoid')(dense)
model = Model(inputs=input_, outputs=out)
print model.summary()
return model
и шаг поезда:
def train(train_fp, test_fp, conf_dt):
train_X, train_y, test_X, test_y, _ = load_data(train_fp, test_fp)
train_X = pad_sequences(train_X, maxlen=conf_dt['sent_len'], padding='post', truncating='post')
test_X = pad_sequences(test_X, maxlen=conf_dt['sent_len'], padding='post', truncating='post')
train_y = np.array(train_y, ndmin=2)
test_y = np.array(test_y, ndmin=2)
print 'data load and preprocess done'
print 'train_X.shape: ', train_X.shape
print 'train_y.shape: ', train_y.shape
sys.stdout.flush()
model = mymodel_v1(conf_dt)
model.compile(optimizer='rmsprop', loss='binary_crossentropy')
model.fit(train_X, train_y, batch_size=64, nb_epoch=2, verbose=2)
print model.summary()
model.evaluate(test_X, test_y)
config.dict:
conf_dt = {'vocab_size': 200000, 'dropout': 0.3, 'sent_len': 280, 'embed_size': 50}
вывод формы и модели. Сумма составляет:
train_X.shape: (116389, 280)
train_y.shape: (116389, 1)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 280) 0
_________________________________________________________________
embedding_1 (Embedding) (None, 280, 50) 10000050
_________________________________________________________________
flatten_with_masking_1 (Flat (None, 14000) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 14000) 0
_________________________________________________________________
dense_1 (Dense) (None, 300) 4200300
_________________________________________________________________
dense_2 (Dense) (None, 1) 301
=================================================================
Total params: 14,200,651
Trainable params: 14,200,651
Non-trainable params: 0
трассировка следующая:
File "train.py", line 31, in train
model.fit(train_X, train_y, batch_size=64, nb_epoch=2, verbose=2)
File "/home/homework/.jumbo/lib/python2.7/site-packages/keras/engine/training.py", line 1598, in fit
validation_steps=validation_steps)
File "/home/homework/.jumbo/lib/python2.7/site-packages/keras/engine/training.py", line 1183, in _fit_loop
outs = f(ins_batch)
File "/home/homework/.jumbo/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2273, in __call__
**self.session_kwargs)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run
feed_dict_tensor, options, run_metadata)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
options, run_metadata)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [64] vs. [64,280]
[[Node: loss/dense_2_loss/mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](loss/dense_2_loss/Mean, loss/dense_2_loss/Cast)]]
Caused by op u'loss/dense_2_loss/mul', defined at:
File "train.py", line 49, in <module>
train(train_pt, test_pt, conf_dt)
File "train.py", line 29, in train
model.compile(optimizer='rmsprop', loss='binary_crossentropy')
File "/home/homework/.jumbo/lib/python2.7/site-packages/keras/engine/training.py", line 850, in compile
sample_weight, mask)
File "/home/homework/.jumbo/lib/python2.7/site-packages/keras/engine/training.py", line 455, in weighted
score_array *= mask
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 865, in binary_op_wrapper
return func(x, y, name=name)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1088, in _mul_dispatch
return gen_math_ops._mul(x, y, name=name)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 1449, in _mul
result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/homework/.jumbo/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): Incompatible shapes: [64] vs. [64,280]
[[Node: loss/dense_2_loss/mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](loss/dense_2_loss/Mean, loss/dense_2_loss/Cast)]]
почему это произошло? кто-нибудь может мне помочь? ти.