Tensorflow mlp二分类

只是简单demo 可以看出tensorflow非常简洁,适合快速实验

   

import tensorflow as tf

import numpy as np

import melt_dataset

import sys

from sklearn.metrics import roc_auc_score

   

def init_weights(shape):

return tf.Variable(tf.random_normal(shape, stddev=0.01))

   

def model(X, w_h, w_o):

h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions

return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us

   

batch_size = 50

learning_rate = 0.1

num_iters = 500

hidden_size = 20

   

argv = sys.argv

trainset = argv[1]

testset = argv[2]

   

trX, trY = melt_dataset.load_dense_data(trainset)

print "finish loading train set ",trainset

teX, teY = melt_dataset.load_dense_data(testset)

print "finish loading test set ", testset

   

num_features = trX[0].shape[0]

print 'num_features: ',num_features

print 'trainSet size: ', len(trX)

print 'testSet size: ', len(teX)

print 'batch_size:', batch_size, ' learning_rate:', learning_rate, ' num_iters:', num_iters

   

X = tf.placeholder("float", [None, num_features]) # create symbolic variables

Y = tf.placeholder("float", [None, 1])

   

w_h = init_weights([num_features, hidden_size]) # create symbolic variables

w_o = init_weights([hidden_size, 1])

   

py_x = model(X, w_h, w_o)

   

cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(py_x, Y)) # compute costs

train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # construct an optimizer

predict_op = tf.nn.sigmoid(py_x)

   

sess = tf.Session()

init = tf.initialize_all_variables()

sess.run(init)

   

for i in range(num_iters):

predicts, cost_ = sess.run([predict_op, cost], feed_dict={X: teX, Y: teY})

print i, 'auc:', roc_auc_score(teY, predicts), 'cost:', cost_

for start, end in zip(range(0, len(trX), batch_size), range(batch_size, len(trX), batch_size)):

sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})

   

predicts, cost_ = sess.run([predict_op, cost], feed_dict={X: teX, Y: teY})

print 'final ', 'auc:', roc_auc_score(teY, predicts),'cost:', cost_

   

   

   

python ./mlp.py corpus/feature.normed.rand.12000.0_2.txt corpus/feature.normed.rand.12000.1_2.txt

   

233 auc: 0.932099377357 cost: 0.210673

234 auc: 0.93210173764 cost: 0.210674

235 auc: 0.93210173764 cost: 0.210675

236 auc: 0.932089936225 cost: 0.210676

posted @ 2015-11-21 19:31  阁子  阅读(3835)  评论(0编辑  收藏  举报