Python学习笔记之逻辑回归
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 22 17:39:19 2015 @author: 90Zeng
""" import numpy
import theano
import theano.tensor as T
import matplotlib.pyplot as plt
rng = numpy.random
N = 400 # 400个样本
feats = 784 # 每个样本的维度
D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
training_steps = 10000 # Declare Theano symbolic variables
x = T.dmatrix("x")
y = T.dvector("y") # 随机初始化权重
w = theano.shared(rng.randn(feats), name="w")
# 偏置初始化为 0
b = theano.shared(0.0, name="b")
print "Initial model:"
print w.get_value(), b.get_value() # Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probability that target = 1
prediction = p_1 > 0.5 # The prediction thresholded
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
lost_avg = xent.mean()
cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
gw, gb = T.grad(cost, [w, b]) # Compute the gradient of the cost
# (we shall return to this in a
# following section of this tutorial) # Compile
train = theano.function(
inputs=[x,y],
outputs=[prediction, lost_avg],
updates=((w, w - 0.1 * gw),(b, b - 0.1 * gb)),
)
predict=theano.function(
inputs=[x],
outputs=prediction,
) # Train
err = []
for i in range(training_steps):
pred, er = train(D[0], D[1])
err.append(er) print "Final model:"
print w.get_value(), b.get_value()
print "target values for D:", D[1]
print "prediction on D:", predict(D[0]) # 画出损失函数图
x = range(1000)
plt.plot(x,err[0:1000])
损失函数随着迭代次数变化,运行结果:

Python学习笔记之逻辑回归的更多相关文章
- tensorflow学习笔记五----------逻辑回归
在逻辑回归中使用mnist数据集.导入相应的包以及数据集. import numpy as np import tensorflow as tf import matplotlib.pyplot as ...
- Python学习笔记-StatsModels 统计回归(1)线性回归
1.背景知识 1.1 插值.拟合.回归和预测 插值.拟合.回归和预测,都是数学建模中经常提到的概念,而且经常会被混为一谈. 插值,是在离散数据的基础上补插连续函数,使得这条连续曲线通过全部给定的离散数 ...
- Python学习笔记-StatsModels 统计回归(3)模型数据的准备
1.读取数据文件 回归分析问题所用的数据都是保存在数据文件中的,首先就要从数据文件读取数据. 数据文件的格式很多,最常用的是 .csv,.xls 和 .txt 文件,以及 sql 数据库文件的读取 . ...
- Python 学习笔记 - 不断更新!
Python 学习笔记 太久不写python,已经忘记以前学习的时候遇到了那些坑坑洼洼的地方了,开个帖子来记录一下,以供日后查阅. 摘要:一些报错:为啥Python没有自增 ++ 和自减 --: 0x ...
- Deep learning with Python 学习笔记(11)
总结 机器学习(machine learning)是人工智能的一个特殊子领域,其目标是仅靠观察训练数据来自动开发程序[即模型(model)].将数据转换为程序的这个过程叫作学习(learning) 深 ...
- Deep learning with Python 学习笔记(8)
Keras 函数式编程 利用 Keras 函数式 API,你可以构建类图(graph-like)模型.在不同的输入之间共享某一层,并且还可以像使用 Python 函数一样使用 Keras 模型.Ker ...
- Deep learning with Python 学习笔记(7)
介绍一维卷积神经网络 卷积神经网络能够进行卷积运算,从局部输入图块中提取特征,并能够将表示模块化,同时可以高效地利用数据.这些性质让卷积神经网络在计算机视觉领域表现优异,同样也让它对序列处理特别有效. ...
- Deep learning with Python 学习笔记(1)
深度学习基础 Python 的 Keras 库来学习手写数字分类,将手写数字的灰度图像(28 像素 ×28 像素)划分到 10 个类别 中(0~9) 神经网络的核心组件是层(layer),它是一种数据 ...
- 机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归
机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归 关键字:Logistic回归.python.源码解析.测试作者:米仓山下时间:2018- ...
随机推荐
- Linux第二章读书笔记
1.获取内核源码 1.1Git 分布式的:下载和管理Linux内核源代码: - 获取最新提交到版本树的一个副本 $ git clone git://git.kernel.org/pub/scm/lin ...
- 安全相关论文--Security and Dependability
安全相关论文--Security and Dependability 所参考的文献来自于Kreutz D, Ramos F M V, Esteves Verissimo P, et al. Softw ...
- PHP 爬虫——QueryList
前言: 来了个任务说要做个电影网站,要写个壳,数据直接从别人那扒.行吧!那就要学习下PHP爬虫了.占个博客,以后补充.http://study.querylist.cc/archives/6/ 之前开 ...
- 将ssh失败的用户放入hosts.deny中
1.find / -name secure 找到linux系统安全日志文件 2.cp `find / -name secure` /tmp/`date +%F` 将secure文件复制出来 或者使用f ...
- final 140字评论II
1.约跑app: 从性能上讲,着重修改了其他组找出的bug,性能上有了很大的提高,增强了实用性. 从功能上讲,该app可以增加用户之间的互动性,有较多的客户群,适合人群不限于青少年和成年人. 从UI上 ...
- thinkphp5 速查表
本速查表里的类都是think为命名空间的,实例化时省去了 use.用的时候注意. 本速查表里会有四种方法的调用: 公有方法 $class = new Class(); $class->foo ...
- CentOS修改主机名字
目录 查看hostnmae 修改hostname 远程别名/etc/hosts 查看hostnmae [root@centos ~]$ hostname centos 修改hostname [root ...
- 【题解】 POJ 1201 Intervals(差分约束)
懒得复制,戳我戳我 Solution: 这道题就是一个板子题 抽象成第\(a\)至第\(b\)间选择数的个数为\(c\),我们就可以用前缀和来表示,这样就可以得到不等式\(s[b]-s[a-1]> ...
- 【转】 CRC循环冗余校验码
1.CRC CRC循环冗余校验码是数据通信中的一种查错校验码. 循环冗余检查对数据进行多项式计算,将计算结果附加在帧后面,接收数据的设备执行模2运算,保证数据传输的正确性和完整性. 2.模2除法 ①不 ...
- SpringBoot中的定时任务与Quartz的整合
SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...