tensorflow实现分类问题classification

数据与预测目标:

x是在2和-2附近的正态分布,y是0和1,y=0代表x更接近2,也就是x是正数,y=1代表x是负数。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
tf.set_random_seed(2)
np.random.seed(2)

########################################################################
#demo1:classification
#fake data

n_data = np.ones((100,2))
x0 = np.random.normal(2*n_data,1)
x1 = np.random.normal(-2*n_data,1)
y0 = np.zeros(100)
y1 = np.ones(100)

y = np.hstack([y0,y1])
x = np.vstack([x0,x1])
#plot data
#plt.scatter(x[:,0],x[:,1],c='red')
plt.scatter(x[:,0],x[:,1],c=y,s=80,lw=0,cmap='RdYlGn')#standard demo
#just test,there is tips in pycharm    #cmap='BrBG'
plt.show()

#nn layers
tf_x = tf.placeholder(tf.float32,x.shape)
tf_y = tf.placeholder(tf.int32,y.shape)

l1 = tf.layers.dense(tf_x,10,tf.nn.relu)
outputs = tf.layers.dense(l1,2)#logits,not one-hot,not softmax

#train
#tf.losses.softmax_cross_entropy
loss = tf.losses.sparse_softmax_cross_entropy(labels=tf_y,logits=outputs)
accuracy = tf.metrics.accuracy(labels=tf_y,predictions=tf.argmax(outputs,axis=1))
optimizer = tf.train.GradientDescentOptimizer(0.1)
train_step = optimizer.minimize(loss)

plt.ion()
init = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())#accuracy needs initial variable
with tf.Session() as sess:
    sess.run(init)#tf.layers need initital!!!
    for i in range(100):
        _,loss_,acura_,pred_ = sess.run([train_step,loss,accuracy,outputs],{tf_x:x, tf_y:y})
        if i % 10 == 0:

            print('loss:',loss_,' accuracy:',acura_[1])
            plt.cla()
            plt.scatter(x[:, 0], x[:, 1], c=pred_.argmax(1), s=100, lw=0, cmap='RdYlGn')
            plt.text(1.5, -4, 'Accuracy=%.2f' % acura_[1], fontdict={'size': 20, 'color': 'red'})
            plt.pause(0.1)
    plt.ioff()
    plt.show()

下边是把label换成one-hot的形式:y增维并且用vstack拼接。

y=[1,0]代表y=0,y=[0,1]代表y=1

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
tf.set_random_seed(2)
np.random.seed(2)


######################################################################
#deme2:one_hot,i will change y to one_hot

#fake data
n_data = np.ones((100,2))
x0 = np.random.normal(2*n_data,1)
x1 = np.random.normal(-2*n_data,1)
y0 = np.concatenate((np.ones(100)[:,np.newaxis],np.zeros(100)[:,np.newaxis]),axis=1)
y1 = np.concatenate((np.zeros(100)[:,np.newaxis],np.ones(100)[:,np.newaxis]),axis=1)

y = np.vstack([y0,y1])
x = np.vstack([x0,x1])

#nn layers
tf_x = tf.placeholder(tf.float32,x.shape)
tf_y = tf.placeholder(tf.int32,y.shape)

l1 = tf.layers.dense(tf_x,10,tf.nn.relu)
outputs = tf.layers.dense(l1,2)#logits,not one-hot,not softmax

#train
#tf.losses.softmax_cross_entropy
loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y,logits=outputs)#need onehot_labels  y[x0] = [1,0]
accuracy = tf.metrics.accuracy(          # return (acc, update_op), and create 2 local variables
    labels=tf.argmax(tf_y,axis=1), predictions=tf.argmax(outputs, axis=1),)[1]
optimizer = tf.train.GradientDescentOptimizer(0.1)
train_step = optimizer.minimize(loss)

init = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())#accuracy needs initial variable
with tf.Session() as sess:
    sess.run(init)#tf.layers need initital!!!
    for i in range(100):
        _,loss_,acura_ = sess.run([train_step,loss,accuracy],{tf_x:x, tf_y:y})
        if i % 10 == 0:
            print('loss:',loss_,' accuracy:',acura_)

猜你喜欢

转载自blog.csdn.net/huqinweI987/article/details/82800442