深度学习TensorFlow基本操作

TensorFlow是什么

Tensorflow是一个Google开发的第二代机器学习系统,克服了第一代系统DistBelief仅能开发神经网络算法、难以配置、依赖Google内部硬件等局限性,应用更加广泛,并且提高了灵活性和可移植性,速度和扩展性也有了大幅提高。字面上理解,TensorFlow就是以张量(Tensor)在计算图(Graph)上流动(Flow)的方式的实现和执行机器学习算法的框架。具有以下特点:

  • 灵活性。TensorFlow不是一个严格的“神经网络”库。只要可以将计算表示成数据流图,就可以使用TensorFlow,比如科学计算中的偏微分求解等。(实际上其官网的介绍中对TF的定位就是基于数据流图的科学计算库,而非仅仅是机器学习库)
  • 可移植性。同一份代码几乎不经过修改既可以部署到有任意数量CPU、GPU或TPU(Tensor Processing Unit,Google专门为机器学习开发的处理器)的PC、服务器或移动设备上。
  • 自动求微分。同Theano一样,TensorFlow也支持自动求微分,用户不需要再通过反向传播求解梯度。
  • 多语言支持。TensorFlow官方支持Python、C++、Go和Java接口,用户可以在硬件配置较好的机器中用Python进行实验,在资源较紧张或需要低延迟的环境中用C++进行部署。
  • 性能。虽然TensorFlow最开始发布时仅支持单机,在性能评测上并不出色,但是凭借Google强大的开发实力,TensorFlow性能已经追上了其他框架

Google第一代分布式机器学习框架DistBelief在内部大规模使用后没有选择开源,而第二代TensorFlow于2015年11月在GitHub上开源,并在持续快速开发迭代中。TensorFlow最早由Google Brain的工程师开发,设计初衷是加速机器学习的研究,并快速地将研究原型转化为产品。Google选择开源TensorFlow的原因很简单:第一是希望借助社区的力量,大家一起完善TensorFlow。第二是回馈社区,Google希望让这个优秀的工具得到更多的应用,提高学术界和工业界使用机器学习的效率。

TensorFlow中的变量

tf中传入的变量必须要是tf格式

You add a variable to the graph by constructing an instance of the class Variable.

import tensorflow as tf

w = tf.Variable([[0.5,1.0]])  #creat a row vector
x = tf.Variable([[2.0],[1.0]])   #creat a col vector

y = tf.matmul(w,x)  # 矩阵相乘

全局变量初始化:以上三个步骤相当于有这个事,但是还没有被tensorflow认识和执行,需要初始化这一固定的操作流程。

The most common initialization pattern is to use the convenience function global_variables_initializer() to add an Op to the graph that initializes all the variables. You then run that Op after launching the graph

#全局变量初始化
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)    #在没有run之前,所有的都是空架子
    print(y.eval())

eval()

In a session, computes and returns the value of this variable.

This is not a graph construction method, it does not add ops to the graph.

This convenience method requires a session where the graph containing this variable has been launched. If no session is passed, the default session is used. See tf.compat.v1.Session for more information on launching a graph and on sessions.

变量常用操作

tensorflow很多操作跟numpy有些类似的

  • tf.zeros([3, 4], int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

  • tf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]

  • tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]

  • tf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]

  • tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

  • tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.] [-1. -1. -1.]]

  • tf.linspace(10.0, 12.0, 3, name="linspace") => [ 10.0 11.0 12.0]

  • tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]

#生成的值服从具有指定平均值和标准偏差的正态分布
norm = tf.random_normal([2, 3], mean=-1, stddev=4)   #二三之间

# 洗牌
c = tf.constant([[1, 2], [3, 4], [5, 6]])
shuff = tf.random_shuffle(c)

# 每一次执行结果都会不同
sess = tf.Session()   #创建一个会话
print (sess.run(norm))
print (sess.run(shuff))

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值