本帖是前一贴的补充:

  1. 使用大数据,了解怎么处理数据不能一次全部加载到内存的情况。如果你内存充足,当我没说
  2. 训练好的模型的保存和使用
  3. 使用的模型没变,还是简单的feedforward神经网络(update:添加CNN模型)
  4. 如果你要运行本帖代码,推荐使用GPU版本或强大的VPS,我使用小笔记本差点等吐血
  5. 后续有关于中文的练习《TensorFlow练习13: 制作一个简单的聊天机器人》《TensorFlow练习7: 基于RNN生成古诗词》《TensorFlow练习18: 根据姓名判断性别

 

在正文开始之前,我画了一个机器学习模型的基本开发流程图:

TensorFlow练习2: 对评论进行分类

使用的数据集

使用的数据集:http://help.sentiment140.com/for-students/ (情绪分析)

数据集包含1百60万条推特,包含消极、中性和积极tweet。不知道有没有现成的微博数据集。

数据格式:移除表情符号的CSV文件,字段如下:

  • 0 – the polarity of the tweet (0 = negative, 2 = neutral, 4 = positive)
  • 1 – the id of the tweet (2087)
  • 2 – the date of the tweet (Sat May 16 23:58:44 UTC 2009)
  • 3 – the query (lyx). If there is no query, then this value is NO_QUERY.
  • 4 – the user that tweeted (robotickilldozr)
  • 5 – the text of the tweet (Lyx is cool)

training.1600000.processed.noemoticon.csv(238M)
testdata.manual.2009.06.14.csv(74K)

数据预处理

 

 
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
nltk
word_tokenize
WordNetLemmatizer
 
pickle
np
pd
OrderedDict
 
'training.1600000.processed.noemoticon.csv'
'testdata.manual.2009.06.14.csv'
 
# 提取文件中有用的字段
:
)
:
:
)
# 4
:
# 消极评论
:
# 中性评论
:
# 积极评论
 
]
tweet
# [0, 0, 1]:%:%:%: that's a bummer.  You shoulda got David Carr of Third Day to do it. ;D
:
)
# 处理完成,处理后文件大小127.5M
 
)
)
 
# 创建词汇表
:
]
)
:
:
# 统计单词出现次数
:
]
)
:
)
:
1
:
1
 
)
:
# 过滤掉一些词
)
:
)
lex
 
)
 
:
)
 
 
# 把字符串转为向量
def string_to_vector(input_file, output_file, lex):
output_f = open(output_file, 'w')
lemmatizer = WordNetLemmatizer()
with open(input_file, buffering=10000, encoding='latin-1') as f:
for line in f:
label = line.split(':%:%:%:')[0]
tweet = line.split(':%:%:%:')[1]
words = word_tokenize(tweet.lower())
words = [lemmatizer.lemmatize(word) for word in words]
 
features = np.zeros(len(lex))
for word in words:
if word in lex:
features[lex.index(word)] = 1  # 一个句子中某个词可能出现两次,可以用+=1,其实区别不大
 
features = list(features)
output_f.close()
 
 
f = open('lexcion.pickle', 'rb')
lex = pickle.load(f)
f.close()
 
# lexcion词汇表大小112k,training.vec大约112k*1600000  170G  太大,只能边转边训练了
# string_to_vector('training.csv', 'training.vec', lex)
# string_to_vector('tesing.csv', 'tesing.vec', lex)

上面代码把原始数据转为training.csv、和tesing.csv,里面只包含label和tweet。lexcion.pickle文件保存了词汇表。

如果数据文件太大,不能一次加载到内存,可以把数据导入数据库
Dask可处理大csv文件

开始漫长的训练

 

 
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
os
random
tf
pickle
np
_tokenize
WordNetLemmatizer
 
)
)
)
 
 
:
)
)
)
# 从文件中随机选择n条记录
:
]
)
_size
:
)
)
)
lines
 
 
:
:
]
]
)
:
]
]
)
]
)
:
:
1
 
)
)
_y
 
)
 
 
#######################################################################
 
# 输入层
 
# hide layer
# hide layer(隐藏层)听着很神秘,其实就是除输入输出层外的中间层
 
# 输出层
 
 
:
}
}
}
 
# w·x+b
)
# 激活函数
)
# 激活函数
)
 
_output
 
 
)
)
90
 
:
)
)
)
 
:
)
 
)
)
0
0
# 一直训练
]
]
 
#if model.ckpt文件已存在:
# saver.restore(session, 'model.ckpt')  恢复保存的session
 
:
)
:
]
]
)
]
 
)
:
:
# 一个句子中某个词可能出现两次,可以用+=1,其实区别不大
 
)
)
 
)
:
)
 
# 准确率
:
)
)
)
# 保存准确率最高的训练模型
)
accuracy
# 保存session
0
1
 
 
)

上面程序占用内存600M,峰值1G。

运行:

TensorFlow练习2: 对评论进行分类

训练模型保存为model.ckpt。

使用训练好的模型

 

 
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
tf
pickle
word_tokenize
WordNetLemmatizer
np
 
)
)
)
 
# 输入层
 
# hide layer
# hide layer(隐藏层)听着很神秘,其实就是除输入输出层外的中间层
 
# 输出层
:
}
}
}
 
# w·x+b
)
# 激活函数
)
# 激活函数
)
 
_output
 
)
:
)
 
:
)
)
)
 
)
)
]
 
)
:
:
1
 
#print(predict.eval(feed_dict={X:[features]})) [[val1,val2,val3]]
)
res
 
 
)

 

上面使用简单的feedfroward模型,下面使用CNN模型

 

 
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
# https://github.com/Lab41/sunny-side-up
os
random
tf
pickle
np
word_tokenize
WordNetLemmatizer
 
)
)
)
 
:
)
)
)
# 从文件中随机选择n条记录
:
]
)
st_size
:
)
)
)
lines
 
:
:
]
]
)
:
]
]
)
]
)
:
:
1
 
)
)
test_y
 
)
##############################################################################
)
3
 
)
)
 
)
 
90
 
:
# embedding layer
:
128
)
)
)
# convolution + maxpool layer
128
]
]
:
:
]
)
)
)
)
)
)
 
)
)
)
# dropout
:
)
# output
:
)
)
)
 
output
 
:
)
 
)
)
)
)
 
)
:
)
 
)
0
:
]
]
 
#if model.ckpt文件已存在:
# saver.restore(session, 'model.ckpt')  恢复保存的session
:
)
:
]
]
)
]
 
)
:
:
# 一个句子中某个词可能出现两次,可以用+=1,其实区别不大
 
)
)
 
)
)
:
)
 
:
)
)
)
)
)
 
1
 
)

使用了CNN模型之后,准确率有了显著提升。

 

http://blog.topspeedsnail.com/archives/10420

 

相关文章:

  • 2021-12-03
  • 2021-07-06
  • 2021-07-16
  • 2021-11-11
  • 2021-06-09
  • 2021-08-14
  • 2021-12-09
猜你喜欢
  • 2021-11-29
  • 2021-07-19
  • 2021-04-13
  • 2022-12-23
  • 2021-09-19
  • 2021-06-10
  • 2022-12-23
相关资源
相似解决方案