设为首页 收藏本站
查看: 1248|回复: 0

[经验分享] Python学习笔记之逻辑回归

[复制链接]

尚未签到

发表于 2015-11-29 16:11:25 | 显示全部楼层 |阅读模式
1 # -*- coding: utf-8 -*-
2 """
3 Created on Wed Apr 22 17:39:19 2015
4
5 @author: 90Zeng
6 """
7
8 import numpy
9 import theano
10 import theano.tensor as T
11 import matplotlib.pyplot as plt
12 rng = numpy.random
13 N = 400 # 400个样本
14 feats = 784 # 每个样本的维度
15 D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
16 training_steps = 10000
17
18 # Declare Theano symbolic variables
19 x = T.dmatrix("x")
20 y = T.dvector("y")
21
22 # 随机初始化权重
23 w = theano.shared(rng.randn(feats), name="w")
24 # 偏置初始化为 0
25 b = theano.shared(0.0, name="b")
26 print "Initial model:"
27 print w.get_value(), b.get_value()
28
29 # Construct Theano expression graph
30 p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b))   # Probability that target = 1
31 prediction = p_1 > 0.5                    # The prediction thresholded
32 xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
33 lost_avg = xent.mean()
34 cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
35 gw, gb = T.grad(cost, [w, b])             # Compute the gradient of the cost
36                                           # (we shall return to this in a
37                                           # following section of this tutorial)
38
39 # Compile
40 train = theano.function(
41     inputs=[x,y],
42     outputs=[prediction, lost_avg],
43     updates=((w, w - 0.1 * gw),(b, b - 0.1 * gb)),
44     )
45 predict=theano.function(
46     inputs=[x],
47     outputs=prediction,
48     )
49
50 # Train
51 err = []
52 for i in range(training_steps):
53     pred, er = train(D[0], D[1])
54     err.append(er)
55
56 print "Final model:"
57 print w.get_value(), b.get_value()
58 print "target values for D:", D[1]
59 print "prediction on D:", predict(D[0])
60
61 # 画出损失函数图
62 x = range(1000)
63 plt.plot(x,err[0:1000])
  损失函数随着迭代次数变化,运行结果:
DSC0000.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-145044-1-1.html 上篇帖子: python ctypes 探究 下篇帖子: Python中写一个乒乓球类的游戏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表