我尝试从这个github https://github.com/bgmartins/sentence-classification/blob/master/sentence-classification.py
复制一个句子分类模型这是我的代码:
data = [ ( row["sentence"] , row["label"] ) for row in csv.DictReader(open("./test-data.txt"), delimiter='\t', quoting=csv.QUOTE_NONE) ]
random.shuffle( data )
train_size = int(len(data) * percent)
train_texts = [ txt.lower() for ( txt, label ) in data[0:train_size] ]
test_texts = [ txt.lower() for ( txt, label ) in data[train_size:-1] ]
train_labels = [ label for ( txt , label ) in data[0:train_size] ]
test_labels = [ label for ( txt , label ) in data[train_size:-1] ]
num_classes = len( set( train_labels + test_labels ) )
tokenizer = Tokenizer(nb_words=max_features, lower=True, split=" ")
tokenizer.fit_on_texts(train_texts)
train_sequences = sequence.pad_sequences( tokenizer.texts_to_sequences( train_texts ) , maxlen=max_sent_len )
test_sequences = sequence.pad_sequences( tokenizer.texts_to_sequences( test_texts ) , maxlen=max_sent_len )
train_matrix = tokenizer.texts_to_matrix( train_texts )
test_matrix = tokenizer.texts_to_matrix( test_texts )
embedding_weights = np.zeros( ( max_features , embeddings_dim ) )
for word,index in tokenizer.word_index.items():
if index < max_features:
try: embedding_weights[index,:] = embeddings[word]
except: embedding_weights[index,:] = np.random.rand( 1 , embeddings_dim )
le = preprocessing.LabelEncoder( )
le.fit( train_labels + test_labels )
train_labels = le.transform( train_labels )
test_labels = le.transform( test_labels )
但是当我尝试适应LSTM时:
model = Sequential()
model.add(Embedding(max_features, embeddings_dim, input_length=max_sent_len, mask_zero=True, weights=[embedding_weights] ))
model.add(Dropout(0.25))
model.add(LSTM(output_dim=embeddings_dim , activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True))
model.add(Dropout(0.25))
model.add(LSTM(output_dim=embeddings_dim , activation='sigmoid', inner_activation='hard_sigmoid'))
model.add(Dropout(0.25))
model.add(Dense(1))
model.add(Activation('sigmoid'))
if num_classes == 2: model.compile(loss='binary_crossentropy', optimizer='adam', class_mode='binary')
else: model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit( train_sequences , train_labels , nb_epoch=30, batch_size=32)
我收到此错误:
TypeError: run() got an unexpected keyword argument 'class_mode'
完整错误追溯:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last) <ipython-input-9-d63cbba5b5e7> in <module>()
12 if num_classes == 2: model.compile(loss='binary_crossentropy', optimizer='adam', class_mode='binary')
13 else: model.compile(loss='categorical_crossentropy', optimizer='adam')
---> 14 model.fit( train_sequences , train_labels , nb_epoch=30, batch_size=32)
15 results = model.predict_classes( test_sequences )
16 print ("Accuracy = " + repr( sklearn.metrics.accuracy_score( test_labels , results ) ))
~\Anaconda3\lib\site-packages\keras\models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
961 initial_epoch=initial_epoch,
962 steps_per_epoch=steps_per_epoch,
--> 963 validation_steps=validation_steps)
964
965 def evaluate(self, x=None, y=None,
~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs) 1703 initial_epoch=initial_epoch, 1704 steps_per_epoch=steps_per_epoch,
-> 1705 validation_steps=validation_steps)
1706
1707 def evaluate(self, x=None, y=None,
~\Anaconda3\lib\site-packages\keras\engine\training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
1233 ins_batch[i] = ins_batch[i].toarray()
1234
-> 1235 outs = f(ins_batch)
1236 if not isinstance(outs, list):
1237 outs = [outs]
~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py __call__(self, inputs)
2476 session = get_session()
2477 updated = session.run(fetches=fetches, feed_dict=feed_dict,
-> 2478 **self.session_kwargs)
2479 return updated[:len(self.outputs)]
2480
答案 0 :(得分:2)
model.compile
不会使用任何名为&#34; class_mode&#34;的参数,将其删除。
答案 1 :(得分:1)
请参阅:https://keras.io/preprocessing/image/
class_mode
与生成器一起使用。