如何从可训练的张量流量变量转换为不可训练的张量流量变量?

时间:2019-04-11 04:02:42

标签: python tensorflow

我需要恢复DNN(VGG16Net),并使用转移学习来构建另一个网络。因此,在这里,我需要将一些过滤器,偏差变量从可训练的tensorflow变量转换为不可训练的变量(我使用的是原生tensorflow框架,而不是keras或任何更高的杠杆包)。

例如从卷积层4_1获取权重 我用了 2019-04-11 13:49:57.574 1522-1522/? E/libc: failed to read response to DumpRequest packet: Math result not representable 2019-04-11 13:49:58.496 1665-1680/system_process E/ActivityManager: ANR in co.getdere (co.getdere/.MainActivity) PID: 8353 Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 51. Wait queue head age: 5504.1ms.) Load: 10.5 / 5.32 / 3.29 CPU usage from 93941ms to 0ms ago (2019-04-11 13:48:21.960 to 2019-04-11 13:49:55.901): 89% 23829/com.android.chrome:privileged_process0: 89% user + 0% kernel 3.6% 1423/adbd: 0.1% user + 3.4% kernel / faults: 14671 minor 3.5% 1665/system_server: 1.5% user + 1.9% kernel / faults: 7762 minor 24 major 1.1% 1370/logd: 0.7% user + 0.4% kernel / faults: 63 minor 1.9% 1442/surfaceflinger: 0.3% user + 1.6% kernel / faults: 237 minor 1 major 1.3% 13488/com.google.android.gms.persistent: 0.7% user + 0.5% kernel / faults: 2441 minor 8 major 1.1% 547/kswapd0: 0% user + 1.1% kernel 0.8% 1426/android.hardware.audio@2.0-service: 0% user + 0.8% kernel / faults: 6 minor 0.7% 1433/android.hardware.graphics.composer@2.1-service: 0.1% user + 0.6% kernel / faults: 8 minor 0.5% 1435/android.hardware.sensors@1.0-service: 0% user + 0.5% kernel 0.4% 2952/com.google.android.googlequicksearchbox:search: 0.2% user + 0.2% kernel / faults: 3148 minor 21 major 0.3% 13596/com.google.android.gms: 0.2% user + 0% kernel / faults: 2706 minor 6 major 0.2% 1516/audioserver: 0.1% user + 0.1% kernel / faults: 338 minor 0.2% 23759/com.android.chrome: 0.1% user + 0.1% kernel / faults: 331 minor 14 major 0.1% 7/rcu_preempt: 0% user + 0.1% kernel 0.1% 1895/com.android.phone: 0.1% user + 0% kernel / faults: 92 minor 0.1% 1434/android.hardware.power@1.0-service: 0% user + 0% kernel 0% 1791/com.android.systemui: 0% user + 0% kernel / faults: 105 minor 2 major 0% 4016/kworker/u9:4: 0% user + 0% kernel 0% 1//init: 0% user + 0% kernel / faults: 66 minor 0% 7010/kworker/u9:1: 0% user + 0% kernel 0% 7186/kworker/0:1: 0% user + 0% kernel 0% 2291/com.google.android.apps.nexuslauncher: 0% user + 0% kernel / faults: 23 minor 0% 6866/kworker/u9:0: 0% user + 0% kernel 0% 3/ksoftirqd/0: 0% user + 0% kernel 0% 3649/kworker/u9:2: 0% user + 0% kernel 0% 6901/kworker/u8:0: 0% user + 0% kernel 0% 13551/com.google.android.inputmethod.latin: 0% user + 0% kernel / faults: 70 minor 0% 1498/dmcrypt_write: 0% user + 0% kernel 0% 1510/jbd2/dm-0-8: 0% user + 0% kernel 0% 1639/dhcpclient: 0% user + 0% kernel 0% 3939/kworker/3:2: 0% user + 0% kernel 0% 1528/android.hardware.gnss@1.0-service: 0% user + 0% kernel 0% 3664/kworker/2:1: 0% user + 0% kernel 0% 11/migration/1: 0% user + 0% kernel 0% 12/ksoftirqd/1: 0% user + 0% kernel 0% 17/ksoftirqd/2: 0% user + 0% kernel 0% 20/migration/3: 0% user + 0% kernel 0% 21/ksoftirqd/3: 0% user + 0% kernel 0% 698/hwrng: 0% user + 0% kernel 0% 1070/ueventd: 0% user + 0% kernel 0% 1432/android.hardware.graphics.allocator@2.0-service: 0% user + 0% kernel / faults: 13 minor 0% 1436/android.hardware.wifi@1.0-service: 0% user + 0% kernel 0% 1525/netd: 0% user + 0% kernel / faults: 63 minor 0% 1527/wificond: 0% user + 0% kernel 0% 1533/tombstoned: 0% user + 0% kernel 0% 5000/kworker/u8:2: 0% user + 0% kernel 0% 7072/kworker/1:1: 0% user + 0% kernel +0% 8261/logcat: 0% user + 0% kernel +0% 8285/kworker/2:0: 0% user + 0% kernel +0% 8353/co.getdere: 0% user + 0% kernel 53% TOTAL: 36% user + 16% kernel + 0.1% iowait + 0.1% softirq CPU usage from 2005ms to 2559ms later (2019-04-11 13:49:57.906 to 2019-04-11 13:49:58.460): 151% 8353/co.getdere: 63% user + 87% kernel / faults: 2179 minor 21% 8353/co.getdere: 5.4% user + 16% kernel 20% 8364/HeapTaskDaemon: 10% user + 9.1% kernel 3.6% 8587/GLThread 6152: 3.6% user + 0% kernel 1.8% 8358/Jit thread pool: 0% user + 1.8% kernel 1.8% 8424/GLThread 5989: 1.8% user + 0% kernel 1.8% 8427/GLThread 5992: 1.8% user + 0% kernel 1.8% 8428/GLThread 5993: 0% user + 1.8% kernel 1.8% 8431/GLThread 5996: 1.8% user + 0% kernel 1.8% 8432/GLThrea 2019-04-11 13:49:58.681 1432-1432/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 118 ms 2019-04-11 13:49:58.706 1665-2909/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da 2019-04-11 13:50:06.930 1665-1679/system_process E/memtrack: Couldn't load memtrack module 但是变量“ conv4_3_filter”始终是可训练的变量。 因此,在这里我试图找到一种通用方法将任何tensorflow变量从可训练转换为不可训练。 我该如何解决?

