tensorflow2.0
梅文化_2019
这个作者很懒,什么都没留下…
展开
-
TensorFlow2.0:张量的合并与分割
**一 tf.concat( ) 函数–合并**In [2]: a = tf.ones([4,35,8]) In [3]: b = tf.ones([2,35,8]) In [4]: c...原创 2019-08-13 16:40:01 · 2611 阅读 · 0 评论 -
TensorFlow2.0:维度变换
**一 reshape函数 重排列**In [1]: import tensorflow as tf In [2]: a = tf.random.normal([4,28,28,3],mean=1,stddev=1) In [3]: a.shap...原创 2019-08-13 09:29:46 · 1038 阅读 · 0 评论 -
TensorFlow2.0:张量的数学运算
(1)+ - * /(2)** pow square(3) sqrt(4) // %(5) exp log(6)@ matmul(7) linear layerelement-wise: + - * /matrix-wise: @ matmuldim-wise: reduce_mean/max/min/sum**一 + - * / % //运算**+:对应矩阵元素相加...原创 2019-08-13 10:15:15 · 4780 阅读 · 0 评论 -
TensorFlow2.0:梯度计算
**一 2阶梯度的计算**将需要计算梯度的变量设置为tf.Variable( ),并将计算函数置于with tf.GradientTape ( ) as tape内.import tensorflow as tfx = tf.Variable(tf.constant([2.]))w = tf.Variable(tf.constant([2.]))b = tf.Variable(t...原创 2019-08-22 10:46:00 · 4572 阅读 · 0 评论 -
TensorFlow2.0:单层感知机梯度计算
**一 单层单输出感知机梯度计算**单层感知机指的是输入有多个节点,输出只有一个节点.其实质为二分类,即将样本的多个特征值作为输入,输出为二分类.假设输入有5个样本,每个样本有3个特征参数,样本的标签值为[0,1,0,1,0].那么x的维度为[5,3],y的维度为[5].其中感知机参数设置为:w的维度为[3,1],b的维度为[3].由于为单输出,因此输出层可以使用sigmoid激...原创 2019-08-22 11:13:30 · 724 阅读 · 0 评论 -
TensorFlow2.0:tensorboard使用
**一、tensorboard的安装**使用命令pip install tensorboard**二、使用方法**2.1、启动listener在项目所在的目录创建文件夹logs用于存放监听数据使用命令行中定位到项目所在的文件夹,并使用命令tensorboard --logdir logs启用listener使用浏览器输入localhost:6006打开端口2.2、使用代码...原创 2019-09-10 14:59:04 · 4362 阅读 · 0 评论 -
TensorFlow2.0: keras.metrics的使用
keras.metrics中有两个api函数可以简化准确率acc和损失值loss的计算。其分别是metrics.Accuracy( )和metrics.Mean( )。一、建立测量尺#建立测量尺acc_meter = metrics.Accuracy()loss_meter = metrics.Mean()二、更新数据loss_meter.update_state(loss)acc...原创 2019-09-10 15:09:07 · 7867 阅读 · 2 评论 -
TensorFlow2.0:keras.compile与fit的使用
import tensorflow as tffrom tensorflow import kerasfrom tensorflow.keras import layers,datasets,optimizers,Sequential,metricsdef preprocess(x,y): x = tf.cast(x,dtype=tf.float32)/255. x = t...原创 2019-09-10 16:17:50 · 2944 阅读 · 0 评论 -
TensorFlow2.0:自定义层与自定义网络
自定义层函数需要继承layers.Layer,自定义网络需要继承keras.Model。其内部需要定义两个函数:1、__init__初始化函数,内部需要定义构造形式;2、call函数,内部需要定义计算形式及返回值。#self def layerclass MyDense(layers.Layer):#inherit layers.Layer def __init__(self,i...原创 2019-09-11 08:45:20 · 3238 阅读 · 0 评论 -
TensorFlow2.0:索引和切片(2)
**一 tf.gather( ) 函数**–tf.gather(params, indices, validate_indices=None, name=None, axis=0)params:待切片的参数indices:取出数据所在的位置axis:指定切片数据所在的维度tf.gather(a,axis=0,indices=[2,3]): 对参数a进行切片, 对维度0进行操作,将维度...原创 2019-08-13 08:36:15 · 1041 阅读 · 0 评论 -
TensorFlow2.0:索引和切片(1)
**一 基本的索引方式**给定每个维度的索引,直接进行索引In [1]: import tensorflow as tf In [2]: import numpy as np ...原创 2019-08-12 16:57:50 · 261 阅读 · 0 评论 -
TensorFlow2.0:创建tensor
**一 从numpy list中得到tensor**—tf.convert_to_tensor()In [1]: import tensorflow as tf In [2]: import numpy as np ...原创 2019-08-12 16:58:23 · 234 阅读 · 0 评论 -
TensorFlow2.0:数据统计
**一 tf.norm( )函数–求范数**In [30]: a = tf.ones([2,2]) ...原创 2019-08-13 18:24:43 · 365 阅读 · 0 评论 -
TensorFlow2.0:张量排序
一 sort argsort排序tf.sort( )按照升序或者降序对张量进行排序tf.argsort( )按照升序或者降序对张量进行排序,但返回的是索引In [4]: a = tf.range(5) In [5]: a ...原创 2019-08-19 09:36:54 · 2131 阅读 · 0 评论 -
TensorFlow2.0:数据的填充与复制
**一 tf.pad( )填充函数**tf.pad(tensor, paddings, mode="CONSTANT", name=None, constant_values=0): paddings参数的设置方法:假设输入tensor有两个维度,则paddings参数设置为paddings = [[a,b],[c,d]]a,b表示为axis=0维度上最前面填充a行,最后面填充b行,...原创 2019-08-19 10:16:59 · 1498 阅读 · 0 评论 -
TensorFlow2.0:张量限幅
**一 tf.clip_by_value( )函数**tf.maximum(a,b)返回a和b之间的最大值tf.minimum(a,b)返回a和b之间的最小值tf.clip_by_value(t, clip_value_min, clip_value_max, name=None)限制tensor t中张量的最小值为clip_value_min,张量的最大值为clip_value_max...原创 2019-08-19 10:51:21 · 326 阅读 · 0 评论 -
TensorFlow2.0:高阶操作
**一 tf.where( )函数**tf.where(tensor)当只有一个输入时,输入为布尔型,其返回的是值为True的位置.In [3]: a = tf.random.normal([3,3])#生成3行3列的随机数组 ...原创 2019-08-19 12:05:12 · 183 阅读 · 0 评论 -
TensorFlow2.0:常用数据范围压缩函数
**一 tf.nn.relu( )函数**tf.nn.relu( )激活函数可以将小于0的数据变成0,大于0的数据保持不变.In [2]: a = tf.constant([-1,-2,0,1,2]) ...原创 2019-08-20 10:57:56 · 919 阅读 · 0 评论 -
TensorFlow2.0:误差计算
**一 MSE(Mean Square Error)均方误差**In [2]: out = tf.random.normal([5,4]) In [3]: y = tf.constant([1,2,3,0,2]) ...原创 2019-08-20 12:29:21 · 937 阅读 · 0 评论 -
Tensorflow2.0数据类型
**一 Tensorflow的类型**--int, float, double--bool--tringIn [1]: import tensorflow as tf In [2]: import numpy as np ...原创 2019-08-12 10:06:44 · 1430 阅读 · 0 评论 -
TensorFlow2.0:模型的保存与加载
**一、权重参数的保存与加载**network.save_weights('weights.ckpt')network.load_weights('weights.ckpt')权重参数的保存与加载可以针对任何模型,包括自定义的。但是在加载权重参数时,其模型的结构需要与原来的完全一致。import tensorflow as tffrom tensorflow import ker...原创 2019-09-11 09:33:28 · 2842 阅读 · 0 评论