如何从tensorflow完全连接获得权重

dbo*_*ers 8 python tensorflow

我正在尝试在训练后从模型中提取权重.这是一个玩具的例子

import tensorflow as tf
import numpy as np

X_ = tf.placeholder(tf.float64, [None, 5], name="Input")
Y_ = tf.placeholder(tf.float64, [None, 1], name="Output")

X = ...
Y = ...
with tf.name_scope("LogReg"):
    pred = fully_connected(X_, 1, activation_fn=tf.nn.sigmoid)
    loss = tf.losses.mean_squared_error(labels=Y_, predictions=pred)
    training_ops = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(200):
        sess.run(training_ops, feed_dict={
            X_: X,
            Y_: Y
        })
        if (i + 1) % 100 == 0:
            print("Accuracy: ", sess.run(accuracy, feed_dict={
                X_: X,
                Y_: Y
            }))

# Get weights of *pred* here
Run Code Online (Sandbox Code Playgroud)

我看过从张量流模型文档获取权重 但是找不到检索权重值的方法.

所以在玩具示例中,假设X_具有形状(1000,5),我怎样才能获得1层权重中的5个值

Ali*_*Ali 12

您的代码中存在一些需要修复的问题:

1-您需要使用variable_scope而不是name_scope在以下行(请参阅TensorFlow文档以了解它们之间的差异):

with tf.name_scope("LogReg"):
Run Code Online (Sandbox Code Playgroud)

2-为了能够在代码中稍后检索变量,您需要知道它的名称.因此,您需要为感兴趣的变量指定一个名称(如果您不支持一个名称,则会分配一个默认名称,但是您需要弄清楚它是什么!):

pred = tf.contrib.layers.fully_connected(X_, 1, activation_fn=tf.nn.sigmoid, scope = 'fc1')
Run Code Online (Sandbox Code Playgroud)

现在让我们看看上面的修复如何帮助我们获得变量的值.每一层都有两种类型的变量:权重和偏差.在下面的代码片段(您的修改版本)中,我将仅展示如何检索完全连接的图层的权重:

X_ = tf.placeholder(tf.float64, [None, 5], name="Input")
Y_ = tf.placeholder(tf.float64, [None, 1], name="Output")

X = np.random.randint(1,10,[10,5])
Y = np.random.randint(0,2,[10,1])

with tf.variable_scope("LogReg"):
    pred = tf.fully_connected(X_, 1, activation_fn=tf.nn.sigmoid, scope = 'fc1')
    loss = tf.losses.mean_squared_error(labels=Y_, predictions=pred)
    training_ops = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

with tf.Session() as sess:

    all_vars= tf.global_variables()
    def get_var(name):
        for i in range(len(all_vars)):
            if all_vars[i].name.startswith(name):
                return all_vars[i]
        return None
    fc1_var = get_var('LogReg/fc1/weights')

    sess.run(tf.global_variables_initializer())    
    for i in range(200):
        _,fc1_var_np = sess.run([training_ops,fc1_var], feed_dict={
        X_: X,
        Y_: Y 
        })
        print fc1_var_np
Run Code Online (Sandbox Code Playgroud)