#matmul:将矩阵 a 乘以矩阵 b,生成a * b
#pow(x,y)=x^y
#subtract:返回x-y 的元素
#multiply 对应元素相乘,不是矩阵相乘,而是相同维度的两个向量(或者矩阵)对应的元素相乘,结果还是原向量的维度一致的向量
#reduce_sum:https://blog.csdn.net/u012193416/article/details/83349138
# n * 1,先对应元素相乘,再通过reduce_sum求和(结果保持向量结果),最后再和w0偏置进行求和
linear_terms = tf.add(w0,tf.reduce_sum(tf.multiply(w,x),1,keep_dims=True)) 
pair_interactions = 0.5 * tf.reduce_sum(
    tf.subtract(
        tf.pow(
            tf.matmul(x,tf.transpose(v)),2),
        tf.matmul(tf.pow(x,2),tf.transpose(tf.pow(v,2)))
    ),axis = 1 , keep_dims=True)

 

 

2.关于tf.truncated_normal()函数介绍

参考:https://blog.csdn.net/qq_36512295/article/details/100599979

tf.truncated_normal(shape, mean, stddev)
释义:截断的产生正态分布的随机数,即随机数与均值的差值若大于两倍的标准差,则重新生成。

shape,生成张量的维度
mean,均值
stddev,标准差

截断正态分布,是指限制正态分布的区间,可以是上限也可以是下限。

 

3.tensorflow的sess.run的解释

比如

a=tf.add(2,5)     #这里本来a为7

b=tf.multiply(a,3) #b=21

sess=tf.Session()

replace_dict={a:15}  #把a=15替换原a

sess.run(b,feed_dict=replace_dict) #这里就是用新a替换掉旧a。所以结果为15X3=45

 

4.tf.feature_column详解

参考:https://blog.csdn.net/kangshuangzhu/article/details/106851826

tensorflow2.0 环境下的tfrecord读写及tf.io.parse_example和tf.io.parse_single_example的区别中已经讲到了从tfrecord 中读取数据需要提供一个dict,里面包含了特征名称和特征的类型,如果我们特征很少,只需要手写这个dict就可以。但是当特征非常多的时候,就需要更方便的工具来生成这个dict。这个工具的就是tf.feature_column,同时tf.feature_column也是一个特征工程的工具,可以用来自动one-hot处理,还有hash分桶等处理。

 

categorical_column_with_vocabulary_list:

对于枚举值量少的类别型特征,比如省份等。

city = tf.feature_column.categorical_column_with_vocabulary_list("city",["shanghai","beijing","guangzhou","tianjin","shenzhen"])

 

categorical_column_with_identity

这个方法用于已经编码的sparse特征,例如,店铺id虽然数量非常大,但是已经把每个店铺id都从0开始编码,那么就可以用。(其实就是先做label encoding)

#其中,num_bucket是最大编号
poi = tf.feature_column.categorical_column_with_identity("poi", num_buckets=10, default_value=0)

categorical_column_with_vocabulary_file

前面已经说了,当sparse特征的种类数量非常巨大的时候,就不能用用categorical_column_with_vocabulary_list了,用categorical_column_with_identity 又需要事先对sparse特征编码,这时候可以用tf.feature_column.categorical_column_with_vocabulary_file命令,读取sparse特征的所有可能取值。当然这种方法的效率也是比较低的,在要求低延迟的线上是不太划算的。

tf.feature_column.categorical_column_with_vocabulary_file(
    key, vocabulary_file, vocabulary_size=None, dtype=tf.dtypes.string,
    default_value=None, num_oov_buckets=0
)

categorical_column_with_hash_bucket

如果sparse特征非常庞大,例如上面的poi可以写成

poi = tf.feature_column.categorical_column_with_hash_bucket("poi", hash_bucket_size=10, dtype=tf.dtypes.int64)

但是应该注意的是,hash_bucket_size的大小应该留有充分的冗余量,否则非常容易出现hash冲突,在这个例子中,一共有3个店铺,把hash_bucket_size设定为10,仍然得到了hash冲突的结果,这样poi的信息就被丢失了一些信息。

 

feature_column.indicator column

tf.feature_column.indicator column  是一个onehot工具,用于把sparse特征进行onehot 变换,用于把categorical_column_with_*工具生成的特征变成onehot 编码

tf.feature_column.indicator column 的入参非只有一个,就是categorical_column_with_*的结果。

poi = tf.feature_column.categorical_column_with_hash_bucket("poi", hash_bucket_size=15, dtype=tf.dtypes.int64)
poi_idc = tf.feature_column.indicator_column(poi)

 

feature_column.embedding_column

用于生成embedding后的张量。

categorical_column: categorical_column_with_* 工具的结果

dimension:embedding后的维度

combiner:对于多种类的sparse特征怎么组合,Currently 'mean', 'sqrtn' and 'sum' are supported

tf.feature_column.embedding_column(
    categorical_column, dimension, combiner='mean', initializer=None,
    ckpt_to_load_from=None, tensor_name_in_ckpt=None, max_norm=None, trainable=True,
    use_safe_embedding_lookup=True
)

 

tf.feature_column.crossed_column()

参考:https://blog.csdn.net/pearl8899/article/details/107979097

对hash映射之后的特征进行交叉。

优势:特征交叉,在有些情况下,特征独自编码与多维特征交叉后的特征特性会有不一样的结果。

 

 

tf.data

参考:https://blog.csdn.net/weixin_31767897/article/details/79365968

在机器学习过程中,对数据的获取、过滤、使用、存储是很重要的一个内容,因为数据可能是不完整的、有杂质的、来源不同的。面对海量数据,我们当然不可能每次都手动整合。Tensorflow框架下,对数据的处理使用的是tf.data,它可以帮助我们以多种方式获取数据、灵活的处理数据和保存数据,使我们能够把更多的精力专注在算法的逻辑上。

dataset = tf.data.Dataset.from_tensor_slices(
   {"a": tf.random_uniform([4]),
    "b": tf.random_uniform([4, 100], maxval=100, dtype=tf.int32)})
print(dataset.output_types)  # ==> "{'a': tf.float32, 'b': tf.int32}"
print(dataset.output_shapes)  # ==> "{'a': (), 'b': (100,)}"

 

tf.Dataset.shuffle

tensorflow中的数据集类Dataset有一个shuffle方法,用来打乱数据集中数据顺序,训练时非常常用。

https://www.cnblogs.com/wisir/p/12932154.html

 

tf.estimator

参考:https://www.cnblogs.com/yifdu25/p/8284196.html

其中,

tf.estimator.DNNLinearCombinedClassifier

DNNLinearCombinedClassifier 类

继承自: Estimator

定义在:tensorflow/python/estimator/canned/dnn_linear_combined.py

TensorFlow Linear 和 DNN 的估算器(estimator)加入了分类模型。

注意:此估算器(estimator)也称为 wide-n-deep

model = tf.estimator.DNNLinearCombinedClassifier(
    model_dir = model_dir,
    linear_feature_columns=base_columns + crossed_columns,
    dnn_feature_columns=deep_columns,
    dnn_hidden_units=[100,50]
)