tensor

TensorFlow2.0(六):Dataset

泪湿孤枕 提交于 2019-12-01 07:42:24
/*--> */ /*--> */ /*--> */ /*--> */ /*--> */ /*--> */ 在整个机器学习过程中,除了训练模型外,应该就属数据预处理过程消耗的精力最多,数据预处理过程需要完成的任务包括数据读取、过滤、转换等等。为了将用户从繁杂的预处理操作中解放处理,更多地将精力放在算法建模上,TensorFlow中提供了data模块,这一模块以多种方式提供了数据读取、数据处理、数据保存等功能。本文重点是data模块中的Dataset对象。 1 创建 ¶ 对于创建Dataset对象,官方文档中总结为两种方式,我将这两种方式细化后总结为4中方式: (1)通过Dataset中的range()方法创建包含一定序列的Dataset对象。 range() range()方法是Dataset内部定义的一个的静态方法,可以直接通过类名调用。另外,Dataset中的range()方法与Python本身内置的range()方法接受参数形式是一致的,可以接受range(begin)、range(begin, end)、range(begin, end, step)等多种方式传参。 In [1]: import tensorflow as tf import numpy as np In [2]: dataset1 = tf.data.Dataset.range(5) type

深度学习_1_Tensorflow_1

不打扰是莪最后的温柔 提交于 2019-12-01 04:44:28
# 深度学习 # 图像识别,自然语言处理 # 机器学习 深度学习 # 分类:神经网络(简单) 神经网络(深度) # 回归 图像:卷积神经网络 # 自然语言处理:循环神经网络 # cpu:运行操作系统,处理业务,计算能力不是特别突出 # gpu:专门为计算设计的 import tensorflow as tf a = tf.constant(5.0) b = tf.constant(6.0) sum1 = tf.add(a,b) # 在session外边打印时只能查看对象 # 程序的图 a,b,sum1也有graph graph = tf.get_default_graph() print(a.graph) print(graph) # session()运行默认的图,当运行的元素不是默认图的时候,会报错 with tf.Session() as sess: print(sess.run(sum1)) # 输出值 # 创建新的图 g = tf.Graph() with g.as_default(): c = tf.constant(11.0) print(c.graph) # 与上边的图不同 # 图程序的空间,变量,线程等资源都在图中 # 会话运行图的程序, # tf.Session(graph=c) 指定图运行, 里边run的时候要注意 # session.run的作用:启动整个图

How can you re-use a variable scope in tensorflow without a new scope being created by default?

