TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例—Jason niu

import tensorflow as tf
# 22 scope (name_scope/variable_scope)
from __future__ import print_function

class TrainConfig:
    batch_size = 20
    time_steps = 20
    input_size = 10
    output_size = 2
    cell_size = 11
    learning_rate = 0.01

class TestConfig(TrainConfig):
    time_steps = 1

class RNN(object):

    def __init__(self, config):
        self._batch_size = config.batch_size
        self._time_steps = config.time_steps
        self._input_size = config.input_size
        self._output_size = config.output_size
        self._cell_size = config.cell_size
        self._lr = config.learning_rate
        self._built_RNN()

    def _built_RNN(self):
        with tf.variable_scope(‘inputs‘):
            self._xs = tf.placeholder(tf.float32, [self._batch_size, self._time_steps, self._input_size], name=‘xs‘)
            self._ys = tf.placeholder(tf.float32, [self._batch_size, self._time_steps, self._output_size], name=‘ys‘)
        with tf.name_scope(‘RNN‘):
            with tf.variable_scope(‘input_layer‘):
                l_in_x = tf.reshape(self._xs, [-1, self._input_size], name=‘2_2D‘)  # (batch*n_step, in_size)
                # Ws (in_size, cell_size)
                Wi = self._weight_variable([self._input_size, self._cell_size])
                print(Wi.name)
                # bs (cell_size, )
                bi = self._bias_variable([self._cell_size, ])
                # l_in_y = (batch * n_steps, cell_size)
                with tf.name_scope(‘Wx_plus_b‘):
                    l_in_y = tf.matmul(l_in_x, Wi) + bi
                l_in_y = tf.reshape(l_in_y, [-1, self._time_steps, self._cell_size], name=‘2_3D‘)

            with tf.variable_scope(‘cell‘):
                cell = tf.contrib.rnn.BasicLSTMCell(self._cell_size)
                with tf.name_scope(‘initial_state‘):
                    self._cell_initial_state = cell.zero_state(self._batch_size, dtype=tf.float32)

                self.cell_outputs = []
                cell_state = self._cell_initial_state
                for t in range(self._time_steps):
                    if t > 0: tf.get_variable_scope().reuse_variables()
                    cell_output, cell_state = cell(l_in_y[:, t, :], cell_state)
                    self.cell_outputs.append(cell_output)
                self._cell_final_state = cell_state

            with tf.variable_scope(‘output_layer‘):
                # cell_outputs_reshaped (BATCH*TIME_STEP, CELL_SIZE)
                cell_outputs_reshaped = tf.reshape(tf.concat(self.cell_outputs, 1), [-1, self._cell_size])
                Wo = self._weight_variable((self._cell_size, self._output_size))
                bo = self._bias_variable((self._output_size,))
                product = tf.matmul(cell_outputs_reshaped, Wo) + bo
                # _pred shape (batch*time_step, output_size)
                self._pred = tf.nn.relu(product)    # for displacement

        with tf.name_scope(‘cost‘):
            _pred = tf.reshape(self._pred, [self._batch_size, self._time_steps, self._output_size])
            mse = self.ms_error(_pred, self._ys)
            mse_ave_across_batch = tf.reduce_mean(mse, 0)
            mse_sum_across_time = tf.reduce_sum(mse_ave_across_batch, 0)
            self._cost = mse_sum_across_time
            self._cost_ave_time = self._cost / self._time_steps

        with tf.variable_scope(‘trian‘):
            self._lr = tf.convert_to_tensor(self._lr)
            self.train_op = tf.train.AdamOptimizer(self._lr).minimize(self._cost)

    @staticmethod
    def ms_error(y_target, y_pre):
        return tf.square(tf.subtract(y_target, y_pre))

    @staticmethod
    def _weight_variable(shape, name=‘weights‘):
        initializer = tf.random_normal_initializer(mean=0., stddev=0.5, )
        return tf.get_variable(shape=shape, initializer=initializer, name=name)

    @staticmethod
    def _bias_variable(shape, name=‘biases‘):
        initializer = tf.constant_initializer(0.1)
        return tf.get_variable(name=name, shape=shape, initializer=initializer)

if __name__ == ‘__main__‘:
    train_config = TrainConfig()  #定义train_config
    test_config = TestConfig()

#     # the wrong method to reuse parameters in train rnn
#     with tf.variable_scope(‘train_rnn‘):
#         train_rnn1 = RNN(train_config)
#     with tf.variable_scope(‘test_rnn‘):
#         test_rnn1 = RNN(test_config)

    # the right method to reuse parameters in train rnn
    #目的使train的RNN调用参数,然后利用variable_scope方法共享RNN,让test的RNN再次调用一样的参数,
    with tf.variable_scope(‘rnn‘) as scope:
        sess = tf.Session()
        train_rnn2 = RNN(train_config)
        scope.reuse_variables()        #告诉TF想重复利用RNN的参数
        test_rnn2 = RNN(test_config)
        # tf.initialize_all_variables() no long valid from
        # 2017-03-02 if using tensorflow >= 0.12
        if int((tf.__version__).split(‘.‘)[1]) < 12 and int((tf.__version__).split(‘.‘)[0]) < 1:
            init = tf.initialize_all_variables()
        else:
            init = tf.global_variables_initializer()
        sess.run(init)

  

