with tf.name_scope('conv1') as scope:
weights1 = tf.Variable([1.0, 2.0], name='weights')
bias1 = tf.Variable([0.3], name='bias')
tf.random_uniform([6040,32], -1, 1)
tf.nn.embedding_lookup(gender_embed_matrix, user_gender, name = "gender_embed_layer")
gender_embed_matrix | user_gender | 查找结果 | |
---|---|---|---|
shape | (2,16) | (?,1) | (?, 1, 16) |
tf.concat()拼接的张量只会改变一个维度,其他维度是保存不变的
在tf中对于axis的理解:
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(
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(tensor,shape,name=None) #将tensor转换为 形状为shape的张量
''' 对movie_categories_embed_layer 按列求和 ,并保持形状不变 '''
tf.reduce_sum(movie_categories_embed_layer, axis=1, keep_dims=True)
tf.truncated_normal([window_size, embed_dim, 1, filter_num],stddev=0.1)
tf.constant(0.1, shape=[2,3]) #形状为(2,3) 填充0.1
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(conv_layer,filter_bias)
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
args | |||||
---|---|---|---|---|---|
input | filter | strides | padding | use_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的第四维 | 卷积时在图像每一维的步长,这是一个一维的向量,长度4 | string类型的量,只能是"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.max_pool(value, ksize, strides, padding, name=None)
对于图片的话 shape是这样的:[batch, height, width, channels] // 批数,高,宽,通道数
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")