-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstm_word2vec_kernel_size=1.py
165 lines (137 loc) · 5.88 KB
/
lstm_word2vec_kernel_size=1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""lstm_word2vec_kernel_size=1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1a18QlYosBg0wxAZ0dQUIpci2gIzzT4jX
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#train ve test verilerini train ve test olarak okuduk
train=pd.read_excel("clean_tweet_train.xlsx")
test=pd.read_excel("clean_tweet_test.xlsx")
corpus = train.append(test, ignore_index=True).fillna(' ')
test.dropna(inplace=True)
test.reset_index(drop=True,inplace=True)
train.dropna(inplace=True)
train.reset_index(drop=True,inplace=True)
x_train=train.text
y_train=train.sentiment
x_test=test.text
y_test=test.sentiment
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalMaxPooling1D
from tqdm import tqdm
tqdm.pandas(desc="progress-bar")
import gensim
from gensim.models.word2vec import Word2Vec
from gensim.models.doc2vec import TaggedDocument
import multiprocessing
from sklearn import utils
def labelize_tweets_ug(tweets,label):
result = []
prefix = label
for i, t in zip(tweets.index, tweets):
result.append(TaggedDocument(t.split(), [prefix + '_%s' % i]))
return result
#bütün tweet verilerini topladık -train-test ve validationda olan textler toplandı
all_x = pd.concat([x_train,x_test])
all_x_w2v = labelize_tweets_ug(all_x, 'all')
#tweet kelimelerine word2vec cbow yöntemi(sg=0) uygulanıyor-window_size=2
cores = multiprocessing.cpu_count()
model_ug_cbow = Word2Vec(sg=0, size=100, negative=5, window=2, min_count=2, workers=cores, alpha=0.065, min_alpha=0.065)
model_ug_cbow.build_vocab([x.words for x in tqdm(all_x_w2v)])
for epoch in range(30):
model_ug_cbow.train(utils.shuffle([x.words for x in tqdm(all_x_w2v)]), total_examples=len(all_x_w2v), epochs=1)
model_ug_cbow.alpha -= 0.002
model_ug_cbow.min_alpha = model_ug_cbow.alpha
#daha sonra skip-gram modeli
model_sg = Word2Vec(sg=1, size=100, negative=5, window=2, min_count=2, workers=cores, alpha=0.065, min_alpha=0.065)
model_sg.build_vocab([x.words for x in tqdm(all_x_w2v)])
"""%%time
#kelime vektörlerinin elde edilemesi için skip-gram modeli kullanılıyor
for epoch in range(30):
model_sg.train(utils.shuffle([x.words for x in tqdm(all_x_w2v)]), total_examples=len(all_x_w2v), epochs=1)
model_sg.alpha -= 0.002
model_sg.min_alpha = model_sg.alpha
"""
#bu iki yöntemi birleştirdik
embeddings_index = {}
for w in model_ug_cbow.wv.vocab.keys():
embeddings_index[w] = np.append(model_ug_cbow.wv[w],model_sg.wv[w])
print('Word vektör sayısı:' , len(embeddings_index))
from keras.preprocessing import sequence
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=10000)
tokenizer.fit_on_texts(x_train)
sequences = tokenizer.texts_to_sequences(x_train)
x_train_seq = pad_sequences(sequences, maxlen=35)
sequences_test = tokenizer.texts_to_sequences(x_test)
x_test_seq = pad_sequences(sequences_test, maxlen=35)
from keras import utils as np_utils
y_test = np_utils.to_categorical(y_test, num_classes= 3)
y_train = np_utils.to_categorical(y_train, num_classes= 3)
#elde ettiğimix-z kelime vektörlerinden bir matrix oluşturuyoruz ,embedding layer
#için num_words ile training için kullanacağımız most frequent word sayısı belirlendi
#200 ise embedding_dimension
num_words = 10000
embedding_matrix = np.zeros((num_words, 200))
for word, i in tokenizer.word_index.items():
if i >= num_words:
continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
print(x_train_seq.shape)
print(y_train.shape)
print(x_test_seq.shape)
print(y_test.shape)
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.layers.embeddings import Embedding
embedding_vecor_length = 200
model = Sequential()
model.add(Embedding(10000, embedding_vecor_length, input_length=35))
model.add(Dropout(0.2))
model.add(Conv1D(filters=32, kernel_size=1, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(100))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train_seq, y_train, epochs=10, batch_size=64)
scores = model.evaluate(x_test_seq, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
embedding_vecor_length = 200
model = Sequential()
model.add(Embedding(10000, embedding_vecor_length,weights=[embedding_matrix], input_length=35,trainable=False))
model.add(Dropout(0.2))
model.add(Conv1D(filters=32, kernel_size=1, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(100))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train_seq, y_train, epochs=10, batch_size=64)
scores = model.evaluate(x_test_seq, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
embedding_vecor_length = 200
model = Sequential()
model.add(Embedding(10000, embedding_vecor_length,weights=[embedding_matrix], input_length=35,trainable=True))
model.add(Dropout(0.2))
model.add(Conv1D(filters=32, kernel_size=1, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(100))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train_seq, y_train, epochs=10, batch_size=64)
scores = model.evaluate(x_test_seq, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))