python深度學習tensorflow1.0參數和特征提取

tf.trainable_variables()提取訓練參數

在tf中,參與訓練的參數可用 tf.trainable_variables()提取出來,如:

#取出所有參與訓練的參數
params=tf.trainable_variables()
print("Trainable variables:------------------------")
#循環列出參數
for idx, v in enumerate(params):
     print("  param {:3}: {:15}   {}".format(idx, str(v.get_shape()), v.name))

這裡隻能查看參數的shape和name,並沒有具體的值。如果要查看參數具體的值的話,必須先初始化,即:

sess=tf.Session()
sess.run(tf.global_variables_initializer())

同理,我們也可以提取圖片經過訓練後的值。圖片經過卷積後變成瞭特征,要提取這些特征,必須先把圖片feed進去。

具體實例

# -*- coding: utf-8 -*-
"""
Created on Sat Jun  3 12:07:59 2017
@author: Administrator
"""
import tensorflow as tf
from skimage import io,transform
import numpy as np
#-----------------構建網絡----------------------
#占位符
x=tf.placeholder(tf.float32,shape=[None,100,100,3],name='x')
y_=tf.placeholder(tf.int32,shape=[None,],name='y_')
#第一個卷積層(100——>50)
conv1=tf.layers.conv2d(
      inputs=x,
      filters=32,
      kernel_size=[5, 5],
      padding="same",
      activation=tf.nn.relu,
      kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool1=tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
#第二個卷積層(50->25)
conv2=tf.layers.conv2d(
      inputs=pool1,
      filters=64,
      kernel_size=[5, 5],
      padding="same",
      activation=tf.nn.relu,
      kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool2=tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
#第三個卷積層(25->12)
conv3=tf.layers.conv2d(
      inputs=pool2,
      filters=128,
      kernel_size=[3, 3],
      padding="same",
      activation=tf.nn.relu,
      kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool3=tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=2)
#第四個卷積層(12->6)
conv4=tf.layers.conv2d(
      inputs=pool3,
      filters=128,
      kernel_size=[3, 3],
      padding="same",
      activation=tf.nn.relu,
      kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool4=tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=2)
re1 = tf.reshape(pool4, [-1, 6 * 6 * 128])
#全連接層
dense1 = tf.layers.dense(inputs=re1, 
                      units=1024, 
                      activation=tf.nn.relu,
                      kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
                      kernel_regularizer=tf.nn.l2_loss)
dense2= tf.layers.dense(inputs=dense1, 
                      units=512, 
                      activation=tf.nn.relu,
                      kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
                      kernel_regularizer=tf.nn.l2_loss)
logits= tf.layers.dense(inputs=dense2, 
                        units=5, 
                        activation=None,
                        kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
                        kernel_regularizer=tf.nn.l2_loss)
#---------------------------網絡結束---------------------------
#%%
#取出所有參與訓練的參數
params=tf.trainable_variables()
print("Trainable variables:------------------------")
#循環列出參數
for idx, v in enumerate(params):
     print("  param {:3}: {:15}   {}".format(idx, str(v.get_shape()), v.name))
#%%
#讀取圖片
img=io.imread('d:/cat.jpg')
#resize成100*100
img=transform.resize(img,(100,100))
#三維變四維(100,100,3)-->(1,100,100,3)
img=img[np.newaxis,:,:,:]
img=np.asarray(img,np.float32)
sess=tf.Session()
sess.run(tf.global_variables_initializer()) 
#提取最後一個全連接層的參數 W和b
W=sess.run(params[26])
b=sess.run(params[27])
#提取第二個全連接層的輸出值作為特征    
fea=sess.run(dense2,feed_dict={x:img})

最後一條語句就是提取某層的數據輸出作為特征。

註意:這個程序並沒有經過訓練,因此提取出的參數隻是初始化的參數。

以上就是python深度學習tensorflow1.0參數和特征提取的詳細內容,更多關於python tensorflow參數特征提取的資料請關註WalkonNet其它相關文章!

推薦閱讀: