手把手教你用 TensorFlow 实现文本分类（下）

● 随机打乱训练数据 ● 增加隐层，和验证集 ● 正则化 ● 对原数据进行PCA预处理 ● 调节训练参数（迭代次数，batch大小等）

随机化训练数据

1、将labels加到trian.txt的第一列

paste -d" " train_labels.txt train.txt > train_to_shuf.txt

2、随机打乱文件行

shuf train_to_shuf.txt -o train.txt

3、 提取打乱后文件的第一列，保存到train_labels.txt

cat train.txt | awk '{print \$1}' > train_labels.txt

4、删除第一列label.

awk '{\$1="";print \$0}' train.txt

正则化，改善过拟合

#!/usr/bin/python #-*-coding:utf-8-*- LAYER_NODE1 = 500 # layer1 node num INPUT_NODE = 5000 OUTPUT_NODE = 10 REG_RATE = 0.01 import tensorflow as tf from datasets import datasets def interface(inputs, w1, b1, w2,b2): """ compute forword progration result """ lay1 = tf.nn.relu(tf.matmul(inputs, w1) + b1) return tf.nn.softmax(tf.matmul(lay1, w2) + b2) # need softmax?? data_sets = datasets() data_sets.read_train_data(".", True) sess = tf.InteractiveSession() x = tf.placeholder(tf.float32, [None, INPUT_NODE], name="x-input") y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name="y-input") w1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER_NODE1], stddev=0.1)) b1 = tf.Variable(tf.constant(0.0, shape=[LAYER_NODE1])) w2 = tf.Variable(tf.truncated_normal([LAYER_NODE1, OUTPUT_NODE], stddev=0.1)) b2 = tf.Variable(tf.constant(0.0, shape=[OUTPUT_NODE])) y = interface(x, w1, b1, w2, b2) cross_entropy = -tf.reduce_sum(y_ * tf.log(y + 1e-10)) regularizer = tf.contrib.layers.l2_regularizer(REG_RATE) regularization = regularizer(w1) + regularizer(w2) loss = cross_entropy + regularization train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss) #training tf.global_variables_initializer().run() saver = tf.train.Saver() cv_feed = {x: data_sets.cv.text, y_: data_sets.cv.label} correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) for i in range(5000): if i % 200 == 0: cv_acc = sess.run(acc, feed_dict=cv_feed) print "train steps: %d, cv accuracy is %g " % (i, cv_acc) batch_xs, batch_ys = data_sets.train.next_batch(100) train_step.run({x: batch_xs, y_: batch_ys}) path = saver.save(sess, "./model4/model.md")

PCA处理

#!/usr/bin/python #-*-coding:utf-8-*- """ PCA for datasets """ import os import sys import commands import numpy from contextlib import nested from datasets import datasets ORIGIN_DIM = 5000 def pca(origin_mat): """ gen matrix using pca row of origin_mat is one sample of dataset col of origin_mat is one feature return matrix U, s and V """ # mean,normaliza1on avg = numpy.mean(origin_mat, axis=0) # covariance matrix cov = numpy.cov(origin_mat-avg,rowvar=0) #Singular Value Decomposition U, s, V = numpy.linalg.svd(cov, full_matrices=True) k = 1; sigma_s = numpy.sum(s) # chose smallest k for 99% of variance retained for k in range(1, ORIGIN_DIM+1): variance = numpy.sum(s[0:k]) / sigma_s print "k = %d, variance is %f" % (k, variance) if variance >= 0.99: break if k == ORIGIN_DIM: print "some thing unexpected , k is same as ORIGIN_DIM" exit(1) return U[:, 0:k], k if __name__ == '__main__': """ main, read train.txt, and do pca save file to train_pca.txt """ data_sets = datasets() train_text, _ = data_sets.read_from_disk(".", "train", one_hot=False) U, k = pca(train_text) print "U shpae: ", U.shape print "k is : ", k text_pca = numpy.dot(train_text, U) text_num = text_pca.shape[0] print "text_num in pca is ", text_num with open("./train_pca.txt", "a+") as f: for i in range(0, text_num): f.write(" ".join(map(str, text_pca[i,:])) + "\n")

0 条评论

相关文章

吴恩达斯坦福CS230第一名：图像超级补全，效果惊艳（附代码）

【新智元导读】图像修复（Image inpainting）是一个已经被广泛研究的计算机视觉问题，即恢复图像中缺失的部分。斯坦福大学CS230课程的Mark Sa...

1363

2461

4286

4505

4506

1382

AI 技术讲座精选：​通过学习Keras从零开始实现VGG网络

Keras代码示例多达数百个。通常我们只需复制粘贴代码，而无需真正理解这些代码。通过学习本教程，您将搭建非常简单的构架，但是此过程会带给您些许好处：您将通过阅读...

3748

33211

3636

3656