1. 程式人生 > >使用Tensorflow實現CNN使用的函式小結

使用Tensorflow實現CNN使用的函式小結

給圖片新增維度np.expand_dims()

Image = np.expand_dims(np.expand_dims(img, 0), -1)

expand_dims(a, axis) Expand the shape of an array. 無論a是多少維的陣列,主要是看axis的取值。 例子參考下面的部落格。

卷積 tf.nn.conv2d

方法定義 tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)

引數: input : 輸入的要做卷積的圖片,要求為一個張量,shape為 [ batch, in_height, in_weight, in_channel ],其中batch為圖片的數量,in_height 為圖片高度,in_weight 為圖片寬度,in_channel 為圖片的通道數,灰度圖該值為1,彩色圖為3。(也可以用其它值,但是具體含義不是很理解) filter: 卷積核,要求也是一個張量,shape為 [ filter_height, filter_weight, in_channel, out_channels ],其中 filter_height 為卷積核高度,filter_weight 為卷積核寬度,in_channel 是影象通道數 ,和 input 的 in_channel 要保持一致,out_channel 是卷積核數量。 strides: 卷積時在影象每一維的步長,這是一個一維的向量,[ 1, strides, strides, 1],第一位和最後一位固定必須是1 padding: string型別,值為“SAME” 和 “VALID”,表示的是卷積的形式,是否考慮邊界。”SAME”是考慮邊界,不足的時候用0去填充周圍,”VALID”則不考慮 use_cudnn_on_gpu: bool型別,是否使用cudnn加速,預設為true

import tensorflow as tf
# case 1
# 輸入是1張 3*3 大小的圖片,影象通道數是5,卷積核是 1*1 大小,數量是1
# 步長是[1,1,1,1]最後得到一個 3*3 的feature map
# 1張圖最後輸出就是一個 shape為[1,3,3,1] 的張量
input = tf.Variable(tf.random_normal([1,3,3,5]))
filter = tf.Variable(tf.random_normal([1,1,5,1]))
op1 = tf.nn.conv2d(input, filter, strides=[1,1,1,1], padding='SAME')
init = tf.initialize_all_variables() 
with tf.Session() as sess:
	sess.run(init)
	print('*' * 20 + ' op1 ' + '*' * 20)
	print(sess.run(op1))

啟用函式 tf.nn.relu

使用起來很簡單,只需要將是上層得到的輸入傳進去就行。 關於Relu,可以閱讀下面的部落格。

陣列的降維np.reshape()

在畫圖的時候使用到了一個np.reshape(input, (1, -1)); 假設input矩陣的shape為(A,B),使用reshape(input,(C,D))得到的新陣列元素數量與原陣列元素數量要相等。即AB=CD.當一個引數為-1時,reshape函式會根據另一個引數的維度計算出陣列的另外一個shape屬性值。例子參考下面的部落格。

池化 tf.nn.max_pool

定義: tf.nn.max_pool(value, ksize, strides, padding, name=None) 引數是四個,和卷積很類似: 第一個引數value:需要池化的輸入,一般池化層接在卷積層後面,所以輸入通常是feature map,依然是[batch, height, width, channels]這樣的shape 第二個引數ksize:池化視窗的大小,取一個四維向量,一般是[1, height, width, 1],因為我們不想在batch和channels上做池化,所以這兩個維度設為了1 第三個引數strides:和卷積類似,視窗在每一個維度上滑動的步長,一般也是[1, stride,stride, 1] 第四個引數padding:和卷積類似,可以取’VALID’ 或者’SAME’ 返回一個Tensor,型別不變,shape仍然是[batch, height, width, channels]這種形式。 例子參看下面的部落格。

tf.nn.dropout

定義 tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None,name=None) 第一個引數x:指輸入 第二個引數keep_prob: 設定神經元被選中的概率,在初始化時keep_prob是一個佔位符, keep_prob = tf.placeholder(tf.float32)。tensorflow在run時設定keep_prob具體的值,例如keep_prob: 0.5。 第五個引數name:指定該操作的名字。 在全連線層中使用。 示例看下面的部落格。

tf.nn.softmat

一般用在網路最後一層的輸出。 最後一層的輸出是由上層輸出乘以權重加上偏置得到的,不需要記過啟用函式。 ft = tf.matmul(last_output,weights)+bias y_cnn = tf.nn.softmax(ft)

交叉熵的計算: cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_CNN), reduction_indices=[1])) 使用優化演算法優化: train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

tf.reduce_sum 和 tf.reduce_mean

求平均值tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None) x = tf.constant([[1., 1.], [2., 2.]]) tf.reduce_mean(x) # 1.5 tf.reduce_mean(x, 0) # [1.5, 1.5] tf.reduce_mean(x, 1) # [1., 2.]

按照某個維度求和reduce_sum(input_tensor,axis=None,keep_dims=False, name=None) # ‘x’ is [[1, 1, 1] # [1, 1, 1]] tf.reduce_sum(x) ==> 6 tf.reduce_sum(x, 0) ==> [2, 2, 2] tf.reduce_sum(x, 1) ==> [3, 3] tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]] tf.reduce_sum(x, [0, 1]) ==> 6

順便把tf.reduce_max也牽出來 求最大值tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)

tf.argmax

tf.argmax( input, axis=None, name=None, dimension=None, output_type=tf.int64) Returns the index with the largest value across axes of a tensor. (deprecated arguments)

tf.equal

tf.equal( x, y, name=None) Returns the truth value of (x == y) element-wise.

tf.cast

tf.cast(x, dtype, name=None) Args: x: A Tensor or SparseTensor of numeric type. It could be uint8, int8, uint16, int16, int32, int64, float16, float32, float64, complex64, complex128, bfloat16. dtype: The destination type. The list of supported dtypes is the same as x. name: A name for the operation (optional). Returns: A Tensor or SparseTensor with same shape as x and same type as dtype.

x = tf.constant([1.8, 2.2], dtype=tf.float32)
tf.cast(x, tf.int32)  # [1, 2], dtype=tf.int32