当前位置: 首页 > news >正文

用TensorFlow进行逻辑回归(五)

Softmax分类

 #List3-50

%matplotlib inline

import numpy as np

import tensorflow as tf

import matplotlib.pyplot as plt

x1_label0 = np.random.normal(1, 1, (100, 1))

x2_label0 = np.random.normal(1, 1, (100, 1))

x1_label1 = np.random.normal(5, 1, (100, 1))

x2_label1 = np.random.normal(4, 1, (100, 1))

x1_label2 = np.random.normal(8, 1, (100, 1))

x2_label2 = np.random.normal(0, 1, (100, 1))

plt.scatter(x1_label0, x2_label0, c='r', marker='o', s=60)

plt.scatter(x1_label1, x2_label1, c='g', marker='x', s=60)

plt.scatter(x1_label2, x2_label2, c='b', marker='_', s=60)

plt.show()

xs_label0 = np.hstack((x1_label0, x2_label0))

xs_label1 = np.hstack((x1_label1, x2_label1))

xs_label2 = np.hstack((x1_label2, x2_label2))

xs = np.vstack((xs_label0, xs_label1, xs_label2))

labels = np.matrix([[1., 0., 0.]] * len(x1_label0) + [[0., 1., 0.]] * len(x1_label1) + [[0., 0., 1.]] * len(x1_label2))

arr = np.arange(xs.shape[0])

np.random.shuffle(arr)

xs = xs[arr, :]

labels = labels[arr, :]

test_x1_label0 = np.random.normal(1, 1, (10, 1))

test_x2_label0 = np.random.normal(1, 1, (10, 1))

test_x1_label1 = np.random.normal(5, 1, (10, 1))

test_x2_label1 = np.random.normal(4, 1, (10, 1))

test_x1_label2 = np.random.normal(8, 1, (10, 1))

test_x2_label2 = np.random.normal(0, 1, (10, 1))

test_xs_label0 = np.hstack((test_x1_label0, test_x2_label0))

test_xs_label1 = np.hstack((test_x1_label1, test_x2_label1))

test_xs_label2 = np.hstack((test_x1_label2, test_x2_label2))

test_xs = np.vstack((test_xs_label0, test_xs_label1, test_xs_label2))

test_labels = np.matrix([[1., 0., 0.]] * 10 + [[0., 1., 0.]] * 10 + [[0., 0., 1.]] * 10)

num_labels = 3

train_size, num_features = xs.shape

W = tf.Variable(tf.zeros([num_features, num_labels]))

b = tf.Variable(tf.zeros([num_labels]))

learning_rate = 0.01

training_epochs = 1000

batch_size = 100

optimizer = tf.optimizers.SGD(learning_rate)

for step in range(training_epochs * train_size // batch_size):

        offset = (step * batch_size) % train_size

        batch_xs = xs[offset:(offset + batch_size), :]

        batch_labels = labels[offset:(offset + batch_size)]

        X=tf.cast(batch_xs,tf.float32)

        Y=tf.cast(batch_labels,tf.float32)

        with tf.GradientTape() as g:

            y_model = tf.nn.softmax(tf.matmul(X, W) + b)

            cost = -tf.reduce_sum(Y * tf.math.log(y_model))

            gradients = g.gradient(cost, [W, b])

        # 更新梯度

        optimizer.apply_gradients(zip(gradients, [W, b]))

        if step % 100 == 0:

            print (step, cost.numpy())

W_val = W

print('w', W_val.numpy())

b_val = b

print('b', b_val.numpy())

#train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

correct_prediction = tf.equal(tf.argmax(y_model, 1), tf.argmax(Y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

print("accuracy", accuracy.numpy())


文章转载自:
http://ardency.pzdurr.cn
http://blowout.pzdurr.cn
http://bluebonnet.pzdurr.cn
http://aneurism.pzdurr.cn
http://abridgable.pzdurr.cn
http://amperemeter.pzdurr.cn
http://autolysin.pzdurr.cn
http://azeotropism.pzdurr.cn
http://apery.pzdurr.cn
http://bourgeoisie.pzdurr.cn
http://battleplan.pzdurr.cn
http://bressummer.pzdurr.cn
http://ang.pzdurr.cn
http://anaemia.pzdurr.cn
http://adjutantship.pzdurr.cn
http://aegir.pzdurr.cn
http://amate.pzdurr.cn
http://bumblepuppy.pzdurr.cn
http://acerbity.pzdurr.cn
http://articulation.pzdurr.cn
http://brahma.pzdurr.cn
http://canberra.pzdurr.cn
http://arnoldian.pzdurr.cn
http://berth.pzdurr.cn
http://bundu.pzdurr.cn
http://avn.pzdurr.cn
http://brannigan.pzdurr.cn
http://chemigraphy.pzdurr.cn
http://aliform.pzdurr.cn
http://babbittry.pzdurr.cn
http://www.dtcms.com/a/280247.html

相关文章:

  • 从0开始学习R语言--Day47--Nomogram
  • dockerfile 最佳实践
  • 微信小程序开发-桌面端和移动端UI表现不一致问题记录
  • Linux-【单体架构/分布式架构】
  • Web3 学习路线与面试经验
  • 免杀学习篇(1)—— 工具使用
  • 【origin画图】让非等距数值等距显示
  • 前端技术面试
  • git起步
  • linux 内核: 遍历当前所有进程
  • 【科研绘图系列】R语言绘制中国地图和散点图以及柱状图
  • Oracle学习专栏(六):高可用架构
  • 代码随想录八股文训练营总结
  • Ai推理助手易语言开源版
  • 【解决】联想电脑亮度调节
  • SAP学习笔记 - 开发45 - RAP开发 Managed App New Service Definition,Metadata Extension
  • c++图形题练习程序
  • 【工程篇】07:如何打包conda环境并拷贝到另一台服务器上
  • HAProxy双机热备,轻松实现负载均衡
  • 【开源】一款基于 .NET 和 Vue3 开源(Apache)的MES管理系统,您的新一代工厂管理助手!
  • 昆仑通态触摸屏脚本编程通讯设置和调试
  • 【亲测有效】ubuntu20.04服务器新建用户+vnc配置教程
  • Racknerd服务器Ubuntu
  • Ant Design预览多张图
  • AR眼镜颠覆医疗:精准手术零误差
  • HTML--教程
  • K-12教育创业新蓝图:告别“刷题”,拥抱“千人千面”个性化学习
  • 物联网设备管理工具实战:用AR运维镜击穿6.8天修复魔咒
  • Bootstrap-HTML(七)Bootstrap在线图标的引用方法
  • 【zynq7020】PS的“Hello World”