Skip to content

Instantly share code, notes, and snippets.

@nlpjoe
Last active January 20, 2021 04:01
Show Gist options
  • Save nlpjoe/2b72b426ca01b46e5c8ef2b67d701a1b to your computer and use it in GitHub Desktop.
Save nlpjoe/2b72b426ca01b46e5c8ef2b67d701a1b to your computer and use it in GitHub Desktop.
[tensorflow 1.4.0 api] #tf
输入
tf.contrib.layers.input_from_feature_columns(features, self.ubb_column_dict[ubb_noclk_jfy_value_name]) 

计算op
tf.multiply(a, b) 返回逐元素的a * btf.matmul(q, a, transpose_b=True)	将矩阵a与矩阵b相乘得到a * btf.div(a, b, name="scores") a除以b

计算loss
tf.nn.sigmoid_cross_entropy_with_logits(labels, logits, name)
tf.nn.sparse_softmax_cross_entropy_with_logits(labels, logits, name)
tf.sigmoid(self._deep_logits1)



赋值op
tf.identity(self._predict_score1, name="rank_predict_ctr")
tf.where(self.labels['time'] < 3, a, self.labels['time'])

全连接
tf.contrib.layers.fully_connected(inputs=output_emb[i],num_outputs=1,activation_fn=None,normalizer_fn=None,weights_initializer=tf.contrib.layers.xavier_initializer())
core_layers.dense(net,
                  units=num_hidden_units,
                  activation=activation_fn,
                  kernel_initializer=kernel_initializer,
                  kernel_regularizer=kernel_regularizer,
                  name=hidden_layer_scope)

with variable_scope.variable_scope("logits") as scope:

维度转换
tf.concat([self._item_emb, self._user_emb, self.hard_att_input_layer], axis=-1)
tf.expand_dims(output, axis=1) 增加维度
tf.squeeze(output, axis=1) 删除维度
tf.ones_like(self.labels['time'])

正则
tf.contrib.layers.batch_norm(ubb_embedding_mlp, is_training=self.is_training)  	batch_norm
tf.contrib.layers.l2_regularizer(scale=FLAGS.dnn_l2_weight),  l2
tf.contrib.layers.xavier_initializer()

tf.min_max_variable_partitioner(
                max_partitions=self._FLAGS.ps_count,
                min_slice_size=(32 * 1024 * 1024),
                bytes_per_string_element=16)
                
GRU
cell = tf.contrib.rnn.GRUCell(num_units=FLAGS.gru_group_num_units)
outputs, last_states = tf.nn.dynamic_rnn(
    cell=cell,
    inputs=input_to_gru,
    dtype=tf.float32)

数值类型转换
tf.to_int32(tf.contrib.layers.input_from_feature_columns(features, self.seq_length_column))

切片取数
tf.slice(ubb_embed_id_i_reshape, begin=[0, 0, 0],  size=[-1, FLAGS.ubb_pos_slice_len, -1])

在张量的维度上计算非零元素个数.
tf.count_nonzero(tf.squeeze(embedding_i_concat_counts, axis=-1), axis=-1)

获得维度
tf.shape(outputs)[0]

维度划分成list
tf.split(outputs, len(self._ubb_time), axis=1)


tf.sparse_to_dense
激活函数swish
def swish(_x, scope=None):
    with tf.variable_scope(name_or_scope=scope, default_name="swish"):
        _alpha = tf.get_variable(name="swish", 
                                 shape=_x.get_shape()[-1],
                                 dtype=_x.dtype, 
                                 initializer=tf.constant_initializer(1.0),
                                 collections=[ops.GraphKeys.MODEL_VARIABLES, 
                                              ops.GraphKeys.GLOBAL_VARIABLES])
        return _x * tf.nn.sigmoid(_alpha * _x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment