tensorflow_常用

l1/l2正则化

tensorflow使用L2 regularization正则化修正overfitting过拟合_秦伟H的博客-CSDN博客

『TensorFlow』正则化添加方法整理 - 叠加态的猫 - 博客园

 net = tf.contrib.layers.fully_connected(inputs=net, num_outputs=num_outputs,
                                                activation_fn=activation_fn,
                                                weights_regularizer=tf.contrib.layers.l2_regularizer(l2_reg), \
                                                partitioner=tf.fixed_size_partitioner(partition_size),
                                                scope='mlp_%s' % scope)

在全连接层添加正则 第十六节,使用函数封装库tf.contrib.layers - 大奥特曼打小怪兽 - 博客园

在损失函数上添加正则 『TensorFlow』正则化添加方法整理 - 叠加态的猫 - 博客园

异常情况 tensorflow - regarding the ValueError: If `inputs` don't all have same shape and dtype or the shape - Stack Overflow

with tf.name_scope

tf.variable_scope和tf.name_scope的用法_UESTC_C2_403的博客-CSDN博客

当有操作的时候,就会变成这个名字。

tensorboard

使用tensorflow训练神经网络 | Calvin's Marbles

运行某个张量

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    print(sess.run([labels_scene_market]))

在batch内行转列

labels = features["mylabels"][:,0]
labels = tf.expand_dims(labels, -1)

train和Spec

tf.estimator.train_and_evaluate 详解_黑暗星球的博客-CSDN博客

  if 'max_steps' in self.params:
            train_spec = tf.estimator.TrainSpec(
                input_fn=lambda: data_load.input_fn(
                    self.batch_size),
                max_steps=self.params['max_steps'])
        else:
            train_spec = tf.estimator.TrainSpec(
                input_fn=lambda: data_load.input_fn(
                    self.batch_size))
        
#评估的时候,注意这个steps的设置,如果设置为none,在评估的时候要等好久。设置为1就好了。
        valid_spec = tf.estimator.EvalSpec(
            input_fn=lambda: data_load.input_fn(
                self.batch_size), steps=1,
            start_delay_secs=10,
            throttle_secs=60,
        )

        tf.estimator.train_and_evaluate(estimator, train_spec, valid_spec)
        tf.logging.info("Optimization Finished!")

data filter

# features_origin= tf.parse_example(line, self.feature_spec)
        features_origin = tf.parse_single_example(line, self.feature_spec)
        keeps = features_origin['dp']
        print('keeps')
        print(keeps)
        one_img = tf.expand_dims(keeps, -1)
        print('keeps 1')
        print(one_img)
        aa = tf.transpose(one_img, [1, 0])
        print(aa)

tf.unique_with_counts

TensorFlow函数:tf.unique_with_counts_w3cschool

tf.where 把本来为0的,embedding至为0

# tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8]
y, idx, count = unique_with_counts(x)
y ==> [1, 2, 4, 7, 8]
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]
count ==> [2, 1, 3, 1, 2]


deep_embeds = tf.reshape(
                embedding_lookup_hashtable(wnd_v[key], deep_feat_unique_ids, is_training=is_training,
                                                serving_default_value=tf.zeros([key], tf.float32)),
                                                 shape=[-1, key])
deep_embeds = tf.where(tf.not_equal(deep_feat_unique_ids, tf.zeros_like(deep_feat_unique_ids)), deep_embeds,
                               tf.zeros_like(deep_embeds))
            # deep input, id feature
deep_inputs_single = tf.reshape(tf.nn.embedding_lookup(deep_embeds, deep_feat_unique_index),
                                 shape=[-1, len_cat * key])

tf.greater

tf.greater(a,b)_放下扳手&拿起键盘的博客-CSDN博客_tf.greater

         dense_input_1 = tf.multiply(deep_dense, w1)
            dense_input_2 = tf.where(tf.greater(deep_dense, 1e-6 * tf.ones_like(deep_dense)),
                                     tf.zeros_like(deep_dense), w2)
            dense_input = dense_input_1 + dense_input_2

tf.gather

滑动验证页面

embedding look up是继承了这个类

estimator中的传入config

Tensorflow Estimator 详解

config 参数是一个 tf.estimator.RunConfig 实例, 包含了配置设备, 训练流程控制, 分布式等参数.

params 参数就是一个Python字典, 可以传入任何用户定义的参数, 从上面的讨论我们知道该参数可以传递到 model_fn 和 input_fn

分布式tensorflow

分布式tensorflow(一) | Lynna's Blog

TensorFlow 分布式(Distributed TensorFlow)_跟着大数据和AI去旅行的博客-CSDN博客_tensorflow 分布式

分布式性能调优的几个方面:

1.使用哈希embedding。

KDD 2020 | Facebook提出组合embedding方法在大规模推荐系统中的应用 - 腾讯云开发者社区-腾讯云(embedding 存储方式)

embedding的生成方式:

第一种方式是作为普通的variable存储tensorflow中的Embedding操作详解 - 知乎,在ps场景下只能存储在一台ps服务器上,性能有限。

第二种方式是在第一种方式上增加分区策略tf.nn.embedding_lookup中关于partition_strategy参数详解_DFann的博客-CSDN博客,目前tensorflow支持div和mode两种方式,能够解决第一种存储方式的局限。只在len(params)>1时,partition_strategy才起作用,第一种的情况下len(params)=1,因为传入的是一个tensor,即使这个tensor是n行n列,也是一个tensor.

第三种方式(不太懂)在server端维护hash表,通过hash表映射到内存空间,这样做的好处有两点:

1.id无需有序2.可以动态的插入和删除embeddings 3.无需提前初始化所有的feature embedding,需要的时候再存储,特别是在spare feature时,极大的节省空间

第三种方式的意思是,初始化矩阵存储的时候用kv形式来存储,本来是用一个大矩阵来存储,用大矩阵存储的缺点是浪费内存。

embedding矩阵哈希冲突EmbeddingVariable - 机器学习PAI - 阿里云

哈希冲突的概率_悠悠吾心666的博客-CSDN博客_hash算法冲突概率。在样本里做哈希编码的时候就不存在这个博客里说的这种情况。

在样本里对类别特征先进行哈希编码MurmurHash一致性Hash算法JAVA版_潜水生活的博客-CSDN博客

2.数据分发方式

Tensorflow Dataset操作接口简介_AI算法-图哥的博客-CSDN博客

3.dnn层设置分片参数

根据ps数量做参数分割。

Tensorflow参数分割_shuai_wow的博客-CSDN博客_tensorflow 分割

4. 在 config中设置seastar

TensorFlow 大规模稀疏模型异步训练的分布式优化 - 案例分享 - tf.wiki 社区

serving

TensorFlow Serving + Docker + Tornado机器学习模型生产级快速部署 - 掘金

序列处理

文件IO接口

https://www.tensorflow.org/api_docs/python/tf/io/gfile

if job_name == "chief" and task_index == 0:
    if not tf.gfile.Exists(model_dir):
        tf.gfile.MakeDirs(model_dir)
        tf.gfile.MakeDirs(model_dir + "/profiler")

修改变量值

import tensorflow as tf

# https://icode.best/i/08630344346204


tensor_input = tf.constant([i for i in range(20)])
tensor_input = tf.reshape(tensor_input, [4, 5])

with tf.Session() as sess:
    print(sess.run([tensor_input]))
# tensor_input = tf.where(tf.greater(tensor_input,8),8,tensor_input)
tensor_input = tf.where_v2(tf.equal(tensor_input,3),3333,tensor_input)
with tf.Session() as sess:
    print(sess.run([tensor_input]))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow Datasets(简称TFDS)是一个TensorFlow的库,用于提供公开可用的数据集。它提供了多种数据集,包括自然语言处理、计算机视觉和基准测试数据等。TFDS提供了简化数据集加载和预处理的功能,可以方便地在TensorFlow中使用这些数据集。 安装TFDS 2.1的步骤如下: 1. 首先,确保已经安装了TensorFlowPython环境。安装TensorFlow可以使用pip命令(如pip install tensorflow)。 2. 打开命令行终端,运行下面的命令安装TFDS: pip install tensorflow-datasets==2.1 3. 安装完成后,可以在Python脚本中导入TFDS库并使用其中的数据集进行训练和测试。 TFDS的使用有以下几个主要步骤: 1. 导入必要的库和模块: import tensorflow_datasets as tfds import tensorflow as tf 2. 加载数据集: dataset, info = tfds.load('mnist', split='train', with_info=True) 这里以加载MNIST手写数字数据集为例,split参数用于指定加载的数据集划分,with_info参数用于获取数据集的信息。 3. 预处理数据集: 对加载的数据集进行预处理,例如归一化、扩充等。 4. 创建数据集迭代器: dataset = dataset.shuffle(1000).batch(128).prefetch(tf.data.experimental.AUTOTUNE) 使用shuffle、batch、prefetch等函数对数据集进行处理,并创建一个数据集迭代器。 5. 训练和测试: for batch in dataset: train_step(batch) 使用创建的数据集迭代器进行训练和测试,可根据具体需求进行相应操作。 TFDS使得数据集的使用更加方便,减少了数据集的预处理工作,利用TFDS可以快速加载常用的数据集,并在TensorFlow中进行模型的训练和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值