# Python学习（十）——逻辑回归（Logistic Regression）

2015/10/12 09:55

1.简介

2.python代码

``````#!/usr/bin/python

import numpy
import theano
import theano.tensor as T
rng=numpy.random

N=400
feats=784
# D[0]:generate rand numbers of size N,element between (0,1)
# D[1]:generate rand int number of size N,0 or 1
D=(rng.randn(N,feats),rng.randint(size=N,low=0,high=2))
training_steps=10000

# declare symbolic variables
x=T.matrix('x')
y=T.vector('y')
w=theano.shared(rng.randn(feats),name='w')  # w is shared for every input
b=theano.shared(0.,name='b') # b is shared too.

print('Initial model:')
print(w.get_value())
print(b.get_value())

# construct theano expressions,symbolic
p_1=1/(1+T.exp(-T.dot(x,w)-b))  # sigmoid function,probability of target being 1
prediction=p_1>0.5
xent=-y*T.log(p_1)-(1-y)*T.log(1-p_1)  # cross entropy
cost=xent.mean()+0.01*(w**2).sum() # cost function to update parameters

#compile
predict=theano.function(inputs=[x],outputs=prediction)

# train
for i in range(training_steps):
pred,err=train(D[0],D[1])

print('Final model:')
print(w.get_value())
print(b.get_value())
print('target values for D:')
print(D[1])
print('prediction on D:')
print(predict(D[0]))

print('newly generated data for test:')
test_input=rng.randn(30,feats)
print('result:')
print(predict(test_input))``````

3.程序解读

0
5 收藏

0 评论
5 收藏
0