简单神经网络预测结构化数据关系___训练集(调参实验)

# coding: utf-8
import tensorflow as tf
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
from sklearn.preprocessing import scale
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.utils import shuffle

num_classes=2
data=pd.DataFrame(pd.read_csv('/home/henson/Desktop/huanping/huanping.csv_EDGE_NBD.csv',encoding='gb18030'))
#data=pd.DataFrame(pd.read_csv('sele_test.csv',encoding='utf-8'))
data.head()
sess = tf.Session()

data=shuffle(data)
X = np.array(data[['author_degree1','author_degree2','No']])
y = np.array(data[['isBD']])

y=y[:,0]
y = (np.arange(2) == y[:,None]).astype(np.float32)

#print(X,y)

#print(y)

#y = tf.one_hot(y, depth=2)
#print(y)
#y_train = tf.cast(y, tf.int32)

#print(y_train)
#print(X,y[:,0])

#StandardScaler= StandardScaler()
#X_Standard = StandardScaler.fit_transform(X)
X_train=X
#y_Standard = StandardScaler.fit_transform(y)

"""
X_train,X_test,y_train,y_test = train_test_split(X_Standard,y,test_size=0,random_state=0)
X_train = scale(X_train)
X_test = scale(X_test)
"""
#y_train = scale(y.reshape((-1,1)))
#y_test = scale(y_test.reshape((-1,1)))


def add_layer(inputs,input_size,output_size,activation_function=None):
    with tf.variable_scope("Weights"):
        Weights = tf.Variable(tf.random_normal(shape=[input_size,output_size]),name="weights")


    with tf.variable_scope("biases"):
        biases = tf.Variable(tf.zeros(shape=[1,output_size]) + 0.1,name="biases")

    with tf.name_scope("Wx_plus_b"):
        Wx_plus_b = tf.matmul(inputs,Weights) + biases

    with tf.name_scope("dropout"):
        Wx_plus_b = tf.nn.dropout(Wx_plus_b,keep_prob=keep_prob_s)
        if activation_function is None:
            return Wx_plus_b
        else:
            with tf.name_scope("activation_function"):
                return activation_function(Wx_plus_b)


xs = tf.placeholder(shape=[None,X_train.shape[1]],dtype=tf.float32,name="inputs")
ys = tf.placeholder(shape=[None,2],dtype=tf.float32)
#ys = tf.placeholder(shape=[None,num_classes],dtype=tf.float32)

print(ys.shape)
keep_prob_s = tf.placeholder(dtype=tf.float32)

with tf.name_scope("layer_1"):
    l1 = add_layer(xs,3,10,activation_function=tf.nn.relu)

with tf.name_scope("layer_2"):#
    l2 = add_layer(l1,10,10,activation_function=tf.nn.relu)
with tf.name_scope("y_pred"):
    #pred = add_layer(l1,10,1)
    logits = add_layer(l2, 10, num_classes)
    print("logits:",logits)

with tf.name_scope("loss"):
    #loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - logits),reduction_indices=[1]))
    #loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=ys,logits=tf.argmax(logits,1)))
    #loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=ys, logits=logits))
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=ys, logits=logits))

tf.summary.scalar("loss",tensor=loss)

with tf.name_scope("train"):
    train_op =tf.train.GradientDescentOptimizer(learning_rate=0.03).minimize(loss)
    #train_op = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)
    correct_prediction = tf.equal(tf.arg_max(logits, 1), tf.arg_max(ys, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


def fit(X, y, n, keep_prob):
    init = tf.global_variables_initializer()
    #feed_dict_train = {ys:y[:,:], xs: X, keep_prob_s: keep_prob}
    feed_dict_train = {ys: y, xs: X, keep_prob_s: keep_prob}
    with tf.Session() as sess:
        saver = tf.train.Saver(tf.global_variables(), max_to_keep=15)
        merged = tf.summary.merge_all()
        writer = tf.summary.FileWriter(logdir="nn_huangping_log", graph=sess.graph)  #写tensorbord
        sess.run(init)
        for i in range(n):
            _loss, _ = sess.run([loss, train_op], feed_dict=feed_dict_train)
            if i % 100 == 0:
                print("epoch:%d/tloss:%.5f " % (i, _loss))
                acc= sess.run(accuracy, feed_dict=feed_dict_train)
                print(acc)
                rs = sess.run(merged, feed_dict=feed_dict_train)
                writer.add_summary(summary=rs, global_step=i)  #写tensorbord
                saver.save(sess=sess, save_path="model/nn_huanping.model", global_step=i) # 保存模型

fit(X_train, y,10000, 0.5)

#调整阈值 tf.arg_max(logits, 1)???

Q1:
要绘制ROC和AUC图,
想通过sklearn
sklearn.metrics.roc_curve(y_true,y_score, pos_label=None, sample_weight=None, drop_intermediate=True)
但是不理解这个y_score怎么获取,怎么获取各个样本属于正例的概率,也就是各样本的权值

Q2:正负样本 1:99
通过将正样本重复多次,打乱顺序参与分类,但是效果不是很明显

Q3: 在Q2策略(正负=1:4)的基础上,对keep_prob:0.8,正负混合的样本进行预测,整体的准确率下降,调整了keep_prob,使其偏向于预测成正样本1更多些,对1分配的权重多些,用来预测全为正样本的准确率略有上升。
但是出现准确率为0.20+-,预测为正样本的情况只出现在参与预测样本的前百分之二十。

猜你喜欢

转载自blog.csdn.net/hensonwells/article/details/79645120