# -*- 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])
损失函数随着迭代次数变化,运行结果: