国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Python學習筆記之邏輯回歸

2019-11-14 17:15:34
字體:
來源:轉載
供稿:網友
 1 # -*- coding: utf-8 -*- 2 """ 3 Created on Wed APR 22 17:39:19 2015 4  5 @author: 90Zeng 6 """ 7  8 import numpy 9 import theano10 import theano.tensor as T11 import matplotlib.pyplot as plt12 rng = numpy.random13 N = 400 # 400個樣本14 feats = 784 # 每個樣本的維度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 # 隨機初始化權重23 w = theano.shared(rng.randn(feats), name="w")24 # 偏置初始化為 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 # 畫出損失函數圖62 x = range(1000)63 plt.plot(x,err[0:1000])

 損失函數隨著迭代次數變化,運行結果:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 横峰县| 济源市| 宽城| 龙里县| 舟曲县| 安塞县| 阜城县| 乡城县| 康马县| 惠安县| 盐边县| 淄博市| 凤凰县| 高阳县| 合作市| 同心县| 黄平县| 兖州市| 镇江市| 留坝县| 中西区| 自治县| 洛南县| 通渭县| 治多县| 吉首市| 兖州市| 教育| 罗平县| 龙口市| 山丹县| 钟祥市| 梅河口市| 蛟河市| 烟台市| 莆田市| 莱西市| 溧阳市| 新绛县| 台山市| 广汉市|