日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python-ML】神经网络-Theano张量库(GPU版的Numpy)

發布時間:2025/4/16 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python-ML】神经网络-Theano张量库(GPU版的Numpy) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# -*- coding: utf-8 -*- ''' Created on 2018年1月26日 @author: Jason.F @summary: pip install Theano, Theano,對于張量能夠高效地實現、編譯和評估數學表達式,支持在GPU上運行,利用GPU中巨大內存帶寬及浮點數運算能力 實現一個基于最小二乘法的線性回歸 ''' import theano from theano import tensor as T import numpy as np import matplotlib.pyplot as pltprint(theano.config.floatX)#查看浮點變量的設置 theano.config.floatX='float32' print(theano.config.device)#查看是在CPU還是GPU上 X_train =np.asarray([[0.0],[1.0],[2.0],[3.0],[4.0],[5.0],[6.0],[7.0],[8.0],[9.0]],dtype=theano.config.floatX) y_train =np.asarray([1.0,1.3,3.1,2.0,5.0,6.3,6.6,7.4,8.0,9.0],dtype=theano.config.floatX) def train_linreg(X_train,y_train,eta,epochs):costs=[]#initial arrayseta0=T.fscalar('eta0')y=T.fvector(name='y')X=T.fmatrix(name='X')w=theano.shared(np.zeros(shape=(X_train.shape[1]+1),dtype=theano.config.floatX),name='w')#calculate costnet_input = T.dot(X,w[1:])+w[0]errors=y-net_inputcost = T.sum(T.pow(errors,2))#perform gradient updategradient = T.grad(cost,wrt=w)update =[(w,w-eta0*gradient)]#compile modeltrain = theano.function(inputs=[eta0],outputs=cost,updates=update,givens={X:X_train,y:y_train})for _ in range(epochs):costs.append(train(eta))return costs,wcosts,w = train_linreg(X_train, y_train, eta=0.001, epochs=10) plt.plot(range(1,len(costs)+1),costs) plt.tight_layout() plt.xlabel('Epoch') plt.ylabel('Cost') plt.show() def predict_linreg(X,w):Xt=T.matrix(name='X')net_input = T.dot(Xt,w[1:])+w[0]predict = theano.function(inputs=[Xt],givens={w:w},outputs=net_input)return predict(X) plt.scatter(X_train,y_train,marker='s',s=50) plt.plot(range(X_train.shape[0]),predict_linreg(X_train, w),color='gray',marker='o',markersize=4,linewidth=3) plt.xlabel('x') plt.ylabel('y') plt.show()

結果:


總結

以上是生活随笔為你收集整理的【Python-ML】神经网络-Theano张量库(GPU版的Numpy)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。