TensorFlow练习18: 根据姓名判断性别
本帖训练一个可以根据姓名判断性别的CNN模型;我使用自己爬取的35万中文姓名进行训练。
使用同样的数据集还可以训练起名字模型,参看:
- TensorFlow练习7: 基于RNN生成古诗词
- https://github.com/tensorflow/models/tree/master/namignizer
- TensorFlow练习13: 制作一个简单的聊天机器人
准备姓名数据集
我上网找了一下,并没有找到现成的中文姓名数据集,额,看来只能自己动手了。
我写了一个简单的Python脚本,爬取了上万中文姓名,格式整理如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
姓名,性别
安镶怡,女
饶黎明,男
段焙曦,男
苗芯萌,男
覃慧藐,女
芦玥微,女
苏佳琬,女
王旎溪,女
彭琛朗,男
李昊,男
利欣怡,女
# 貌似有很多名字男女通用
|
如果你需要这个数据集,可以使用邮件或微信联系我。
训练模型
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
|
import tensorflow as tf
import numpy as np
name_dataset = 'name.csv'
train_x = []
train_y = []
with open(name_dataset, 'r') as f:
first_line = True
for line in f:
if first_line is True:
first_line = False
continue
sample = line.strip().split(',')
if len(sample) == 2:
train_x.append(sample[0])
if sample[1] == '男':
train_y.append([0, 1])# 男
else:
train_y.append([1, 0])# 女
max_name_length = max([len(name) for name in train_x])
print("最长名字的字符数: ", max_name_length)
max_name_length = 8
# 数据已shuffle
#shuffle_indices = np.random.permutation(np.arange(len(train_y)))
#train_x = train_x[shuffle_indices]
#train_y = train_y[shuffle_indices]
# 词汇表(参看聊天机器人练习)
counter = 0
vocabulary = {}
for name in train_x:
counter += 1
tokens = [word for word in name]
for word in tokens:
if word in vocabulary:
vocabulary[word] += 1
else:
vocabulary[word] = 1
vocabulary_list = [' '] + sorted(vocabulary, key=vocabulary.get, reverse=True)
print(len(vocabulary_list))
# 字符串转为向量形式
vocab = dict([(x, y) for (y, x) in enumerate(vocabulary_list)])
train_x_vec = []
for name in train_x:
name_vec = []
for word in name:
name_vec.append(vocab.get(word))
while len(name_vec) < max_name_length:
name_vec.append(0)
train_x_vec.append(name_vec)
#######################################################
input_size = max_name_length
num_classes = 2
batch_size = 64
num_batch = len(train_x_vec) // batch_size
X = tf.placeholder(tf.int32, [None, input_size])
Y = tf.placeholder(tf.float32, [None, num_classes])
dropout_keep_prob = tf.placeholder(tf.float32)
def neural_network(vocabulary_size, embedding_size=128, num_filters=128):
# embedding layer
with tf.device('/cpu:0'), tf.name_scope("embedding"):
W = tf.Variable(tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
embedded_chars = tf.nn.embedding_lookup(W, X)
embedded_chars_expanded = tf.expand_dims(embedded_chars, -1)
# convolution + maxpool layer
filter_sizes = [3,4,5]
pooled_outputs = []
for i, filter_size in enumerate(filter_sizes):
with tf.name_scope("conv-maxpool-%s" % filter_size):
filter_shape = [filter_size, embedding_size, 1, num_filters]
W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1))
b = tf.Variable(tf.constant(0.1, shape=[num_filters]))
conv = tf.nn.conv2d(embedded_chars_expanded, W, strides=[1, 1, 1, 1], padding="VALID")
h = tf.nn.relu(tf.nn.bias_add(conv, b))
pooled = tf.nn.max_pool(h, ksize=[1, input_size - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID')
pooled_outputs.append(pooled)
num_filters_total = num_filters * len(filter_sizes)
h_pool = tf.concat(3, pooled_outputs)
h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total])
# dropout
with tf.name_scope("dropout"):
h_drop = tf.nn.dropout(h_pool_flat, dropout_keep_prob)
# output
with tf.name_scope("output"):
W = tf.get_variable("W", shape=[num_filters_total, num_classes], initializer=tf.contrib.layers.xavier_initializer())
b = tf.Variable(tf.constant(0.1, shape=[num_classes]))
output = tf.nn.xw_plus_b(h_drop, W, b)
return output
# 训练
def train_neural_network():
output = neural_network(len(vocabulary_list))
optimizer = tf.train.AdamOptimizer(1e-3)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, Y))
grads_and_vars = optimizer.compute_gradients(loss)
train_op = optimizer.apply_gradients(grads_and_vars)
saver = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for e in range(201):
for i in range(num_batch):
batch_x = train_x_vec[i*batch_size : (i+1)*batch_size]
batch_y = train_y[i*batch_size : (i+1)*batch_size]
_, loss_ = sess.run([train_op, loss], feed_dict={X:batch_x, Y:batch_y, dropout_keep_prob:0.5})
print(e, i, loss_)
# 保存模型
if e % 50 == 0:
saver.save(sess, "name2sex.model", global_step=e)
train_neural_network()
# 使用训练的模型
def detect_sex(name_list):
x = []
for name in name_list:
name_vec = []
for word in name:
name_vec.append(vocab.get(word))
while len(name_vec) < max_name_length:
name_vec.append(0)
x.append(name_vec)
output = neural_network(len(vocabulary_list))
saver = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
# 恢复前一次训练
ckpt = tf.train.get_checkpoint_state('.')
if ckpt != None:
print(ckpt.model_checkpoint_path)
saver.restore(sess, ckpt.model_checkpoint_path)
else:
print("没找到模型")
predictions = tf.argmax(output, 1)
res = sess.run(predictions, {X:x, dropout_keep_prob:1.0})
i = 0
for name in name_list:
print(name, '女' if res[i] == 0 else '男')
i += 1
detect_sex(["白富美", "高帅富", "王婷婷", "田野"])
|
执行结果:
服务器又该续费了,如果你要使用DigitalOcean VPS,欢迎使用网页底部的链接注册,你会免费获赠10刀。另外,感谢各位码友的支持。
如要转载,请保持本文完整,并注明作者@斗大的熊猫和本文原始地址: http://blog.topspeedsnail.com/archives/10833
TensorFlow练习18: 根据姓名判断性别相关推荐
- TensorFlow练习16: 根据大脸判断性别和年龄
本帖使用TensorFlow做一个根据脸部推断照片人物年龄和性别的练习,网上有很多类似app. 训练数据 – Adience数据集 Adience数据集来源为Flickr相册,由用户使用iPhone或 ...
- 神经网络算法实战——根据姓名判断性别
"根据姓名判断性别"实战. 1.数据读入及处理 本次实验数据取自该书提供的数据集,可自 [http://file.hankcs.com/corpus/cnname.zip] 下载 ...
- tensorflow证件照判断性别
证件照判断性别 说明 本文用1000张身份证号命名的证件照作为训练数据,复现tensorflow官方教程中的图像分类例子 教程中是多分类,本文改为了二分类. 官方教程地址:https://tensor ...
- js根据身份证号码判断性别和年龄
/**这是一个超实用的用js根据身份证号码判断性别男女的特效代码,详细看下面的*/ /** 根据身份证号码判断性别 15位身份证号码:第7.8位为出生年份(两位数),第9.10位为出生月份,第11.1 ...
- java随机姓名_Java生成随机姓名、性别和年龄的实现示例
一.定义实体类Person,封装生成的数据 package net.dc.test; public class Person { private String name; private String ...
- js面试题:创建一个json对象people,并追加属性:姓名、性别、年龄,追加run方法...
创建一个json对象people,并追加属性:姓名.性别.年龄,追加run方法 因为json的格式是"名称:值"这样的键值对,所以此题的解决方式如下: <script typ ...
- 设计一个person类java_定义一个Person类,含姓名、性别、年龄等字段;继承Person类设计...
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 package test2; public class Person { int age; String name; String gender; pub ...
- java 姓名_Java生成随机姓名、性别和年龄的实现示例
一.定义实体类Person,封装生成的数据 package net.dc.test; public class Person { private String name; private String ...
- java person类_定义一个Person类,含姓名、性别、年龄等字段;继承Person类设计
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 package test2; public class Person { int age; String name; String gender; pub ...
最新文章
- javascript RegExp
- 生态伙伴 | Worktile入驻飞书,助力企业轻松实现敏捷开发与协作
- 我和美国 AI 博士聊了聊:2020 年,这件事比存钱更重要!
- 缓存中常见的概念及解决方案
- 通俗地解释脏读、不可重复读、幻读
- kindeditor图片批量上传失败问题
- 大学物理光学思维导图_在线思维导图软件安利:简单、方便画图,大学生、小学生都能用...
- SQL Server中删除重复数据的几个方法
- linux邮件收发程序流程图,[源码和文档分享]基于C语言和TCP Socket实现的Linux环境下的邮件收发客户端程序...
- 解压出来的文件md5会改变吗_监控Linux文件变化,防止系统被黑
- html+默认ie11,IE11浏览器怎么设置默认浏览器
- 美国对华贸易逆差及其为何如此之高
- opencv滤镜-素描
- Django模型系统(一)
- 软件测试常用的工具都在这里了
- A2-02-24.DML- Inserting Data into A Table Using MySQL INSERT Statement
- python中父子进程
- 模拟电子技术(六)信号的运算与处理
- gevent RecursionError
- 如何将本地图片变为网络图片
热门文章
- komodo edit linux,Komodo Edit是什么
- rocktmq 消息延时清空_使用Kotlin+RocketMQ实现延时消息的示例代码
- 32位md5解密_用户名与密码前后加密、后台解密实现方案
- Java多线程之线程池的使用示例
- 【物联网智能网关-14】Html5:Canvas+WebSocket实现远程实时通信(下)
- atitit. 文件上传带进度条 atiUP 设计 java c# php
- HDU 5115 Dire Wolf ——(区间DP)
- 服务端客户端QA:协作对接沟通流程及优化
- docker nodejs 基本应用
- JFinal Extensions 2.0 发布,JFinal 扩展