当前位置: 代码迷 >> python >> 在两个子图之间的Tensorflow中共享权重
  详细解决方案

在两个子图之间的Tensorflow中共享权重

热度:39   发布时间:2023-06-13 13:42:33.0

我有以下设置,其中每个输入包含两个轨迹。 我希望左图与右图具有相同的权重

我尝试按照此处描述的方法来共享变量, ,但它无法正常工作。 创建了两个不同的图形。 我究竟做错了什么?

def build_t_model(trajectories):
    """
    Function to build a subgraph
    """
    with tf.name_scope('h1_t'):
        weights = tf.Variable(tf.truncated_normal([150, h1_t_units], stddev=1.0/math.sqrt(float(150))), name='weights')
        biases = tf.Variable(tf.zeros([h1_t_units]), name='biases')
        h1_t = tf.nn.relu(tf.matmul(trajectories, weights) + biases)

    with tf.name_scope('h2_t'):
        weights = tf.Variable(tf.truncated_normal([h1_t_units, h2_t_units], stddev=1.0/math.sqrt(float(h1_t_units))), name='weights')
        biases = tf.Variable(tf.zeros([h2_t_units]), name='biases')
        h2_t = tf.nn.relu(tf.matmul(h1_t, weights) + biases)

    with tf.name_scope('h3_t'):
        weights = tf.Variable(tf.truncated_normal([h2_t_units, M], stddev=1.0/math.sqrt(float(h2_t_units))), name='weights')
        biases = tf.Variable(tf.zeros([M]), name='biases')
        h3_t = tf.nn.relu(tf.matmul(h2_t, weights) + biases)

    return h3_t


# We build two trajectory networks. The weights should be shared
with tf.variable_scope('traj_embedding') as scope:        
    self.embeddings_left = build_t_model(self.input_traj)
    scope.reuse_variables()
    self.embeddings_right = build_t_model(self.input_traj_mv)

好的,为此使用tf.get_variable而不是tf.Variable。 这有效

def build_t_model(trajectories):
            """
            Build the trajectory network
            """
            with tf.name_scope('h1_t'):
                weights = tf.get_variable(
                    'weights1', 
                    shape=[150, h1_t_units],
                    initializer=tf.truncated_normal_initializer(
                        stddev=1.0/math.sqrt(float(150))))
                biases = tf.get_variable(
                    'biases1', 
                    initializer=tf.zeros_initializer(shape=[h1_t_units]))
                h1_t = tf.nn.relu(tf.matmul(trajectories, weights) + biases)

            with tf.name_scope('h2_t'):
                weights = tf.get_variable(
                    'weights2', 
                    shape=[h1_t_units, h2_t_units],
                    initializer=tf.truncated_normal_initializer(
                        stddev=1.0/math.sqrt(float(h1_t_units))))
                biases = tf.get_variable(
                    'biases2', 
                    initializer=tf.zeros_initializer(shape=[h2_t_units]))
                h2_t = tf.nn.relu(tf.matmul(h1_t, weights) + biases)

            with tf.name_scope('h3_t'):
                weights = tf.get_variable(
                    'weights3', 
                    shape=[h2_t_units, M],
                    initializer=tf.truncated_normal_initializer(
                        stddev=1.0/math.sqrt(float(h2_t_units))))
                biases = tf.get_variable(
                    'biases3', 
                    initializer=tf.zeros_initializer(shape=[M]))
                h3_t = tf.nn.relu(tf.matmul(h2_t, weights) + biases)
            return h3_t
  相关解决方案