python及tensorflow代码学习使用笔记

这篇笔记详述了Python和TensorFlow的使用技巧,包括列表操作、numpy数组与列表的区别、初始化numpy数组、遍历列表、排序、张量操作如卷积、加权求和、softmax、张量扩展、填充、取最大值索引、beam search等,还涉及到TensorFlow的会话恢复、参数传递、检查点管理和训练过程中的操作如损失计算、优化器应用等。
摘要由CSDN通过智能技术生成

1、对于列表的连接操作
sequence是一个数值列表,想要在sequence前面再加上一项,具体的操作如下所示:

inp = [start_id] + sequence[:]     #start_id是一个常数
#即使用加号实现两个列表的连接

对于在列表尾直接添加一项,可以使用append操作

target.append(stop_id)       #在列表target尾部添加end_token

2、对于列表的截断操作

target = target[:maxlen]         #将targrt截断成长度为max_len的列表
# Extract the output ids from the Hypothesis
output_ids = [int(t) for t in best_hyp.tokens[1:]]    #去掉第一个元素

3、numpy array 和 list 的区别
numpy array like the shape of (batch_size, <=max_enc_steps)
list是用逗号分隔的元素

4、初始化numpy array

enc_batch = np.zeros((hps.batch_size, max_enc_seq_len), dtype=np.int32)

5、将列表元素复制给numpy array

enc_batch[i, :] = ex.enc_input[:]  #enc_input是一个数值列表
                                   #enc_batch是一个numpy_array

6、遍历一个列表时可以用enumerate记录遍历的下标

for i, ex in enumerate(example_list):
    self.enc_batch_extend_vocab[i, :] = ex.enc_input_extend_vocab[:]
                                                       # numpy array <- list

7、对一个列表中的元素按照对应的属性进行排序

inputs = sorted(inputs, key=lambda inp: inp.enc_len) #按照编码序列的长度进行排序
def sort_hyps(hyps):
    '''Return a list of Hypothesis objects, sorted by descending average log
    probabilty'''
    return sorted(hyps, key=lambda h: h.avg_log_prob, reverse=True)
    # 降序排列 

8、取列表中的几个元素的操作

inputs[i:i + self._hps.batch_size]

9、声明一个LSTM Cell并进行双向动态RNN解码操作

with tf.variable_scope('encoder'):
     cell_fw = tf.contrib.rnn.LSTMCell(self._hps.hidden_dim,
     initializer=self.rand_unif_init, state_is_tuple=True)
     cell_bw = tf.contrib.rnn.LSTMCell(self._hps.hidden_dim,
     initializer=self.rand_unif_init, state_is_tuple=True)
     (encoder_outputs, (fw_st, bw_st)) = tf.nn.bidirectional_dynamic_rnn(
     cell_fw, cell_bw, encoder_inputs, dtype=tf.float32, sequence_length=
     seq_len, swap_memory=True)
     encoder_outputs = tf.concat(axis=2, values=encoder_outputs)
     # concatenate the forwards and backwards states 连接操作
     # [batch_size, <=max_enc_steps, 2*hidden_dim]

10、将两个LSTMStateTuple合并成一个LSTMStateTuple

with tf.variable_scope('reduce_final_st'):
     w_reduce_c = tf.get_variable('w_reduce_c', [hidden_dim*2, hidden_dim],
     dtype=tf.float32, initializer=self.trunc_norm_init)
     w_reduce_h = tf.get_variable('w_reduce_h', [hidden_dim*2, hidden_dim],
     dtype=tf.float32, initializer=self.trunc_norm_init)
     bias_reduce_c = tf.get_variable('bias_reduce_c', [hidden_dim],
     dtype=tf.float32, initializer=self.trunc_norm_init)
     bias_reduce_h = tf.get_variable('bias_reduce_h', [hidden_dim],
     dtype=tf.float32, initializer=self.trunc_norm_init)

     # apply linear layer
     old_c = tf.concat(axis=1, values=[fw_st.c, bw_st.c])
     # fw_st.c [batch_size, hidden_dim]
     # concatnation of fw and bw cell ,after is [batch_size, hidden_dim *2 ]
     old_h = tf.concat(axis=1, values=[fw_st.h, bw_st.h])
     new_c = tf.nn.relu(tf.matmul(old_c, w_reduce_c) + bias_reduce_c)
     new_h = tf.nn_relu(tf.matmul(old_h, w_reduce_h) + bias_recuce_h)
     return tf.contrib.rnn_LSTMStateTuple(new_c, new_h)

11、tensor在指定维度上扩展维度操作

# reshape encoder_states (need to insert a dim)
encoder_states = tf.expand_dims(encoder_states, axis=2)
# now shape is (batch_size,  attn_len, 1, attn_size)

12、计算4-D张量的2-D卷积

# reshape encoder_states (need to insert a dim)
encoder_states = tf.expand_dims(encoder_states, axis=2)
# now shape is (batch_size, attn_len, 1, attn_size)

# to calculate attention, we calculate
#   v^T tanh(W_h h_i + W_s s_t _ b_attn)
# where h_i is an encoder state, and s_t a decoder state
# attn_vec_size is the length of vectors v, b_attn, (W_h h_i) and (W_s s_t).
# we set it to be equal to the size of the encoder states.
attention_vec_size = attn_size

# get the weight matrix W_h and apply it to each encoder state to get 
# (W_h h_i), the encoder features
W_h = variable_scope.get_variable("W_h", [1, 1, attn_size, attention_vec_size])
encoder_features = nn_ops.conv2d(encoder_states, W_h, [1, 1, 1, 1], "SAME")
# shape (batch_size, attn_length, 1, attention_vec_size)

# get the weight vector v
v = variable_scope.get_variable("v", [attention_vec_size])

13、将一个二维Tensor或者以Tensor为元素的列表求加权和,运用线性层后返回加权和

def linear(args, output_size, bias, bias_start=0.0, scope=None):
    '''Linear map: sum_i(args[i] * W[i]), where W[i] is a variable.
    Args:
        args: a 2D Tensor or a list of 2D, batch x n, Tensors.
        output_size: int, second dimension of W[i].
        bias: boolean, whether to add a bias term or not.
        bias_start: starting value to initialize the bias; 0 by default.
        scope: VariableScope for created subgraph; defaults to "Linear"

    Returns:
        A 2D Tensor with shape [batch x output_size] equal to
        sum_i(args[i] * W[i]), where W[i]s are newly created matrices.

    if args is None or (isinstance(args, (list, tuple)) and not args):
       raise ValueError("`args` must be specified")
    if not isinstance(args, (list, tuple)):
       args = [args]

    # Calculate the total size of arguments on dimension 1.
    total_arg_size = 0
    shapes = [a.get_shape().as_list() for a in args]   
    #将得到的维度信息变成列表的形式
    for shape in shapes:
        if len(shape) != 2:
           raise ValueError("Linear is expecting 2D arguments: %s" % str(shapes))
        if not shape[1]:
           raise ValueError("Linear expects shape[1] of arguments: %s" % str(shapes))
        else:
           total_arg_size += shape[1]

    # Now the computation.
    with tf.variable_scope(scope or "Linear"):
         matrix = tf.get_variable("Matrix", [total_arg_size, output_size])
         if len(args) == 1:
            res = tf.matmul(args[0], matrix)
         else:
            res = tf.matmul(tf.concat(axis=1, values=args), matrix)  
            # res: [batch_size x output_size]
            # tf.concat(axis=1, values=args): [batch_size x total_arg_size]
         if not bias:
            return res
         bias_term = tf.get_variable("Bias", [output_size],   
         initiali
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值