tensorflow 多分类

3类

    • hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
    • cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
    • logits = tf.matmul(X, W) + b
    • cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_oneHot))
      softmax交叉熵 softmax和代价函数封装到一起
import tensorflow as tf
tf.set_random_seed(777) #设置随机种子
#定义数据集
x_data = [[1, 2, 1, 1],
          [2, 1, 3, 2],
          [3, 1, 3, 4],
          [4, 1, 5, 5],
          [1, 7, 5, 5],
          [1, 2, 5, 6],
          [1, 6, 6, 6],
          [1, 7, 7, 7]]
y_data = [[0, 0, 1],
          [0, 0, 1],
          [0, 0, 1],
          [0, 1, 0],
          [0, 1, 0],
          [0, 1, 0],
          [1, 0, 0],
          [1, 0, 0]]
#定义占位符
X = tf.placeholder("float", shape=[None, 4])
Y = tf.placeholder(tf.float32, shape=[None, 3])

#权重和偏置
W = tf.Variable(tf.random_normal([4, 3]), name='weight')
b = tf.Variable(tf.random_normal([3]), name='bias')

#预测模型
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)     # 多分类
# hypothesis = tf.matmul(X, W) + b      # 二分类

#代价或损失函数
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))

#梯度下降优化器
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

#准确率计算
prediction = tf.argmax(hypothesis, axis=1)
correct_prediction = tf.equal(prediction, tf.argmax(y_data, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#创建会话
sess = tf.Session()
sess.run(tf.global_variables_initializer()) #全局变量初始化
#迭代训练
for step in range(5001):
    cost_val, _, acc = sess.run([cost, train, accuracy], feed_dict={X: x_data, Y: y_data})
    if step % 500 == 0:# 显示损失值收敛情况
        print(step, cost_val, acc)
#准确率
h, c, a = sess.run([hypothesis, prediction, accuracy], feed_dict={X: x_data, Y: y_data})
print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)
#测试
h1, p1 = sess.run([hypothesis, prediction], feed_dict={X: [[1, 2, 3, 4]]})
print(h1, p1)
h2, p2 = sess.run([hypothesis, prediction], feed_dict={X: [[4,1,2,3], [3,2,4,5]]})
print(h2, '\n', p2)
while True:
    str = input()
    try:
        if str == 'exit':
            break
        test = list(map(float,str.split(',')))
        h1, p1 = sess.run([hypothesis, prediction], feed_dict={X: [test]})
        print(h1, p1)
    except:
        continue

7类

import tensorflow as tf, numpy

tf.set_random_seed(1)

data = numpy.loadtxt(r'G:\A_深度学习1\tensorflow\data-04-zoo.csv', delimiter=',')

x_data = data[:, :-1]   # (101, 16)
y_data = data[:, [-1]]  # (101, 1)
n_classes = 7   # 7个类别


# 定义占位符
X, Y = tf.placeholder('float', shape=[None, 16]), tf.placeholder(tf.int32, shape=[None, 1])

# 用one-hot编码,将类别映射到0-1
Y_oneHot = tf.one_hot(Y, n_classes)
'''
[
    [[1,0,0,0,0,0,0]]   0
    [[0,1,0,0,0,0,0]]   1
]
处理完后是三维的,需要变成二维
'''
Y_oneHot = tf.reshape(Y_oneHot, [-1, n_classes])
'''
[
    [1,0,0,0,0,0,0]
    [0,1,0,0,0,0,0]
]
'''

# 定义权重和偏置
W, b = tf.Variable(tf.random_normal([16, n_classes])), tf.Variable(tf.random_normal([n_classes]))

logits = tf.matmul(X, W) + b

# h = tf.nn.softmax(logits)
# cost = tf.reduce_mean(-tf.reduce_sum(Y_oneHot*tf.log(h)), axis=1)     # 有些问题
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_oneHot))

train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

#准确率计算
prediction = tf.argmax(logits, 1)
correct_prediction = tf.equal(prediction, tf.argmax(Y_oneHot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(1501):
        cost_val, _, acc = sess.run([cost, train, accuracy], feed_dict={X: x_data, Y: y_data})
        if step % 100 == 0:  # 显示损失值收敛情况
            print(step, cost_val, acc)
    # 准确率
    h, c, a = sess.run([logits, prediction, accuracy], feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h[:3], "\nCorrect (Y): ", c[:3], "\nAccuracy: ", a)
    # 测试
    h1, p1 = sess.run([logits, prediction],
                      feed_dict={X: [[1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 8, 1, 0, 1]]})
    print(h1, p1)


深层神经网路

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WGS.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值