![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
深度学习deep learning
醉意流年go
这个作者很懒,什么都没留下…
展开
-
tf.keras 使用类似Dict的方式进行建模、训练并保存
tensorflow keras 自定义字典输入和字典层 训练模型原创 2022-08-22 14:20:11 · 398 阅读 · 0 评论 -
tensoflow实现相对位置编码(relative position representation)
# coding:utf-8"""@author: liu@File: relative_pos_representation_RPR.py@CreateTime: 2021/8/17 """"""实现相对位置编码参考: https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/layers/common_attention.py"""import tensorflow as tfdef ge.原创 2021-08-19 17:05:28 · 957 阅读 · 0 评论 -
transformers获取和验证bert的中间层
# coding:utf-8"""@author: liu@File: get_tfbertmainlayer_demo.py@CreateTime: 2021/7/26 """import numpy as npimport tensorflow as tffrom transformers import TFBertModel, TFBertMainLayer, BertConfig, BertTokenizerbert_config = BertConfig.from_.原创 2021-07-27 11:17:14 · 1760 阅读 · 0 评论 -
tf2中feature_columns与keras model的结合使用
分为两种情况:1、tensorflow 最新高版本的情况下(比如在tf>=2.4)2、在tensorflow==2.0的情况下在tensorflow >=2.4的情况下:import tensorflow as tffrom tensorflow.keras import layersdef train_save_model(): genre_vocab_list = ["female", "male"] level_vocab_list..原创 2021-07-06 09:48:12 · 460 阅读 · 0 评论 -
tf 2.0 keras和feature_column的结合使用
tensorflow == 2.4# coding:utf-8"""@author: liu@File: tf_feature_col.py@CreateTime: 2021/7/5 """import tensorflow as tffrom tensorflow.keras import layersgenre_vocab_list = ["female", "male"]level_vocab_list = ["low", "middle", "up"]原创 2021-07-05 13:43:46 · 376 阅读 · 0 评论 -
tensorflow padded_batch的注意事项
tensorflow版本:1.13.11、在padded_batch中,若函数出现错误:TypeError: If shallow structure is a sequence, input must also be a sequence. Input has type: <class 'int'>. 案例如下,在以下案例中, 出现上述错误的原因是:在paddd_batch中的参数:padding_values, 如果不设置此参数,则没有问题,即此行代码更改为:...原创 2021-02-04 11:08:36 · 1070 阅读 · 1 评论 -
tensorflow dataset tfrecord的写入和读取
tensorflow的版本:1.13.1针对tensorflow 2以及上的版本,可以参考tensorflow的官方介绍:https://www.tensorflow.org/tutorials/load_data/tfrecord def create_int_feature(self, values): feature = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values)))原创 2021-02-03 16:20:00 · 286 阅读 · 0 评论 -
Asymmetric Loss For Multi-Label Classification 重现 【非对称损失函数】 tensorflow
应用场景:不平衡下的多标签和单标签多分类问题分为两部分:1、多标签分类def multi_label_asymmtric_loss_tf(labels, logits, gamma_pos=1, gamma_neg=4, clip=0.05, eps=1e-8, ): # 计算概率 caalculating probabilities logits_sigmoid = tf.nn.sigmoid(logits) logits_sigmoid_pos = logits_原创 2021-01-27 09:58:16 · 1889 阅读 · 3 评论 -
tensorflow cross entropy实现交叉熵
import tensorflow as tfprint(tf.__version__)tf.enable_eager_execution()logits = tf.Variable(tf.truncated_normal(shape=(2, 5)))labels = tf.Variable([[1, 0, 0, 0, 0], [0, 0, 0, 0, 1]], dtype=tf.float32)"""下面是实现交叉熵损失的两种方法:等价的两种方法"""# 第一种交叉熵实现方.原创 2021-01-25 14:39:41 · 107 阅读 · 0 评论 -
tensorflow 1.13, keras + placeholder + saved_model的混用【主要是验证一下】
共分为两步:1、训练模型并保存2、加载模型进行预测1、训练模型并保存代码如下:import tensorflow as tf # tf.version==1.13.1import numpy as npx = tf.placeholder(tf.int32, shape=(None, 100), name="x")y = tf.placeholder(tf.int32, shape=(None, ))# embeddings = tf.keras.layers..原创 2021-01-13 11:47:55 · 494 阅读 · 0 评论 -
tensorboard的基本使用
import tensorflow as tftf.summary.scalar("1", 1.0)tf.summary.scalar("2", 2.0)"""上述在使用了若干个tf.summary函数中记录了信息值后,需要对所有需要的记录纸进行全部的合并,需要调用tf.summary.merge_all()"""merged_all = tf.summary.merge_all()writer = tf.summary.FileWriter("logdir/", graph=tf..原创 2020-10-09 12:42:50 · 194 阅读 · 0 评论 -
tensorflow 实现RBFSoftmax
参考论文:RBF_softmax:Learning Deep Representative Prototypes with Radial Basis Function Softmax"""RBF_softmax:Learning Deep Representative Prototypes with Radial Basis Function Softmax交叉熵是深度学习中非常常用的一种损失,通过交叉熵学到的特征表示会有比较大的类内的多样性。因为传统的softmax损失优化的是类内.原创 2020-09-22 11:43:56 · 384 阅读 · 0 评论 -
tensorflow 实现BahdanauAttention
class BahdanauAttention(tf.layers.Layer): def __init__(self, num_units): super(BahdanauAttention, self).__init__() self.num_units = num_units self.w1 = tf.layers.Dense(num_units) self.w2 = tf.layers.Dense(num_units) .原创 2020-07-31 16:08:59 · 536 阅读 · 0 评论 -
tensorflow 实现weight norm
讲述权重归一化在文本在conv1d上和全连接层dense上的应用,参考了simple-effective-text-matching的实现import numpy as npimport tensorflow as tfdef gelu(x): return 0.5 * x * (1 + tf.nn.tanh(x * 0.7978845608 * (1 + 0.044715 * x * x)))def get_weight(shape, gain=np.sqrt(2), w..原创 2020-07-17 13:52:45 · 1455 阅读 · 0 评论 -
【AReLU: Attention-based-Rectified-Linear-Unit】 tensorflow 实现arelu激活函数
import tensorflow as tfclass TfModel(object): def __init__(self, max_len=10, vocab_size=500, embedding_size=100, alpha=0.90, beta=2.0): self.max_len = max_len self.vocab_size = vocab_size self.embedding_size = embedding_siz.原创 2020-07-01 14:15:03 · 495 阅读 · 0 评论 -
tensorflow L2正则化实现以及差异性(tf.nn.l2_loss、tf.nn.l2_normalize、tf.contrib.layers.l2_regularizer)
import tensorflow as tffrom tensorflow.contrib import layersa = tf.Variable([3.0, 4.0])# 第一种正则化方法aa = tf.nn.l2_normalize(a) # a中每个元素除以(平方和开2次根号), 标准的l2范数, 得到是一个shape与输入a相同的tensor# 第二种正则化方法a_l2_loss = tf.nn.l2_loss(a) # a中每个元素平方和除以2, 是一个scalar# .原创 2020-06-30 11:35:46 · 1448 阅读 · 2 评论 -
tensorflow中sparse_placeholder在saved_model中保存pb模型的使用方法
# coding:utf-8# @author: liu# @file: sparse_tensor_pb.py# @time: 2020/4/3 11:00# @desc:# coding:utf-8import tensorflow as tfimport randomimport numpy as npimport osimport shutilprint...原创 2020-04-03 14:12:52 · 503 阅读 · 0 评论 -
深度学习中的线性代数
1、范数目的:在机器学习和深度学习中,限制模型复杂度、提升模型的泛华能力。范数(Norm)的定义:|x|p=(i|x|p)1pL1范数:即x的绝对值之和L2范数:即xi的平方和的二次方根(欧几里得范数)代码:Import numpy as npImport numpy.linalg as LAX = np.arange(0,1,0.1)X1 = LA.nor...原创 2018-11-04 17:09:03 · 497 阅读 · 0 评论 -
numpy常用操作
Numpy:Numerical Python的简称使用工具:numpy语言:Python3主要特点:ndarray,快速,节省空间的多维数组,提供数组化的算数运算和高级的广播功能 使用标注数学函数对整个数组的数据进行快速运算,不需要编写循环 读取或者写入磁盘上的阵列数据和操作存储器映像文件的工具 线性代数,随机数生成,以及傅里叶变换的能力 集成C、C++、Fortran代码...原创 2018-10-30 21:47:37 · 398 阅读 · 0 评论 -
NER 模型
github上的NER模型(中文)https://github.com/XierHacker/ChineseWordSegmenthttps://github.com/Determined22/zh-NER-TFhttps://github.com/baiyyang/medical-entity-recognitionhttps://github.com/hontsev/Chine...原创 2019-07-10 10:08:04 · 606 阅读 · 0 评论