[TensorFlow深度学习入门]实战四·逻辑回归鸢尾花进行分类(对比均方根误差与softmax交叉熵误差区别)

[TensorFlow深度学习入门]实战四·逻辑回归鸢尾花进行分类

  • 问题描述
    数据集
    鸢尾花数据集下载地址
    鸢尾花数据集包含四个特征和一个标签。这四个特征确定了单株鸢尾花的下列植物学特征:
    1、花萼长度
    2、花萼宽度
    3、花瓣长度
    4、花瓣宽度

该标签确定了鸢尾花品种,品种必须是下列任意一种:

山鸢尾 (0)
变色鸢尾 (1)
维吉尼亚鸢尾 (2)

  • 代码
    使用均方根误差
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf


file_path = "iris.csv"
df_iris = pd.read_csv(file_path, sep=",", header="infer")
np_iris = df_iris.values
np.random.shuffle(np_iris)

def normalize(temp):
    temp = 2*(temp - np.mean(temp,axis = 0))/(np.max(temp,axis = 0)-np.min(temp,axis = 0))
    return(temp)

def convert2onehot(data):
    # covert data to onehot representation
    return pd.get_dummies(data)

xs = normalize(np_iris[:,1:5]).astype(np.double)
ys = convert2onehot(np_iris[:,-1]).values

x = tf.placeholder(tf.float32,[None,4])
y_ = tf.placeholder(tf.float32,[None,3])

w1 = tf.get_variable("w1",initializer=tf.random_normal([4,64]))
w2 = tf.get_variable("w2",initializer=tf.random_normal([64,3]))
b1 = tf.get_variable("b1",initializer=tf.zeros([1,64]))
b2 = tf.get_variable("b2",initializer=tf.zeros([1,3]))
l1 = tf.matmul(x,w1)+b1
l1 = tf.nn.elu(l1)

y = tf.matmul(l1,w2)+b2

loss = tf.reduce_mean(tf.square(y-y_))
opt = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

with tf.Session() as sess:
    srun = sess.run
    init = tf.global_variables_initializer()
    srun(init)
    
    for e in range(6001):
        loss_val,_ = srun([loss,opt],{x:xs[:90,:],y_:ys[:90,:]})
        if(e%400 ==0):
            print("%d steps loss is %f"%(e,loss_val))
    ys_pre = srun(y,{x:xs[90:,:]})
    result = (np.argmax(ys_pre,axis=1) == np.argmax(ys[90:,:],axis=1))
    print(np.sum(result)/60)
  • 结果
    log
0 steps loss is 62.941807
400 steps loss is 0.056762
800 steps loss is 0.039173
1200 steps loss is 0.032764
1600 steps loss is 0.029213
2000 steps loss is 0.026903
2400 steps loss is 0.025220
2800 steps loss is 0.023925
3200 steps loss is 0.022888
3600 steps loss is 0.022027
4000 steps loss is 0.021291
4400 steps loss is 0.020648
4800 steps loss is 0.020077
5200 steps loss is 0.019560
5600 steps loss is 0.019088
6000 steps loss is 0.018654
0.9933333333333333
  • 使用softmax
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf


file_path = "./0DNN/iris.csv"
df_iris = pd.read_csv(file_path, sep=",", header="infer")
np_iris = df_iris.values
np.random.shuffle(np_iris)

def normalize(temp):
    temp = 2*(temp - np.mean(temp,axis = 0))/(np.max(temp,axis = 0)-np.min(temp,axis = 0))
    return(temp)

def convert2onehot(data):
    # covert data to onehot representation
    return pd.get_dummies(data)

xs = normalize(np_iris[:,1:5]).astype(np.double)
ys = convert2onehot(np_iris[:,-1]).values

x = tf.placeholder(tf.float32,[None,4])
y_ = tf.placeholder(tf.float32,[None,3])

w1 = tf.get_variable("w1",initializer=tf.random_normal([4,64],stddev=1))
w2 = tf.get_variable("w2",initializer=tf.random_normal([64,3],stddev=1))
b1 = tf.get_variable("b1",initializer=tf.zeros([1,64])+0.01)
b2 = tf.get_variable("b2",initializer=tf.zeros([1,3])+0.01)
l1 = tf.nn.elu(tf.matmul(x,w1)+b1)


