# -*- 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学习笔记之逻辑回归的更多相关文章

  1. tensorflow学习笔记五----------逻辑回归

    在逻辑回归中使用mnist数据集.导入相应的包以及数据集. import numpy as np import tensorflow as tf import matplotlib.pyplot as ...

  2. Python学习笔记-StatsModels 统计回归(1)线性回归

    1.背景知识 1.1 插值.拟合.回归和预测 插值.拟合.回归和预测,都是数学建模中经常提到的概念,而且经常会被混为一谈. 插值,是在离散数据的基础上补插连续函数,使得这条连续曲线通过全部给定的离散数 ...

  3. Python学习笔记-StatsModels 统计回归(3)模型数据的准备

    1.读取数据文件 回归分析问题所用的数据都是保存在数据文件中的,首先就要从数据文件读取数据. 数据文件的格式很多,最常用的是 .csv,.xls 和 .txt 文件,以及 sql 数据库文件的读取 . ...

  4. Python 学习笔记 - 不断更新!

    Python 学习笔记 太久不写python,已经忘记以前学习的时候遇到了那些坑坑洼洼的地方了,开个帖子来记录一下,以供日后查阅. 摘要:一些报错:为啥Python没有自增 ++ 和自减 --: 0x ...

  5. Deep learning with Python 学习笔记(11)

    总结 机器学习(machine learning)是人工智能的一个特殊子领域,其目标是仅靠观察训练数据来自动开发程序[即模型(model)].将数据转换为程序的这个过程叫作学习(learning) 深 ...

  6. Deep learning with Python 学习笔记(8)

    Keras 函数式编程 利用 Keras 函数式 API,你可以构建类图(graph-like)模型.在不同的输入之间共享某一层,并且还可以像使用 Python 函数一样使用 Keras 模型.Ker ...

  7. Deep learning with Python 学习笔记(7)

    介绍一维卷积神经网络 卷积神经网络能够进行卷积运算,从局部输入图块中提取特征,并能够将表示模块化,同时可以高效地利用数据.这些性质让卷积神经网络在计算机视觉领域表现优异,同样也让它对序列处理特别有效. ...

  8. Deep learning with Python 学习笔记(1)

    深度学习基础 Python 的 Keras 库来学习手写数字分类,将手写数字的灰度图像(28 像素 ×28 像素)划分到 10 个类别 中(0~9) 神经网络的核心组件是层(layer),它是一种数据 ...

  9. 机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归

    机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归 关键字:Logistic回归.python.源码解析.测试作者:米仓山下时间:2018- ...

随机推荐

  1. 第二阶段冲刺——five

    个人任务: 王金萱:合并程序(统计团队博客). 季方:合并并排除运行团队博客时出现的错误. 马佳慧:优化登录.注册信息的填写判断. 司宇航:完成打印名单的功能. 站立会议: 任务看板和燃尽图:

  2. ffmpeg格式转换

    遇到有些wav文件在ubuntu下无法打开的情况,可以使用ffmpeg进行格式转换即可 ffmpeg -i 0.wav test.wav

  3. Cloudstack 的搭建

    Note: 关闭了NFS Storage 的防火墙 service iptables stop 1. 新创建的Linux没有获取IP; vi /etc/sysconfig/network-script ...

  4. composer 使用(踩坑笔记)

    composer的介绍 来张图就好啦!我不细说啦 安装composer 1).PHP 环境变量配置,可参考百度经验: 2).详情可参考中国全量镜像进行 Composer 的安装 3).安装完成后,命令 ...

  5. Jira 7.2.4简单安装过程

    1. 下载安装jira的安装文件 这里使用同事已经下载好的文件. 2. 下载破解文件. 也是同事下载好的 具体文件为 atlassian-jira-software--x64 51CTO下载-jira ...

  6. Pandas笔记目录

    速查笔记 使用实例 Pandas-数据导入 (未完成) Pandas-数据探索 基础属性 shape indexs columns values dtype/dtypes 汇总和计算描述统计 coun ...

  7. AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到

    AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到

  8. Java_按位与&,按位或,取反,左移,右移运算符

    //按位与运算& System.out.println(0&0);//0 System.out.println(0&1);//0 System.out.println(1&am ...

  9. RSA modulus too small: 512 < minimum 768 bits

    RSA modulus too small: 512 < minimum 768 bits $ ssh admin@192.168.50.46 ssh_rsa_verify: RSA modul ...

  10. 动态生成web表-asp.net table

    1. 页面上定义一个server 的table <table style="width: 100%" id="tbContent" runat=" ...