700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【Python-ML】神经网络-Theano张量库(GPU版的Numpy)

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

时间:2020-03-29 02:37:42

相关推荐

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

# -*- coding: utf-8 -*-'''Created on 1月26日@author: Jason.F@summary: pip install Theano,Theano,对于张量能够高效地实现、编译和评估数学表达式,支持在GPU上运行,利用GPU中巨大内存带宽及浮点数运算能力实现一个基于最小二乘法的线性回归'''import theanofrom theano import tensor as Timport numpy as npimport 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()

结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。