1#-*- coding: utf-8 -*-2"""3Created on Wed APR22 17:39:19 201545@author: 90Zeng6"""78importnumpy9importtheano10importtheano.tensor as T11importmatplotlib.pyplot as plt12rng =numpy.random13N = 400#400个样本14feats = 784#每个样本的维度15D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))16training_steps = 100001718#Declare Theano symbolic variables19x = T.dmatrix("x")20y = T.dvector("y")2122#随机初始化权重23w = theano.shared(rng.randn(feats), name="w")24#偏置初始化为 025b = theano.shared(0.0, name="b")26print"Initial model:"27printw.get_value(), b.get_value()2829#Construct Theano expression graph30p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b))#Probability that target = 131prediction = p_1 > 0.5#The prediction thresholded32xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1)#Cross-entropy loss function33lost_avg =xent.mean()34cost = xent.mean() + 0.01 * (w ** 2).sum()#The cost to minimize35gw, gb = T.grad(cost, [w, b])#Compute the gradient of the cost36#(we shall return to this in a37#following section of this tutorial)3839#Compile40train =theano.function(41inputs=[x,y],42outputs=[prediction, lost_avg],43updates=((w, w - 0.1 * gw),(b, b - 0.1 *gb)),44)45predict=theano.function(46inputs=[x],47outputs=prediction,48)4950#Train51err =[]52foriinrange(training_steps):53pred, er = train(D[0], D[1])54err.append(er)5556print"Final model:"57printw.get_value(), b.get_value()58print"target values for D:", D[1]59print"prediction on D:", predict(D[0])6061#画出损失函数图62x = range(1000)63plt.plot(x,err[0:1000])
损失函数随着迭代次数变化,运行结果: