#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]))
theano tutorial(四) logistic regression 练习
最新推荐文章于 2020-10-19 16:42:03 发布