matlab gpu deep learning_在Matlab中使用tensorflow (1)
生活随笔
收集整理的這篇文章主要介紹了
matlab gpu deep learning_在Matlab中使用tensorflow (1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為了在matlab中利用豐富的python開源資源,探索了如下內容:
1)在matlab中直接調用tensorflow函數;
2)在matlab中調用tensorflow的python程序,結合matlab方便的數據處理功能;
本篇對內容1)進行描述,工作環境為win10 64位系統,python 3.6,tensorflow 1.1,matlab 2018b。為了實現在matlab中直接調用tensorflow(對其他python module方法是通用的),需要進行如下工作:
1)在win10中安裝python,不會可百度;
2)安裝pip,用pip安裝tensorflow、numpy等python包,不會可baidu;
3)在matlab中確認python是否安裝成功,不會可在matlab幫助文檔中查找python相關內容,不多很好找;
4)運行如下代碼
clc % import libs tf = py.importlib.import_module('tensorflow'); np = py.importlib.import_module('numpy'); % set problem a = tf.Variable(np.float32(0.001), pyargs('dtype',tf.float32)); k1 = tf.placeholder(tf.float32,pyargs('shape',py.None,'name','k1')); cost = a^2 + k1*a + 5; % set optimizer optimizer = tf.train.RMSPropOptimizer(pyargs('learning_rate',0.1)).minimize(cost); init = tf.global_variables_initializer(); % solve trysess = tf.Session();sess.run(init);dict = py.dict(pyargs(k1.name,np.array([4])));idxr = [];costr = [];for i = 1:20sess.run(optimizer,pyargs('feed_dict',dict));a_val = sess.run(a,pyargs('feed_dict',dict));cost_val = sess.run(cost,pyargs('feed_dict',dict));% 顯示信息msgstr = ['迭代%d次:變量a=%f,cost=%fn'];fprintf(msgstr,i,a_val,cost_val);idxr = [idxr i];costr = [costr double(cost_val)];endsess.close() catchsess.close() end % plot plot(idxr,costr)5)運行結束后顯示結果,值得注意的是,matlab中運行效率tensorflow的效率比在spyder中高
迭代1次:變量a=-0.252028,cost=4.055407 迭代2次:變量a=-0.439602,cost=3.434840 迭代3次:變量a=-0.593724,cost=2.977612 迭代4次:變量a=-0.726587,cost=2.621581 迭代5次:變量a=-0.844292,cost=2.335660 迭代6次:變量a=-0.950372,cost=2.101719 迭代7次:變量a=-1.047062,cost=1.908090 迭代8次:變量a=-1.135870,cost=1.746720 迭代9次:變量a=-1.217856,cost=1.611750 迭代10次:變量a=-1.293788,cost=1.498735 迭代11次:變量a=-1.364241,cost=1.404189 迭代12次:變量a=-1.429650,cost=1.325299 迭代13次:變量a=-1.490354,cost=1.259739 迭代14次:變量a=-1.546619,cost=1.205554 迭代15次:變量a=-1.598660,cost=1.161074 迭代16次:變量a=-1.646657,cost=1.124851 迭代17次:變量a=-1.690764,cost=1.095627 迭代18次:變量a=-1.731121,cost=1.072296 迭代19次:變量a=-1.767859,cost=1.053890 迭代20次:變量a=-1.801107,cost=1.039558總結
以上是生活随笔為你收集整理的matlab gpu deep learning_在Matlab中使用tensorflow (1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中的pass语句_Pytho
- 下一篇: junit 引入spring 注解管理_