model.add(Conv1D(filters * 2, 1, padding='valid', activation='relu', strides=1)) model.add(Conv1D(10, 1, padding='valid', activation='relu', strides=1)) model.add(GlobalAveragePooling1D()) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() print('Train...') history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val)) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) print('Test score:', score) print('Test accuracy:', acc) export(model, history, tokenizer, name="sentiment_140_cnn_rnn", score=score, acc=acc)
from sklearn.feature_extraction.text import CountVectorizer import pickle from sklearn.naive_bayes import MultinomialNB from datasets import sentiment_140 from utils.data_utils import export maxlen = 100 # Get data (x_train, y_train), (x_val, y_val), (x_test, y_test) = sentiment_140.load_data() # No need for train and validation for bayes x_train = np.concatenate((x_train, x_val)) y_train = np.concatenate((y_train, y_val)) vectorizer = CountVectorizer() # Creates vocabulary for words, and transform sentences in train to a word frequency vector. x_train = vectorizer.fit_transform(x_train) x_test = vectorizer.transform(x_test) # transform to frequency vector. bayes = MultinomialNB() bayes.fit(x_train, y_train) acc = bayes.score(x_test, y_test) export(bayes, vocabulary=vectorizer, acc=acc)
print('Build model...') model = Sequential() model.add(Embedding(max_features, embedding_size, input_length=maxlen)) model.add(Dropout(0.5)) model.add(Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1)) model.add(MaxPooling1D(pool_size=pool_size)) model.add(GRU(gru_output_size)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print('Train...') history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, callbacks=[EarlyStopping(patience=2)], validation_data=(x_val, y_val)) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) print('Test score:', score) print('Test accuracy:', acc) export(model, history, name="imdb_cnn_gru", score=score, acc=acc)
model.add( Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1)) model.add(MaxPooling1D(pool_size=pool_size)) model.add(GRU(gru_output_size)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() print('Train...') history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val)) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) print('Test score:', score) print('Test accuracy:', acc) export(model, history, tokenizer, name="sentiment_140_neg_gru_lstm", score=score, acc=acc)