-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path二分类.py
146 lines (77 loc) · 2.44 KB
/
二分类.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
# coding: utf-8
import pandas as pd
import random
import tensorflow as tf
from tensorflow import keras
import numpy as np
import tensorflow.keras.layers as layers
def check(c):
return '\u4e00' <= c <= '\u9fa5'
def get_words(sent_ids):
return ' '.join([id2word.get(i, '?') for i in sent_ids])
neg = pd.read_csv("data/neg.csv",header = None)
pos = pd.read_csv("data/pos.csv",error_bad_lines=False,header = None)
li = len(neg)*[0]+len(pos)*[1]
random.shuffle(li)
word2id = {}
n = 1
for words in neg[0]:
for word in words:
if check(word):
if word not in word2id:
word2id[word] = n
n+=1
for words in pos[0]:
for word in words:
if check(word):
if word not in word2id:
word2id[word] = n
n+=1
index = list(range(1,len(word2id)+1))
random.shuffle(index)
n = 0
for word in word2id:
word2id[word] = index[n]
n+=1
id2word = {v:k for k, v in word2id.items()}
words_list = []
neg_num = 0
pos_num = 0
for i in li:
if i == 0:
x = [word for word in neg[0][neg_num]]
y = list(filter(check,x))
z = list(map(lambda x:word2id[x],y))
words_list.append(z)
neg_num+=1
if i == 1:
x = [word for word in pos[0][pos_num]]
y = list(filter(check,x))
z = list(map(lambda x:word2id[x],y))
words_list.append(z)
pos_num+=1
words_list = keras.preprocessing.sequence.pad_sequences(
words_list, value=0,
padding='post', maxlen=128
)
train_x = words_list[:int(len(words_list)*0.7)]
train_y = li[:int(len(words_list)*0.7)]
test_x = words_list[int(len(words_list)*0.7):]
test_y = li[int(len(words_list)*0.7):]
vocab_size = len(word2id)+1
model = keras.Sequential()
model.add(layers.Embedding(vocab_size, 16))
model.add(layers.GlobalAveragePooling1D())
model.add(layers.Dense(16, activation='relu',kernel_regularizer=keras.regularizers.l2(0.01)))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(16, activation='relu',kernel_regularizer=keras.regularizers.l2(0.01)))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(train_x,train_y,
epochs=40, batch_size=1024,
validation_data=(test_x, test_y),
verbose=1)