半世苍凉 提交于 2019-12-01 04:13:29
I have created a variable scope in one part of my graph, and later in another part of the graph I want to add OPs to an existing scope. That equates to this distilled example: import tensorflow as tf with tf.variable_scope('myscope'): tf.Variable(1.0, name='var1') with tf.variable_scope('myscope', reuse=True): tf.Variable(2.0, name='var2') print([n.name for n in tf.get_default_graph().as_graph_def().node]) Which yields: ['myscope/var1/initial_value', 'myscope/var1', 'myscope/var1/Assign', 'myscope/var1/read', 'myscope_1/var2/initial_value', 'myscope_1/var2', 'myscope_1/var2/Assign', 'myscope_1

deep_learning_Function_tensorflow_random_normal_initializer

。_饼干妹妹 提交于 2019-11-30 22:59:08
函数原型:tf.random_normal_initializer(mean=0.0, stddev=1.0, seed=None, dtype=tf.float32) 返回一个生成具有正态分布的张量的初始化器。 参数: mean:python标量或标量tensor,产生的随机值的平均值。 stddev:一个python标量或一个标量tensor,标准偏差的随机值生成。 seed:一个Python整数。 用于创建随机seed有关行为,请参阅官网API:set_random_seed。 dtype:数据类型, 只支持浮点类型。 函数返回:产生具有正态分布的张量的初始化器。 转自: https://blog.csdn.net/eml_jw/article/details/72880708 来源: https://www.cnblogs.com/0405mxh/p/11643908.html

Pytorch入门教程

淺唱寂寞╮ 提交于 2019-11-30 19:44:59
  记得刚开始学TensorFlow的时候,那给我折磨的呀,我一直在想这个TensorFlow官方为什么搭建个网络还要画什么静态图呢,把简单的事情弄得麻烦死了,直到这几天我开始接触Pytorch,发现Pytorch是就是不用搭建静态图的Tensorflow版本,就想在用numpy一样,并且封装了很多深度学习高级API,numpy数据和Tensor数据相互转换不用搭建会话了,只需要一个转换函数,搭建起了numpy和TensorFlow爱的桥梁。   Pytorch自17年推出以来,一度有赶超TensorFlow的趋势,是因为Pytorch采用动态图机制,替代Numpy使用GPU的功能,搭建网络灵活。 Pytorch和TensorFlow的区别: TensorFlow是基于静态计算图的,静态计算图是先定义后运行,一次定义多次运行(Tensorflow 2.0也开始使用动态计算图) PyTorch是基于动态图的,是在运行的过程中被定义的,在运行的时候构建,可以多次构建多次运行 张量   Pytorch中的Tensor和ndarray类似,区别在于ndarray不能再GPU上加速,而Tensor可以使用GPU加速 构建一个未初始化3*3的矩阵 import torchx = torch.empty(3,3) # tensor([[1.0469e-38, 5.9694e-39, 8

Tensorflow, how to multiply a 2D tensor (matrix) by corresponding elements in a 1D vector

霸气de小男生 提交于 2019-11-30 16:16:45
I have a 2D matrix M of shape [batch x dim] , I have a vector V of shape [batch] . How can I multiply each of the columns in the matrix by the corresponding element in the V? That is: I know an inefficient numpy implementation would look like this: import numpy as np M = np.random.uniform(size=(4, 10)) V = np.random.randint(4) def tst(M, V): rows = [] for i in range(len(M)): col = [] for j in range(len(M[i])): col.append(M[i][j] * V[i]) rows.append(col) return np.array(rows) In tensorflow, given two tensors, what is the most efficient way to achieve this? import tensorflow as tf sess = tf

TensorFlow2.0教程30:使用tf.function和AutoGraph提高代码性能

允我心安 提交于 2019-11-30 13:19:42
  在TensorFlow 2.0中,默认情况下启用了急切执行。 对于用户而言直观且灵活(运行一次性操作更容易,更快),但这可能会牺牲性能和可部署性。   要获得最佳性能并使模型可在任何地方部署,可以优先使用tf.function从程序中构建图。 因为有AutoGraph,可以使用tf.function构建高效性能的Python代码,但仍有一些陷阱需要警惕。   今天我们就来介绍一下tensorflow2.0中的TF fuction和AutoGraph。   下面的辅助程序代码,用于演示可能遇到的各种错误。   import contextlib   # 构建包含上下文管理器的函数,使其可以在with中使用   @contextlib.contextmanager   def assert_raises(error_class):   try:   yield   except error_class as e:   print('Caught expected exception \n {}: {}'.format(error_class, e))   except Exception as e:   print('Got unexpected exception \n {}: {}'.format(type(e), e))   else:   raise Exception(

TensorFlow2.0教程29:使用低级api训练(非tf.keras)

对着背影说爱祢 提交于 2019-11-30 13:18:38
  一、Variables   TensorFlow的张量是不可变的无状态对象。当我们有要改变的张量时,可以使用python的特性,把计算得到的值赋给这个python变量。   x = tf.ones([6,6])   x = x + 3 # x+3后得到了一个新的张量,并把这个张量赋给x   print(x)   tf.Tensor(   [[4. 4. 4. 4. 4. 4.]   [4. 4. 4. 4. 4. 4.]   [4. 4. 4. 4. 4. 4.]   [4. 4. 4. 4. 4. 4.]   [4. 4. 4. 4. 4. 4.]   [4. 4. 4. 4. 4. 4.]], shape=(6, 6), dtype=float32)   然而机器学习中间需要变化的状态(每个参数朝损失变小的方向改变,所以TensorFlow也要内置有状态的操作,这就是Variables,它可以表示模型中的参数,而且方便高效。   Variables是一个存在值的对象,当其被使用是,它被隐式地被从存储中读取,而当有诸如tf.assign_sub, tf.scatter_update这样的操作时,得到的新值会储存到原对象中。   v = tf.Variable(2)   v.assign(6)   print(v)   v.assign_add(tf.square(3))  

Efficient element-wise multiplication of a matrix and a vector in TensorFlow

时光毁灭记忆、已成空白 提交于 2019-11-30 11:48:59
问题 What would be the most efficient way to multiply (element-wise) a 2D tensor (matrix): x11 x12 .. x1N ... xM1 xM2 .. xMN by a vertical vector: w1 ... wN to obtain a new matrix: x11*w1 x12*w2 ... x1N*wN ... xM1*w1 xM2*w2 ... xMN*wN To give some context, we have M data samples in a batch that can be processed in parallel, and each N -element sample must be multiplied by weights w stored in a variable to eventually pick the largest Xij*wj for each row i . 回答1: The simplest code to do this relies

Debugging keras tensor values

99封情书 提交于 2019-11-30 11:15:26
I am implementing own keras loss function. How can I access tensor values? What I've tried def loss_fn(y_true, y_pred): print y_true It prints Tensor("target:0", shape=(?, ?), dtype=float32) Is there any keras function to access y_true values? Keras' backend has print_tensor which enables you to do this. You can use it this way: import keras.backend as K def loss_fn(y_true, y_pred): y_true = K.print_tensor(y_true, message='y_true = ') y_pred = K.print_tensor(y_pred, message='y_pred = ') ... The function returns an identical tensor. When that tensor is evaluated, it will print its content,