1.导入包
2.
1 #正确率方法定义 2 def compute_accuracy(y_target, y_predict): 3 correct_prediction = np.equal(y_predict, y_target) 4 accuracy = np.sum(correct_prediction)/len(correct_prediction) 5 return accuracy
3.数据
1 #可看成rng是np.random的简写 2 rng = np.random 3 4 5 N = 400 # training sample size数据的example 6 feats = 784 # number of input variables数据点 7 8 # generate a dataset: D = (input_values, target_class) #输入数据、类别 9 D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2)) #生成虚拟数据 类别:0,1
4.
1 # Declare Theano symbolic variables 2 3 x = T.dmatrix("x") 4 y = T.dvector("y")
5.权重偏置
1 # initialize the weights and biases 2 3 W = theano.shared(rng.randn(feats), name="w") #Weights 4 b = theano.shared(0.1, name="b") #biases
6.loss
1 #分类层 2 p_1 = T.nnet.sigmoid(T.dot(x, W) + b) # Logistic Probability that target = 1 (activation function) 3 prediction = p_1 > 0.5 # The prediction thresholded >0.5被认为是1 4 5 #计算loss 6 xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function 7 # or 8 # xent = T.nnet.binary_crossentropy(p_1, y) # this is provided by theano 9 10 cost = xent.mean() + 0.01 * (W ** 2).sum() # The cost to minimize (l2 regularization) 11 #L1loss:0.01 * (W ** 2).sum()为了避免overfitting(过拟合) 12 13 #求梯度 14 gW, gb = T.grad(cost, [W, b]) # Compute the gradient of the cost
7.compile
1 learning_rate = 0.1 #学习率 2 train = theano.function( 3 inputs=[x, y], 4 outputs=[prediction, xent.mean()], #xent.mean()是平均损失 5 updates=((W, W - learning_rate * gW), (b, b - learning_rate * gb))) #更新权重偏置 6 7 predict = theano.function(inputs=[x], outputs=prediction) #预测
8.training
1 for i in range(500): 2 pred, err = train(D[0], D[1]) #D[0]数据 D[1]类别 train返回的一个是predict一个是loss 3 if i % 50 == 0: 4 print('cost:', err) 5 print("accuracy:", compute_accuracy(D[1], predict(D[0]))) #计算准确率 6 7 print("target values for D:") 8 print(D[1]) 9 print("prediction on D:") 10 print(predict(D[0]))
浙公网安备 33010602011771号