1 个答案:

答案 0 :(得分:1)

我认为无法修改trainable的{​​{1}}属性。但是,有多种解决方法。

假设您有两个变量:

tf.Variable

在使用import tensorflow as tf v1 = tf.Variable(tf.random_normal([2, 2]), name='v1') v2 = tf.Variable(tf.random_normal([2, 2]), name='v2') 类及其子类进行优化时,默认情况下,它将从tf.train.Optimizer集合中获取变量。默认情况下,您用tf.GraphKeys.TRAINABLE_VARIABLES定义的每个变量都会添加到此集合中。您可以做的是清除此集合,并仅将您愿意优化的那些变量追加到该集合中。例如,如果我只想优化trainable=True而不是v1

v2

另一种方法是使用优化器的var_list = tf.trainable_variables() print(var_list) # [<tf.Variable 'v1:0' shape=(2, 2) dtype=float32_ref>, # <tf.Variable 'v2:0' shape=(2, 2) dtype=float32_ref>] tf.get_default_graph().clear_collection(tf.GraphKeys.TRAINABLE_VARIABLES) cleared_var_list = tf.trainable_variables() print(cleared_var_list) # [] tf.add_to_collection(tf.GraphKeys.TRAINABLE_VARIABLES, var_list[0]) updated_var_list = tf.trainable_variables() print(updated_var_list) # [<tf.Variable 'v1:0' shape=(2, 2) dtype=float32_ref>] 关键字参数,并在训练期间(执行var_list时)传递要更新的变量:

train_op