tensorflow实现分类流程
生成样本集
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from sklearn.utils import shuffle
def generate(sample_size,mean,cov,diff,regression):
num_classes=2
sample_per_class=int(sample_size/2)
X0=np.random.multivariate_normal(mean,cov,sample_per_class)
Y0=np.zeros(sample_per_class)
#print("X0:",X0)
#print("Y0:",Y0)
for ci,d in enumerate(diff):
#print("-------------------ci,d---------------------:")
#print("ci:",ci)
#print("d:",d)
#print("--------------------X1:y1------------------------:")
X1=np.random.multivariate_normal(mean+d,cov,sample_per_class)
Y1=(ci+1)*np.ones(sample_per_class)
#print("X1:",X1)
#print("Y1:",Y1)
X0=np.concatenate((X0,X1))
Y0=np.concatenate((Y0,Y1))
#print("---------------------X0,Y0-------------------------:")
#print("X0:",X0)
#print("Y0:",Y0)
#print("--------------------X,Y--------------------------:")
if regression==False:
class_id=[Y1==class_number for class_number in range(num_classes)]
Y=np.asarray(np.hstack(class_id),dtype=np.float32)
X,Y=shuffle(X0,Y)
else:
X,Y=shuffle(X0,Y0)
#print("X:",X)
#print("Y:",Y)
#print("------------------------------------------------:")
return X,Y
样本可视化
np.random.seed(10)
num_classes=2
mean=np.random.randn(num_classes)
cov=np.eye(num_classes)
X,Y=generate(1000,mean,cov,[3.0],True)
#print("mean:",mean)
#print("cov:",cov)
#print("X:",X)
#print("Y:",Y)
colors=['r' if l==0 else 'b' for l in Y[:]]
plt.scatter(X[:,0],X[:,1],c=colors)
plt.show()
模拟数据
使用tensorflow进行分类
#定义维度
lab_dim=1
input_dim=2
#print(input_dim)
#定义占位符数据
input_features=tf.placeholder(tf.float32,[None,input_dim])
input_labels=tf.placeholder(tf.float32,[None,lab_dim])
#定义变量
W=tf.Variable(tf.random_normal([input_dim,lab_dim]),name="weight")
b=tf.Variable(tf.zeros([lab_dim],name="bias"))
#输出数据
output=tf.nn.sigmoid(tf.matmul(input_features,W)+b)
#交叉熵
coross_entropy=-(input_labels*tf.log(output)+(1-input_labels)*tf.log(1-output))
#误差
ser=tf.square(input_labels-output)
#损失函数
loss=tf.reduce_mean(coross_entropy)
#误差均值
err=tf.reduce_mean(ser)
#优化器
optimizer=tf.train.AdamOptimizer(0.04)
train=optimizer.minimize(loss)
maxEpochs=50
minibatchSize=25
with tf.Session() as sess:
#初始化所有变量与占位符
sess.run(tf.global_variables_initializer())
for epoch in range(maxEpochs):
sumerr=0
#对于每一个batch
for i in range(np.int32(len(Y)/minibatchSize)):
#取出X值
x1=X[i*minibatchSize:(i+1)*minibatchSize,:]
#取出Y值
y1=np.reshape(Y[i*minibatchSize:(i+1)*minibatchSize],[-1,1])
#改变y的数据结构,变成tensor数据
tf.reshape(y1,[-1,1])
#对相关结果进行计算
_,lossval,outputval,errval=sess.run([train,loss,output,err],feed_dict={input_features:x1,input_labels:y1})
#计算误差和
sumerr=sumerr+errval
print("epoch:",epoch)
print("cost=",lossval,"err=",sumerr)
#结果可视化
train_X, train_Y = generate(100, mean, cov, [3.0],True)
colors = ['r' if l == 0 else 'b' for l in train_Y[:]]
plt.scatter(train_X[:,0], train_X[:,1], c=colors)
#plt.scatter(train_X[:, 0], train_X[:, 1], c=train_Y)
#plt.colorbar()
# x1w1+x2*w2+b=0
# x2=-x1* w1/w2-b/w2
x = np.linspace(-1,8,200)
y=-x*(sess.run(W)[0]/sess.run(W)[1])-sess.run(b)/sess.run(W)[1]
plt.plot(x,y, label='Fitted line')
plt.legend()
plt.show()
epoch: 0
cost= 0.28224963 err= 5.835677430033684
epoch: 1
cost= 0.1841161 err= 2.9293049834668636
epoch: 2
cost= 0.13622522 err= 1.8027594378218055
epoch: 3
cost= 0.10920071 err= 1.321009835228324
epoch: 4
cost= 0.09234682 err= 1.0732473297975957
epoch: 5
cost= 0.081041396 err= 0.926894772797823
epoch: 6
cost= 0.07300299 err= 0.8318855110555887
epoch: 7
cost= 0.06699502 err= 0.7659151882398874
epoch: 8
cost= 0.062306933 err= 0.7177525935694575
epoch: 9
cost= 0.058514904 err= 0.6812087508151308
epoch: 10
cost= 0.055357553 err= 0.6526279035024345
epoch: 11
cost= 0.052668083 err= 0.6297260934952646
epoch: 12
cost= 0.050336055 err= 0.6110085289692506
epoch: 13
cost= 0.048285052 err= 0.5954574717907235
epoch: 14
cost= 0.046460498 err= 0.5823574732639827
epoch: 15
cost= 0.044822298 err= 0.571191034920048
epoch: 16
cost= 0.0433398 err= 0.5615753585589118
epoch: 17
cost= 0.04198938 err= 0.5532208279473707
epoch: 18
cost= 0.040752184 err= 0.5459049143246375
epoch: 19
cost= 0.039613225 err= 0.5394539169501513
epoch: 20
cost= 0.038560122 err= 0.533729906193912
epoch: 21
cost= 0.037582446 err= 0.5286223700095434
epoch: 22
cost= 0.03667196 err= 0.5240421258495189
epoch: 23
cost= 0.035821244 err= 0.5199156226881314
epoch: 24
cost= 0.03502427 err= 0.5161824229871854
epoch: 25
cost= 0.034275606 err= 0.5127919654041762
epoch: 26
cost= 0.03357083 err= 0.5097018567466876
epoch: 27
cost= 0.032905858 err= 0.506876300656586
epoch: 28
cost= 0.032277178 err= 0.5042847362201428
epoch: 29
cost= 0.031681698 err= 0.5019011745171156
epoch: 30
cost= 0.031116951 err= 0.49970316601684317
epoch: 31
cost= 0.030580308 err= 0.4976714387157699
epoch: 32
cost= 0.030069621 err= 0.49578892458521295
epoch: 33
cost= 0.029582985 err= 0.4940409708506195
epoch: 34
cost= 0.029118799 err= 0.49241474875452695
epoch: 35
cost= 0.028675303 err= 0.4908985588190262
epoch: 36
cost= 0.028251264 err= 0.48948282841593027
epoch: 37
cost= 0.027845193 err= 0.4881584036847926
epoch: 38
cost= 0.027456209 err= 0.48691741812217515
epoch: 39
cost= 0.027083045 err= 0.48575285974220606
epoch: 40
cost= 0.026724808 err= 0.4846585850318661
epoch: 41
cost= 0.02638059 err= 0.48362843324866844
epoch: 42
cost= 0.026049538 err= 0.4826579857908655
epoch: 43
cost= 0.025730899 err= 0.4817419640312437
epoch: 44
cost= 0.02542402 err= 0.4808764703484485
epoch: 45
cost= 0.025128283 err= 0.4800582237949129
epoch: 46
cost= 0.024843078 err= 0.4792831062586629
epoch: 47
cost= 0.024567802 err= 0.4785483961677528
epoch: 48
cost= 0.024302106 err= 0.4778511731637991
epoch: 49
cost= 0.02404523 err= 0.4771889972980716
结果
欢迎小伙伴留言讨论
网友评论