Python learning notes logistic regression and python learning notes Regression

Source: Internet
Author: User
Tags theano

Python learning notes logistic regression and python learning notes Regression

1 #-*-coding: UTF-8-*-2 "3 Created on Wed Apr 22 17:39:19 2015 4 5 @ author: 90 Zeng 6 "7 8 import numpy 9 import theano10 import theano. tensor as T11 import matplotlib. pyplot as plt12 rng = numpy. random13 N = 400 #400 samples 14 feats = 784 # dimension of each sample 15 D = (rng. randn (N, feats), rng. randint (size = N, low = 0, high = 2) 16 training_steps = 1000017 18 # Declare Theano symbolic variables19 x = T. dmatrix ("x") 20 y = T. dvector ("y") 21 22 # random initialization weight 23 w = theano. shared (rng. randn (feats), name = "w") 24 # The offset is initialized to 025 B = theano. shared (0.0, name = "B") 26 print "Initial model:" 27 print w. get_value (), B. get_value () 28 29 # Construct Theano expression graph30 p_1 = 1/(1 + T. exp (-T. dot (x, w)-B) # Probability that target = 131 prediction = p_1> 0.5 # The prediction thresholded32 xent =-y * T. log (p_1)-(1-y) * T. log (1-p_1) # Cross-entropy loss function33 lost_avg = xent. mean () 34 cost = xent. mean () + 0.01 * (w ** 2 ). sum () # The cost to minimize35 gw, gb = T. grad (cost, [w, B]) # Compute the gradient of the cost36 # (we shall return to this in a37 # following section of this tutorial) 38 39 # Compile40 train = theano. function (41 inputs = [x, y], 42 outputs = [prediction, lost_avg], 43 updates = (w, w-0.1 * gw), (B, b-0.1 * gb), 44) 45 predict = theano. function (46 inputs = [x], 47 outputs = prediction, 48) 49 50 # Train51 err = [] 52 for I in range (training_steps): 53 pred, er = train (D [0], D [1]) 54 err. append (er) 55 56 print "Final model:" 57 print w. get_value (), B. get_value () 58 print "target values for D:", D [1] 59 print "prediction on D:", predict (D [0]) 60 61 # Draw the loss function fig 62 x = range (1000) 63 plt. plot (x, err [0: 1000])

The loss function varies with the number of iterations. Running result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.