关于深度学习推荐系统的tensorflow一些API的使用(一)

tensorflow 的一些API的使用

  • tf.name_scope
    它的主要目的是为了更加方便地管理参数命名。
		with tf.name_scope('conv1') as scope:
		    		weights1 = tf.Variable([1.0, 2.0], name='weights')
		    		bias1 = tf.Variable([0.3], name='bias')
  • tf.Variable
    TensorFlow有一个更好的方法来表示变量:Variable 。 一个Variable代表一个可修改的张量
  • tf.random_uniform
    tf.random_uniform([6040,32], -1, 1)
    在shape = (6040,32) 的张量中随机填充 -1 ~ 1 之间的随机值
  • tf.nn.embedding_lookup
    tf.nn.embedding_lookup(gender_embed_matrix, user_gender, name = "gender_embed_layer")
    使用user_gender在嵌入矩阵 gender_embed_matrix 中查找
gender_embed_matrixuser_gender查找结果
shape(2,16)(?,1)(?, 1, 16)
  • tf.concat

tf.concat()拼接的张量只会改变一个维度,其他维度是保存不变的
在tf中对于axis的理解:

  1. axis=0实际就是按行拼接,axis=1就是按列拼接
  2. 但这实际上这只有在我们的输入是二维矩阵时才可以这样理解。axis的实际含义是根据axis指定的维度进行连接,如矩阵m1的维度为(2,3), 那么axis=0就代表了第一个维度‘2’
  3. 此外,axis的值也可以设置为负数,如axis=-1实际上就是指倒数第一个维度,axis=2 就是按照第三个维度拼接,各张量第三维度相加,其余维度不变。
tf.concat([tensor1, tensor2, tensor3,...], axis)  
  t1 = [[1, 2, 3], [4, 5, 6]]
  t2 = [[7, 8, 9], [10, 11, 12]]
  tf.concat([t1, t2], 0)  # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
  tf.concat([t1, t2], 1)  # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
user_combine_layer = tf.concat([uid_fc_layer, gender_fc_layer, age_fc_layer, job_fc_layer], 2)
'''对于uid_fc_layer, gender_fc_layer, age_fc_layer, job_fc_layer第三维相加,其他维度不变'''
  • tf.contrib.layers.fully_connected
    增加一个全连接层
    自动初始化w和b
    激活函数默认为relu函数
tf.contrib.layers.fully_connected(
    inputs,
    num_outputs,
    activation_fn=tf.nn.relu,
    normalizer_fn=None,
    normalizer_params=None,
    weights_initializer=initializers.xavier_initializer(),
    weights_regularizer=None,
    biases_initializer=tf.zeros_initializer(),
    biases_regularizer=None,
    reuse=None,
    variables_collections=None,
    outputs_collections=None,
    trainable=True,
    scope=None
)

https://tensorflow.google.cn/api_docs/python/tf/contrib/layers/fully_connected#args
例子:

user_combine_layer = tf.contrib.layers.fully_connected(user_combine_layer, 200, tf.tanh)  #(?, 1, 200)
  • tf.reshape
tf.reshape(tensor,shape,name=None)   #将tensor转换为 形状为shape的张量
  • tf.reduce_sum
    reduce_sum应该理解为压缩求和,用于降维
''' 对movie_categories_embed_layer 按列求和 ,并保持形状不变 '''
tf.reduce_sum(movie_categories_embed_layer, axis=1, keep_dims=True)
  • tf.truncated_normal
    从截断的正态分布中输出随机值。
    生成的值服从具有指定平均值和标准偏差的正态分布
tf.truncated_normal([window_size, embed_dim, 1, filter_num],stddev=0.1)
  • tf.constant
    在tf中创建常量
tf.constant(0.1, shape=[2,3])     #形状为(2,3) 填充0.1
  • tf.expand_dims
    想要维度增加一维

tf.expand_dims(
    input,
    axis=None,
    name=None,
    dim=None
)

one_img = tf.expand_dims(one_img, 0)
one_img = tf.expand_dims(one_img, -1) #-1表示最后一维
  • tf.nn.bias_add
tf.nn.bias_add(conv_layer,filter_bias)

在这里插入图片描述

  • tf.nn.conv2d
    tf.nn.conv2d是TensorFlow里面实现卷积的函数
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
args
inputfilterstridespaddinguse_cudnn_on_gpu
指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维卷积时在图像每一维的步长,这是一个一维的向量,长度4string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true

结果返回一个Tensor,这个输出,就是我们常说的feature map,shape仍然是[batch, height, width, channels]这种形式

参考 https://www.cnblogs.com/qggg/p/6832342.html

  • tf.nn.relu
    激活函数 ReLu=max(0,x)
  • tf.nn.max_pool
    max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似
tf.nn.max_pool(value, ksize, strides, padding, name=None)

对于图片的话 shape是这样的:[batch, height, width, channels] // 批数,高,宽,通道数

  • tf.nn.dropout
    tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层。Dropout就是在不同的训练过程中随机扔掉一部分神经元。也就是让某个神经元的激活值以一定的概率p,让其停止工作,这次训练过程中不更新权值,也不参加神经网络的计算。
    train的时候才是dropout起作用的时候,test的时候不应该让dropout起作用。
    dropout必须设置概率keep_prob,并且keep_prob也是一个占位符,跟输入是一样的。
tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None,name=None) 

tf.nn.dropout(pool_layer_flat, dropout_keep_prob, name = "dropout_layer")
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值