tensorflow1.1/构建卷积神经网络识别文本

环境:tensorflow 1.1,python3

#coding:utf-8
import numpy as np
import tensorflow as tf
import pickle
#import matplotlib.pyplot as plt

with open('sentiment_set.pickle','rb') as f:
        [test_data,test_labels,train_data,train_labels] = pickle.load(f)


#shuffle data
np.random.seed(100)
train_data = np.random.permutation(train_data)
np.random.seed(100)
train_labels = np.random.permutation(train_labels)
np.random.seed(200)
train_data = np.random.permutation(test_data)
np.random.seed(200)
train_labels = np.random.permutation(test_labels)


batch_size = 120
learning_rate = 0.01

#词向量有423列
xs = tf.placeholder(tf.float32,[None,423])
ys = tf.placeholder(tf.int32,[None,2])
#keep_prob = tf.placeholder(tf.float32)

#传入卷积神经网络
x = tf.reshape(xs,[-1,47,9,1])
conv1 = tf.layers.conv2d(inputs=x,filters=3,kernel_size=3,strides=1,padding='same',activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(conv1,pool_size=2,strides=2)
conv2 = tf.layers.conv2d(pool1,filters=6,kernel_size=3,strides=1,padding='same',activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(conv2,pool_size=2,strides=2)
flat = tf.reshape(pool2,[-1,2*11*6])
dense = tf.layers.dense(flat,64)
#dropout1 = tf.nn.dropout(dense,keep_prob)
output = tf.layers.dense(dense,2)

loss = tf.losses.softmax_cross_entropy(onehot_labels=ys,logits=output)
train = tf.train.AdamOptimizer(learning_rate).minimize(loss)
accuracy = tf.metrics.accuracy(labels=tf.argmax(ys,axis=1),predictions=tf.argmax(output,axis=1))[1]

with tf.Session() as sess:
    init = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
    sess.run(init)
    for step in range(10000):
        i = 0
        while i < train_data.shape[0]:
            batch_x = train_data[i:i+batch_size]
            batch_y = train_labels[i:i+batch_size]
            i = i+batch_size
            _,c = sess.run([train,loss],feed_dict={xs:batch_x,ys:batch_y})
        if step % 10 ==0:
            acc = sess.run(accuracy,feed_dict={xs:test_data,ys:test_labels})
            print('= = = = = => > > > > >','step:',int(step/10),'loss: %.4f' %c,'accuracy:%.2f' %acc)

结果:

采用卷积神经网络,精度有一定提升,但是提升不高,后续考虑采用word2vec+cnn处理文本分类问题
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值