Tensorflow深度学习应用(筑基篇)
筑基篇
#coding=gbk ''' 1.張量 用于描述數(shù)據(jù),可以理解為多維數(shù)組,包含張量的名字,階數(shù),形狀數(shù)值類型. Tensor("Add:0", shape=(m,n,k), dtype=float32),表示節(jié)點(diǎn)名為Add,0代表第0個(gè)輸出,shape表示為MxNxK維的數(shù)組,類型為float32。2.張量的類型 tf.float32,tf.float64,tf.int8,tf.int16,tf.int32,tf.int64,tf.uint8,tf.bool,tf.complex64,tf.complex128,默認(rèn)為int32(float32)3.常量與變量 constant ,無需初始化,運(yùn)行過程中不會(huì)改變 Variable,需要初始化,在運(yùn)行過程中有可能改變。 name=tf.Variable(value,name) init_op=name.initializer(),初始化變量name init_op=tf.global_variables_initializer(),初始化前面定義的所有變量。這個(gè)操作是需要運(yùn)行的。sess=tf.Session() init=tf.global_variables_initializer() ,變量的初始化 sess.run(init),需要運(yùn)行才能初始化4.變量的賦值 var=tf.Variable(0,name='var',trainable=False),trainable=False指定某個(gè)變量在模型訓(xùn)練中不參加訓(xùn)練,默認(rèn)為True. updata=tf.assign()variable_to_updated,new_value),人為更新變量的值5.占位符(placeholder) 對(duì)于定義時(shí)不知道其數(shù)值的變量,在程序運(yùn)行過程中才知道值,這時(shí)候就需要用到占位符。 tf.placegolder(dtype,shape=None,name=None)6.使用了占位符,在運(yùn)行時(shí)我們通過Feed提交數(shù)據(jù)和Fetch提取運(yùn)行數(shù)據(jù)7.tensorBoard 可視化 設(shè)置日志路徑logdir的值,清除之前的計(jì)算圖,寫入計(jì)算圖; 通過cmd窗口,輸入命令:tensorboard --logdir=日志路徑,然后通過該命令給出的鏈接,通過本機(jī)瀏覽器訪問,即localhost:端口號(hào)。'''#1.基本模型#導(dǎo)入tensorflow 模塊import tensorflow.compat.v1 as tf #創(chuàng)建一個(gè)常量,會(huì)作為一個(gè)節(jié)點(diǎn)加入計(jì)算圖中 Model = tf.constant("Hello World") #創(chuàng)建一個(gè)會(huì)話 sess = tf.Session() #運(yùn)行計(jì)算圖,得到結(jié)果 print(sess.run(Model)) #關(guān)閉會(huì)話,釋放資源 sess.close() #tensorflow是通過計(jì)算圖的形式表述計(jì)算的編程系統(tǒng),每個(gè)計(jì)算都是計(jì)算圖上的一個(gè)一個(gè)節(jié)點(diǎn),節(jié)點(diǎn)之間的邊描述了計(jì)算之間的關(guān)系。node1=tf.constant(3.0,tf.float32,name="node1") node2=tf.constant(3.0,tf.float32,name="node2") node3 = tf.add(node1, node2) sess = tf.Session() print(node3) print(sess.run(node3)) sess.close()#創(chuàng)建一個(gè)張量,獲取它的維度 scalar = tf.constant(50) vector = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9]) matrix = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])print(scalar.get_shape())#shape=() print(vector.get_shape())#shape=(9,) print(matrix.get_shape())#shape=(3, 3)#獲取張量的元素值,通過下標(biāo)直接訪問 sess = tf.Session() print(sess.run(matrix[1][2])) sess.close()#清除缺省計(jì)算圖 tf.reset_default_graph() a = tf.Variable(1, name="a") b = tf.add(a,2, name="b") c = tf.multiply(b, 3, name="c") d = tf.subtract(c, b, name="d")#可視化計(jì)算圖,并寫入日志 logdir = "E:/VSCODE/" if tf.gfile.Exists(logdir):tf.gfile.DeleteRecursively(logdir) writer =tf.summary.FileWriter(logdir, tf.get_default_graph()) writer.close()#為了保證系統(tǒng)資源的正常釋放,即使出現(xiàn)錯(cuò)誤,也會(huì)釋放掉系統(tǒng)資源,保證穩(wěn)定性 s=tf.constant([1,2,3,4,5,6,7,8,9]) sess=tf.Session() try:print(sess.run(s)) except:print(" Process Exception. ") #程序一點(diǎn)會(huì)執(zhí)行這句,而釋放資源 finally:sess.close()#或者通過with來自動(dòng)管理 s=tf.constant([1,2,3,4,5,6,7,8,9]) with tf.Session() as sess:print(sess.run(s))r=tf.constant([1,2,3,4,5,6,7,8,9]) sess=tf.Session() with sess.as_default():#手動(dòng)指定默認(rèn)的會(huì)話print(r.eval())#通過eval來計(jì)算張量的值#InteractiveSession將生成的會(huì)話自動(dòng)注冊(cè)為默認(rèn)會(huì)話 node1=tf.constant(3.0,tf.float32,name="node1") node2=tf.constant(3.0,tf.float32,name="node2") node3 = tf.add(node1, node2)sess=tf.InteractiveSession() print(node3.eval()) sess.close()#常量與變量相關(guān)操作 value = tf.Variable(0, name="value") one = tf.constant(1) new_value = tf.add(value, one) up_value = tf.assign(value, new_value) init = tf.global_variables_initializer()#打印,更新值 with tf.Session() as sess:sess.run(init) #變量初始化for _ in range(10):sess.run(up_value) #變量值更新print(sess.run(value)) #輸出變量值#為變量x占位,3x2矩陣,值為float32類型 x = tf.placeholder(tf.float32, [3, 2], name='X')#數(shù)據(jù)的提交和提取(Feed/Fetch) x1 = tf.placeholder(tf.float32, name='x1') x2 = tf.placeholder(tf.float32, name='x2') y = tf.multiply(x1, x2, name='y') z = tf.subtract(x1, x2, name='z')init = tf.global_variables_initializer()with tf.Session() as sess:sess.run(init)r1 = sess.run(y, feed_dict={x1: 9.1, x2: 1.9})r2 = sess.run([y, z], feed_dict={x1: [1.0, 2.0, 3.0], x2: [4.0, 5.0, 6.0]})'''y,z = sess.run([y, z], feed_dict={x1: [1.0, 2.0, 3.0], x2: [4.0, 5.0, 6.0]})'''print("y=",r1)print("y=",r2[0])print("z=",r2[0])
cmd命令輸入,注意切換到日志目錄下
代碼中設(shè)置的計(jì)算圖如下
遇到的錯(cuò)誤:
1.SyntaxError: Non-UTF-8 code starting with ‘\xd6’ in file e:\VS實(shí)驗(yàn)項(xiàng)目文件\Project\Python\main.py on line 4, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
主要是編碼的問題,vscode 默認(rèn)的是gbk編碼,設(shè)置#coding=gbk即可。
2.W0105 11:09:45.861135 9504 deprecation_wrapper.py:119] From e:\VS實(shí)驗(yàn)項(xiàng)目文件\Project\Python\main.py:77: The name tf.reset_default_graph is deprecated. Please use tf.compat.v1.reset_default_graph instead.
解決辦法:import tensorflow.compat.v1 as tf
3.TypeError: Fetch argument <tensorflow.python.client.session.Session object at 0x0000026C0A7A9A20> has invalid type <class ‘tensorflow.python.client.session.Session’>, must be a string or Tensor. (Can not convert a Session into a Tensor or Operation.),有問題的代碼如下:
Model = tf.constant(“Hello World”)
#創(chuàng)建一個(gè)會(huì)話
sess = tf.Session()
#運(yùn)行計(jì)算圖,得到結(jié)果
print(sess.run(sess))
#關(guān)閉會(huì)話,釋放資源
sess.close()
這主要是print(sess.run(sess))寫錯(cuò)了,只需要改為print(sess.run(Model )),注意運(yùn)行的是計(jì)算模型。還有一些拼寫錯(cuò)誤,要是不注意啊,真的頭疼。
附:本文章學(xué)習(xí)至中國(guó)大學(xué)mooc-深度學(xué)習(xí)應(yīng)用開發(fā)-Tensorflow實(shí)戰(zhàn),感謝吳老師。
總結(jié)
以上是生活随笔為你收集整理的Tensorflow深度学习应用(筑基篇)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【计算机网络复习 数据链路层】3.1 数
- 下一篇: linux的常用操作——查看和修改文件权