theano tutorial(四) logistic regression 练习

#coding=utf-8

import numpy
import theano
import theano.tensor as T
rng=numpy.random
N=400#400类
feats=784#784个特征

#创建一个数据集D=(inputs_values,target_class)
#randn:Return a sample (or samples) from the "standard normal" distribution
#randint:Return random integers from low (inclusive) to high (exclusive).
D=(rng.randn(N,feats),rng.randint(size=N,low=0,high=2))
max_iter=100
x=T.matrix("x")#100,784
y=T.vector("y")
LAMBDA=0.01#衰减项的
THETA=0.1#学习率

#s=x*w+b
w=theano.shared(rng.randn(feats),"w")# 784 1
b=theano.shared(0.,"b")#1,1 broadcast

# print(w.get_value())
# print(b.get_value())

p=1/(1+T.exp(-T.dot(x,w)+b))#100 1

cost=-(y*T.log(p)+(1-y)*T.log(1-p))
cost=cost.mean()+LAMBDA*(w**2).sum()
gw,gb=T.grad(cost,[w,b])
prediction=p>0.5

train=theano.function(inputs=[x,y],outputs=[prediction,cost],updates=[(w,w-THETA*gw),(b,b-THETA*gb)])
res=theano.function(inputs=[x],outputs=prediction)

for i in range(max_iter):
    pred,error=train(D[0],D[1])

print(w.get_value())
print(b.get_value())
print("target values for D:")
print(D[1])
print("prediction on D:")
print(res(D[0]))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值