# Example 3- A Real Example Logistic Regression with theano

## code example

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

N = 400                                   # training sample size
feats = 784                               # number of input variables

# generate a dataset: D = (input_values, target_class)
D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
# (400, 784) (400,)
training_steps = 10000

# Declare Theano symbolic variables
x = T.dmatrix("x") # (400,784)
y = T.dvector("y") # (400,)

# initialize the weight vector w randomly
#
# this and the following bias variable b
# are shared so they keep their values
w = theano.shared(rng.randn(feats), name="w") #vector (784,)

# initialize the bias term
b = theano.shared(0., name="b") #scalar ()

print("Initial model:")
print(w.get_value().shape)
print(b.get_value().shape)

# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b))   # Probability that target = 1        = vector(400,)
prediction = p_1 > 0.5                    # The prediction thresholded         = vector(400,) of bool
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function    = vector(400,) of Cx for given x
cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize               = scalar   f(x,y,w,b)
gw, gb = T.grad(cost, [w, b])             # Compute the gradient of the cost
# w.r.t weight vector w and
# bias term b
# following section of this tutorial)

updates = [(w, w - 0.1 * gw),(b, b - 0.1 * gb)]
# Compile
train = theano.function(
inputs=[x,y],
outputs=[prediction, xent,cost],
)

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

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

prediction = predict(D[0])  # vector(400,) of bool

#a = np.array([1,2,3,4])
#b = np.array([2,2,4,3])
#np.mean( np.equal(a,b) ) #[0,1,0,0] 0.25

accuracy = 100* np.mean( np.equal(prediction,D[1]) )

print("Final model:")
print(w.get_value().shape)
print(b.get_value().shape)

print "\nCost = ",cost_t
print "Accuracy = ", accuracy,"%"

print("\ntarget values for D:")
#print(D[1])
print("prediction on D:")
#print(prediction)
``````
``````Initial model:
(784,)
()
Final model:
(784,)
()

Cost =  0.121553645129
Accuracy =  100.0 %

target values for D:
prediction on D:
``````

## History

• 20180807: created.

Author: kezunlin
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source kezunlin !

TOC