程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python deep learning tensorflow1.0 parameters and feature extraction

編輯:Python

Catalog

tf.trainable_variables() Extract training parameters

Specific examples

tf.trainable_variables() Extract training parameters

stay tf in , The parameters involved in the training are available  tf.trainable_variables() extracted , Such as :

# Take out all the training parameters params=tf.trainable_variables()print("Trainable variables:------------------------")# Loop to list parameters for idx, v in enumerate(params): print(" param {:3}: {:15} {}".format(idx, str(v.get_shape()), v.name))

Here, you can only view the parameters shape and name, There is no specific value . If you want to view the specific value of the parameter , You have to initialize , namely :

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

Empathy , We can also extract the trained value of the image . The image becomes a feature after convolution , To extract these features , You must put the picture first feed go in .

Specific examples # -*- coding: utf-8 -*-"""Created on Sat Jun 3 12:07:59 [email protected]: Administrator"""import tensorflow as tffrom skimage import io,transformimport numpy as np#----------------- To build the network ----------------------# Place holder x=tf.placeholder(tf.float32,shape=[None,100,100,3],name='x')y_=tf.placeholder(tf.int32,shape=[None,],name='y_')# The first convolution layer (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)# Second convolution layer (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)# The third convolutional layer (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)# The fourth convolution (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])# Fully connected layer 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)#--------------------------- Network end ---------------------------#%%# Take out all the training parameters params=tf.trainable_variables()print("Trainable variables:------------------------")# Loop to list parameters for idx, v in enumerate(params): print(" param {:3}: {:15} {}".format(idx, str(v.get_shape()), v.name))#%%# Read the picture img=io.imread('d:/cat.jpg')#resize become 100*100img=transform.resize(img,(100,100))# Three dimensional to four dimensional (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()) # Extract the parameters of the last full connection layer W and bW=sess.run(params[26])b=sess.run(params[27])# Extract the output value of the second full connection layer as a feature fea=sess.run(dense2,feed_dict={x:img})

The last statement is to extract the data output of a certain layer as a feature .

Be careful : This program is not trained , Therefore, the extracted parameters are only initialization parameters .

That's all python Deep learning tensorflow1.0 Details of parameter and feature extraction , More about python tensorflow For information on parameter feature extraction, please pay attention to other relevant articles on the software development network !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved