tensorflow 入門學習(1)

tf的基本函數介紹點擊打開鏈接

下面的樣例取自tensorflow中文社區

有自己實驗的註釋

樣例1

import tensorflow as tf

matrix1 = tf.constant([[3., 3.]])

matrix2 = tf.constant([[2.],[2.]])

product = tf.matmul(matrix1, matrix2)


sess = tf.Session()

result = sess.run(product)
print result

sess.close()

上面申明瞭兩個矩陣op,然後定義了一個矩陣乘法,都是tf寫好的函數,然後用一個回話運行即可

run裏面的參數即是要返回的結果,可以用[ans1,ans2..]查看多個結果,包括中間變量


樣例2

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print sess.run(c)
同樣申明兩個矩陣,但是用了shape可以講數組轉化成矩陣,這個就是定義維數,如果是-1就是系統自己計算當前維數


樣例3

def weight_variable(shape):
	initial = tf.truncated_normal(shape, stddev=0.1)
	return tf.Variable(initial)

def bias_variable(shape):
	initial = tf.constant(0.1, shape=shape)
	return tf.Variable(initial)

W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
  sess.run(init_op) #important

  print sess.run([b_conv1,W_conv1])


第一個函數加了一個標準差爲0.1的噪聲,類型是32float,在0附近

第二個就初始爲1*32的全部0.1的矩陣
注意有變量所以必須先調用初始化函數即代碼中的important部分



發佈了93 篇原創文章 · 獲贊 45 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章