原文地址:https://www.cnblogs.com/yunyaniu/p/8387547.html

时间: 2024-07-30 07:10:56

TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例—Jason niu的相关文章

深度学习原理与框架-递归神经网络-RNN_exmaple(代码) 1.rnn.BasicLSTMCell(构造基本网络) 2.tf.nn.dynamic_rnn(执行rnn网络) 3.tf.expand_dim(增加输入数据的维度) 4.tf.tile(在某个维度上按照倍数进行平铺迭代) 5.tf.squeeze(去除维度上为1的维度)

1. rnn.BasicLSTMCell(num_hidden) #  构造单层的lstm网络结构 参数说明:num_hidden表示隐藏层的个数 2.tf.nn.dynamic_rnn(cell, self.x, tf.float32) # 执行lstm网络,获得state和outputs 参数说明:cell表示实例化的rnn网络,self.x表示输入层,tf.float32表示类型 3. tf.expand_dim(self.w, axis=0) 对数据增加一个维度 参数说明:self.w表

TF之RNN:matplotlib动态演示之基于顺序的RNN回归案例实现高效学习逐步逼近余弦曲线—Jason niu

import tensorflow as tf import numpy as np import matplotlib.pyplot as plt BATCH_START = 0 TIME_STEPS = 20 BATCH_SIZE = 50 INPUT_SIZE = 1 OUTPUT_SIZE = 1 CELL_SIZE = 10 LR = 0.006 BATCH_START_TEST = 0 def get_batch(): global BATCH_START, TIME_STEPS #

TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值—Jason niu

#TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值 import tensorflow as tf input1 = tf.placeholder(tf.float32) #TF一般只能处理float32的数据类型 input2 = tf.placeholder(tf.float32) #ouput = tf.mul(input1, input2) ouput = tf.multiply(input1, input2) #定义两个变量相乘 w

SA:T1法利用Matlab编写主函数实现对一元函数优化求解——Jason niu

%SA:T1法利用Matlab编写主函数实现对一元函数优化求解--Jason niu x = 1:0.01:2; y = sin(10*pi*x) ./ x; figure plot(x,y,'linewidth',1.5) ylim([-1.5, 1.5]) xlabel('x') ylabel('y') title('SA:T1法利用Matlab编写主函数实现对一元函数y = sin(10*pi*x) / x优化求解-Jason niu') hold on [maxVal,maxIndex]

SA:利用SA算法解决TSP(数据是14个虚拟城市的横纵坐标)问题——Jason niu

%SA:利用SA算法解决TSP(数据是14个虚拟城市的横纵坐标)问题--Jason niu X = [16.4700 96.1000 16.4700 94.4400 20.0900 92.5400 22.3900 93.3700 25.2300 97.2400 22.0000 96.0500 20.4700 97.0200 17.2000 96.2900 16.3000 97.3800 14.0500 98.1200 16.5300 97.3800 21.5200 95.5900 19.4100

TF:Tensorflor之session会话的使用,定义两个矩阵,两种方法输出2个矩阵相乘的结果—Jason niu

import tensorflow as tf matrix1 = tf.constant([[3, 20]]) matrix2 = tf.constant([[6], [100]]) product = tf.matmul(matrix1, matrix2) # method 1,常规方法 sess = tf.Session() result = sess.run(product) print(result) sess.close() # # method 2,with方法 # with tf

nodejs中利用expresss脚手架和bootstrap,数据库mongodb搭建的留言板案例

## 1. 先打开编辑器,创建一个项目 ## 2. 再打开cmd命令提示符下载express脚手架 express   项目名   --view=ejs 或express   -e    项目名 ## 3. 在cmd中进入项目名(myapp)下载所需的依赖 cd myapp  --------->cnpm  install ## 4. 在下载mongoose(前提你电脑上要安装数据库的插件) cn cnpm mongoose  --save ## 5. 在myapp项目中在创建一个文件夹,里面在

利用lvs-nat实现两台web服务器负载均衡的简单案例

写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正.如有不明白的地方,愿可一起探讨. 案例拓扑图 配置主机1 安装ipvsadm # yum -y install ipvsadm # ipvsadm -A -t 10.170.2.80:80 -s rr # ipvsadm -a -t 10.170.2.80:80 -r 192.168.3.101 -m # ipvsadm -a -t 10.170.2.80:80 -r 192.168.3.102 -m # echo 1 > /p

oracle利用分隔符,组合查询想表达的任何话

在实施中,我们可能会遇到客户对显示标题的要求.      比如,本来标题是:广西龙潭医院关于进一步加强政务信息报送工作的通知.然后他们想显示的格式为:<文件名>(桂龙医发[2015]23号 ).那么我们只需用sql语句的分隔符,组合查询出来即可.如下图: 效果图如下: 原文地址:http://bbs.delit.cn/thread-168-1-1.html 转载请注明出处; 撰写人:度量科技http://www.delit.cn