"""y = tf.matmul(l1,w2)+b2

loss = tf.reduce_mean(tf.square(y-y_))
opt = tf.train.GradientDescentOptimizer(0.05).minimize(loss)"""

y = tf.nn.softmax(tf.matmul(l1,w2)+b2)

y = tf.clip_by_value(y,1e-4,10)
cross_entropy = -tf.reduce_mean(tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
loss = cross_entropy
opt = tf.train.GradientDescentOptimizer(0.05).minimize(loss)


with tf.Session() as sess:
    srun = sess.run
    init = tf.global_variables_initializer()
    srun(init)
    
    for e in range(6001):
        loss_val,_ = srun([loss,opt],{x:xs[:,:],y_:ys[:,:]})
        if(e%400 ==0):
            print("%d steps loss is %f"%(e,loss_val))
    ys_pre = srun(y,{x:xs[:,:]})
    result = (np.argmax(ys_pre,axis=1) == np.argmax(ys[:,:],axis=1))
    print(np.sum(result)/150)


  • 输出结果
    log:
0 steps loss is 3.956946
400 steps loss is 0.049743
800 steps loss is 0.043666
1200 steps loss is 0.041287
1600 steps loss is 0.039875
2000 steps loss is 0.038858
2400 steps loss is 0.038027
2800 steps loss is 0.037303
3200 steps loss is 0.036683
3600 steps loss is 0.036195
4000 steps loss is 0.035810
4400 steps loss is 0.035505
4800 steps loss is 0.035253
5200 steps loss is 0.035039
5600 steps loss is 0.034851
6000 steps loss is 0.034681
0.9866666666666667
发布了168 篇原创文章 · 获赞 398 · 访问量 19万+
展开阅读全文

softmax分类器,分类概率都是nan是怎么回事?

04-13

训练结果有测试集和训练集的准确率,但是调取测试集的预测值时,全都是nan,我需要得到测试结果实际应用的。请问怎么解决?拿一个二分类为例 以下是前期搭建框架 import numpy as np import tensorflow as tf x=tf.placeholder("float", [None,115]) y=tf.placeholder("float", [None,2]) W=tf.Variable(tf.zeros([115,2])) b=tf.Variable(tf.zeros([2])) actv= tf.nn.softmax(tf.matmul(x,W)+b) ####网上查到说下面这个loss函数中如果是log(0)的话就会有nan,但那应该连训练结果都没有才对吧?我现在能得到训练结果,但是结果都是nan怎么办? cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1)) learning_rate=0.01 optm= tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) pred=tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1)) accr=tf.reduce_mean(tf.cast(pred,"float")) init=tf.global_variables_initializer() sess=tf.InteractiveSession() sess=tf.Session() sess.run(init) training_lenth=len(G)####(回测长度) training_epochs =50 #训练次数 batch_size = len(G) #每次迭代用多少样本(用全套) ##display_step = 5 #展示 print('Down') 训练重点来了,我需要得到result的实际结果 lenth=2 for epoch in range(training_epochs): avg_cost=0 num_batch=int(len(G)/batch_size) for i in range((lenth-1),lenth): batch_xs=np.array(G[i]) batch_ys=np.array(F[i]) sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) feeds={x:batch_xs, y: batch_ys} avg_cost += sess.run (cost, feed_dict=feeds)/(num_batch*lenth) feeds_train = {x: batch_xs, y: batch_ys} feeds_test = {x: G[i+1], y: F[i+1]} train_acc = sess.run(accr, feed_dict=feeds_train) #feed_dict 针对place holder占位 test_acc = sess.run(accr,feed_dict=feeds_test) result=sess.run(actv,feed_dict=feeds_test) 但是实际给我的结果中,test_acc和train_acc都是有的,但是具体分类的概率值都是nan。 result=sess.run(actv,feed_dict=feeds_train) print (train_acc)# print(test_acc) train_acc a=sess.run([accr,actv],feed_dict=feeds_test) print(a) 0.930233 0.465116 [0.46511629, array([[ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan]], dtype=float32)] 求大神指教,跪送分 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览