tf
github_元宝
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
tfrecord和tf.dataset
老版本:filename_queue = tf.train.string_input_producer([tfrecords_filename],num_epochs=None) #读入流中reader = tf.TFRecordReader()_, serialized_example = reader.read(filename_queue)features = tf.parse_single_example(serialized_example,原创 2021-04-16 21:06:42 · 429 阅读 · 0 评论 -
afm_layer
# coding=utf-8import itertoolsimport tensorflow as tffrom tensorflow.keras import Modelfrom tensorflow.keras.regularizers import l2from tensorflow.keras.layers import Embedding, Dense, Dropout, Inputfrom tensorflow.keras.layers import Layerclass .原创 2021-09-11 12:39:39 · 144 阅读 · 0 评论 -
CIN_layer
#coding=utf-8import tensorflow as tffrom tensorflow.python.keras.layers import Concatenate,Conv1D,Reshape## 只计算其中一层交叉的结果def compressed_interaction_net(x0, xl, D, n_filters): """ @param x0: 原始输入 @param xl: 第l层的输入 @param D: embedding d.原创 2021-09-11 11:03:45 · 196 阅读 · 0 评论 -
deep_cross_layer
#coding=utf-8import tensorflow as tffrom tensorflow.keras.layers import Dense, ReLU, Layerclass DeepCrossLater(Layer): def __init__(self,dim_stack, name=None): """ :param hidden_unit: A list. Neural network hidden units. :pa原创 2021-09-10 18:49:49 · 147 阅读 · 0 评论 -
tf-矩阵相关知识
1、向量 or 矩阵 # 向量(3,) v1 = tf.constant([ 1, 2, 3]) print(v1.shape) ## 矩阵(3, 1) v2 = tf.constant([ [1], [2], [3]]) print(v2.shape)2、最后一维升维 ## 矩阵最后一维增加维度 q = tf.constant([ [ 1, 2, 3],原创 2021-09-06 17:23:28 · 509 阅读 · 0 评论 -
tf keras 训练过程简述
持续更新。。。。。。。原创 2021-08-27 11:31:29 · 212 阅读 · 0 评论 -
tf.keras.layer介绍
通过继承tf.keras.layer实现自定义层的最佳方法是继承tf.keras.Layer类并实现:init ,在其中进行所有与输入无关的变量或常量的初始化build,在其中知道输入张量的形状,并可以进行其余的初始化call,在这里进行前向传播逻辑的计算的定义请注意,不必等到build被调用才创建变量,也可以在__init__中直接定义。但是,在build中创建变量的好处在于,可以根据build的参数 input shape来自动调整变量的shape。而在__init__中创建变量意味着需要显原创 2021-08-25 13:52:59 · 655 阅读 · 0 评论 -
tf2.x函数签名
一般说法:input_signature的好处:1.可以限定函数的输入类型,以防止调用函数时调错,2.一个函数有了input_signature之后,在tensorflow里边才可以保存成savedmodel。在保存成savedmodel的过程中,需要使用get_concrete_function函数把一个tf.function标注的普通的python函数变成带有图定义的函数。使用经验:当一个函数被@tf.function 比较后,可以使用get_concrete_function 然后传入原函数原创 2021-08-25 11:28:15 · 550 阅读 · 0 评论 -
tf.clip_by_global_norm函数解析
#coding=utf-8import tensorflow as tfimport numpy as npdef test_clip_by_global_norm(): x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] r, use_norm = tf.clip_by_global_norm(x, 10) print(r,use_norm) #--------use_norm---------- x_l2 = np.ar原创 2021-08-24 14:20:08 · 380 阅读 · 0 评论 -
tf-estimator学习笔记
持续更新。。。estimator 对标的是tf.keras.Model 模型级别的抽象 ,我觉得这个和module应该是一个级别的,因为每个Estimater的核心是模型函数,一种为训练、预估和预测构建计算图的方法,module实例化的对象也是包含训练、预估和导出...原创 2021-08-24 12:02:16 · 176 阅读 · 0 评论 -
mmoe_layer
#!usr/bin/env python# coding=utf-8import numpy as npimport pandas as pdimport datetimeimport itertoolsimport tensorflow as tffrom tensorflow.keras.layers import *import tensorflow.keras.backend as Kfrom tensorflow.keras import layersclass MMoEL原创 2021-08-06 13:30:04 · 209 阅读 · 0 评论 -
What is a nested structure in TensorFlow?
指的是tuple或者dictProviding answer here from the comment section for the benefit of the community.Nested Structure in TensorFlow generally refers to a tuple or a dict containing tensor values, or other nested structures.The typical case is a dataset where e原创 2021-08-05 11:26:25 · 426 阅读 · 0 评论 -
tf.add_weight 和 tf.Variable使用例子
class Linear(keras.layers.Layer): def __init__(self, units=32, input_dim=32): super(Linear, self).__init__() w_init = tf.random_normal_initializer() self.w = tf.Variable( initial_value=w_init(shape=(input_dim, units)原创 2021-07-21 18:28:45 · 5785 阅读 · 0 评论 -
tf.compat.v1.sparse_to_dense
import tensorflow as tfmulti_one_hot = tf.sparse.SparseTensor(indices=[[0, 0], [0, 1], [2, 0], [4, 0], [4, 1], [4, 2], [4, 3]],原创 2021-05-23 15:56:31 · 338 阅读 · 0 评论 -
tf.nn.embedding_lookup
tf.nn.embedding_lookup( params, ids, partition_strategy='mod', name=None, validate_indices=True, max_norm=None)作用:在params表中按照ids查询向量。params是一个词表,大小为NH,N为词的数量,H为词向量维度的大小。图中的params大小为310,表示有3个词,每个词有10维。一句话中有四个词,对应的id为0,2,0,1,按照ids在原创 2021-05-23 15:21:42 · 211 阅读 · 0 评论 -
scale_dot_product_attention and multi_head_attention tf2.x
import matplotlib as mplimport numpy as npimport sklearnimport pandas as pdimport osimport sysimport timeimport tensorflow as tffrom tensorflow import kerasprint(tf.__version__)print(sys.version_info)for module in mpl, np, pd, sklearn, tf, ker原创 2021-05-11 11:40:20 · 586 阅读 · 0 评论 -
测试数据生成
import matplotlib as mplimport numpy as npimport sklearnimport pandas as pdimport osimport sysimport timeimport tensorflow as tffrom tensorflow import kerasprint(tf.__version__)print(sys.version_info)for module in mpl, np, pd, sklearn, tf, ker原创 2021-05-08 15:59:49 · 223 阅读 · 0 评论 -
TF乘法之multiply、matmul、*
import tensorflow as tfw = tf.Variable([[0.4], [1.2]], dtype=tf.float32) # w.shape: [2, 1]x = tf.Variable([range(1,6), range(5,10)], dtype=tf.float32) # x.shape: [2, 5]y = w * x # 等同于 y = tf.multiply(w, x) y.shape: [2, 5]sess = tf.Session()ini原创 2021-05-08 11:31:31 · 1525 阅读 · 1 评论 -
tensorflow交叉熵损失函数-cross_entropy_with_logits 在库函数中比较
tf.nn.softmax(logits,axis=None,name=None,dim=None)作用:softmax函数的作用就是归一化。输入: 全连接层(往往是模型的最后一层)的值,一般代码中叫做logits输出: 归一化的值,含义是属于该位置的概率,一般代码叫做probs。例如输入[0.4,0.1,0.2,0.3],那么这个样本最可能属于第0个位置,也就是第0类。这是由于logits的维度大小就设定的是任务的类别,所以第0个位置就代表第0类。softmax函数的输出不改变维度的大小。原创 2021-05-08 10:23:11 · 679 阅读 · 0 评论 -
tf.where()用法
where(condition, x=None, y=None, name=None)的用法condition, x, y 相同维度,condition是bool型值,True/False返回值是对应元素,condition中元素为True的元素替换为x中的元素,为False的元素替换为y中对应元素x只负责对应替换True的元素,y只负责对应替换False的元素,x,y各有分工由于是替换,返回值的维度,和condition,x , y都是相等的。看个例子:import tensorflow a原创 2021-05-07 19:46:46 · 288 阅读 · 0 评论 -
tf.ones_like()
tf.ones_like( input, dtype=None, name=None)import tensorflow as tfimport numpy as np# 生成一个tensor,内部数据随机产生a = tf.convert_to_tensor(np.random.random([2, 4, 5]), dtype=tf.float32)# ones_likeb = tf.ones_like(a, dtype=tf.float32, name='ones_like'原创 2021-05-07 18:07:51 · 207 阅读 · 0 评论 -
tf.cast()和 tf.tile()
tf.cast()数据类型转换tensorflow 中tile函数用法讲解原创 2021-05-07 14:06:23 · 150 阅读 · 0 评论 -
tf.SparseTensor和tf.sparse_tensor_to_dense
import tensorflow as tfimport numpy as npindices = np.array([[0, 0], [1, 1], [2, 2], [3, 4]], dtype=np.int32)values = np.array([1, 2, 3, 4], dtype=np.int32)shape = np.array([5, 5], dtype=np.int32)x = tf.SparseTensor(values=values,indices=indices,dens原创 2021-05-07 12:12:32 · 1344 阅读 · 1 评论 -
tensoflow笔记:tf.name_scope和tf.variable_scope
这两种作用域,对于使用tf.Variable()方式创建的变量,具有相同的效果,都会在变量名称前面,加上域名称。对于通过tf.get_variable()方式创建的变量,只有variable_scope名称会加到变量名称前面,而name_scope不会作为前缀。例如 print(v1.name) # var1:0with tf.name_scope("my_name_scope"): v1 = tf.get_variable("var1", [1], dtype=tf.float32)原创 2021-05-06 14:21:56 · 167 阅读 · 0 评论 -
tensorflow的模型保存加载
Tensorflow的保存分为 四种 :checkpoint模式;saved_model模式(包含pb文件和variables);纯pb模式;(只有一个pb文件)4.keras的 h5 模式原创 2021-04-20 19:05:40 · 296 阅读 · 1 评论 -
get_or_create_global_step
import tensorflow.compat.v1 as tftf.disable_v2_behavior()# tf.enable_eager_execution()import numpy as npx = tf.placeholder(tf.float32, shape=[None, 1], name='x')y = tf.placeholder(tf.float32, shape=[None, 1], name='y')w = tf.Variable(tf.constant(0.0原创 2021-04-19 17:19:58 · 1045 阅读 · 1 评论 -
tf SavedModel 文件内容
ll model_ckpt/1.0ctr/incre_model/vents.out.tfevents.1618819104.hostnamevents.out.tfevents.1618819310.hostnamemodel_ckpt/1.0ctr/incre_model/checkpointevalevents.out.tfevents.1618818779.hostnamegraph.pbtxtmodel.ckpt-0.data-00000-of-00001model.ckpt-.原创 2021-04-19 16:47:37 · 164 阅读 · 0 评论 -
tf2.x项目7学习记录
1、(None, max_length,embedding_size )经过lstm层后变成(None, 64)这个地方需要注意!原创 2021-04-19 14:11:20 · 148 阅读 · 0 评论 -
tf.keras.losses.SparseCategoricalCrossentropy()与CategoricalCrossentropy()简单说明
tf.keras.losses.SparseCategoricalCrossentropy()与CategoricalCrossentropy()的区别:如果目标是one-hot 编码,比如二分类【0,1】【1,0】,损失函数用 categorical_crossentropy。如果目标是数字编码 ,比如二分类0,1,损失函数用 sparse_categorical_crossentropy。# SparseCategoricalCrossentropyscce = tf.keras.losses.原创 2021-04-18 22:50:35 · 3391 阅读 · 0 评论 -
tfrecord + keras + estimator
import tensorflow.compat.v1 as tftf.disable_v2_behavior()from keras import backend as K_BATCH_SIZE =10model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(256, activation='relu'), tf.keras.la原创 2021-04-17 11:37:51 · 211 阅读 · 0 评论 -
__iter__() is only supported inside of tf.function or when eager execution is enabled
import tensorflow as tftf.enable_eager_execution()orimport tensorflow.compat.v1 as tftf.disable_v2_behavior()tf.enable_eager_execution()原创 2021-04-16 20:50:50 · 762 阅读 · 0 评论 -
Input to DecodeRaw is not a multiple of 8, the size of double
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to DecodeRaw has length 1 that is not a multiple of 4, the size of float[[node DecodeRaw_1 (defined at /GraphRelated/tf_repos/keras_estimator/aaaa.py:63) ]]解决方案一:label = tf.decode_raw(f原创 2021-04-16 20:31:23 · 454 阅读 · 0 评论 -
Tensorflow报错:Assign requires shapes of both tensors to match. lhs shape= [16563] rhs shape= [16562]
运行Tensorflow项目时报错,解决办法:第一、确定修改后的网络输入shape和你输入的图片shape是符合的第二、删掉原来产生的logs第二、清空原来产生的checkpoint原创 2021-04-13 18:45:29 · 539 阅读 · 0 评论 -
tensorflow学习笔记
1、tf.data 是 batch shuffle后的 (input, target)格式数据dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)for input_example, target_example in dataset.take(1): print ('Input data: ', repr(''.join(idx2char[input_example.numpy()])))原创 2021-04-09 14:29:49 · 148 阅读 · 0 评论 -
tf1.x产出模型小例子
import tensorflow.compat.v1 as tftf.disable_v2_behavior()a = tf.constant([10.0, 20.0, 40.0], name='a')b = tf.Variable(tf.random_uniform([3]), name='b') # 从均匀分布中输出随机值,[3]代表张量尺寸output = tf.add_n([a, b], name='add') # Add all input tensors element wise原创 2021-04-07 14:33:27 · 173 阅读 · 0 评论 -
tensorboard查看tf saved_model
# coding: utf-8from __future__ import print_functionimport tensorflow.compat.v1 as tftf.disable_v2_behavior()sess=tf.Session()tf.train.import_meta_graph("../model/shakespeare/model-200.meta")tf.summary.FileWriter("log",sess.graph)sess.close()原创 2021-04-07 14:32:05 · 314 阅读 · 0 评论 -
tfrecord读取过程简介
加载TFRecord文件通过parse_fn方法对每条样本机型解析重复N epochsbatchdef parse_fn(example_proto): features = {"state": tf.FixedLenFeature((), tf.string), "action": tf.FixedLenFeature((), tf.int64), "reward": tf.FixedLenFeature((), tf.i.原创 2021-02-13 17:58:34 · 833 阅读 · 1 评论 -
tensorflow 基础知识点(一)
归一化vocabulary_size = 10embedding_size = 4embeddings = tf.truncated_normal([vocabulary_size, embedding_size], stddev=1.0 / math.sqrt(embedding_size))with tf.Session() as session: # print(session.run(embeddings)) norm原创 2020-06-10 14:21:59 · 318 阅读 · 0 评论 -
TFRecord 中 FixedLenFeature、VarLenFeature、FixedLenSequenceFeature 说明
为了解析每个输入样本每一列数据,需要定义一个解析字典。tensorflow提供了三种方式:FixedLenFeature、VarLenFeature、FixedLenSequenceFeature,分别解析定长特征、变长特征、定长序列特征。FixedLenFeature() 函数有三个参数:(1)shape:输入数据的shape。(2)dtype:输入的数据类型。(3)default_value:如果示例缺少此功能,则使用该值。它必须与dtype和指定shape兼容。注意点:tf.FixedLe原创 2021-02-13 17:46:18 · 4108 阅读 · 1 评论 -
tfRecord TypeError: only integer scalar arrays can be converted to a scalar index 错误解决办法
tfRecord TypeError: only integer scalar arrays can be converted to a scalar index 错误解决办法 tfRecord-features-feature 生成 样本时,对具体样本定义到tf feature过程中,出现TypeError: only integer scalar arrays can be converted to a scalar index错误,原因是该记录为类型不匹配 需要从integer scalar arr原创 2021-02-13 17:06:16 · 1579 阅读 · 1 评论
分享