在学习深度学习等知识之前,首先得了解著名的框架TensorFlow里面的一些基础知识,下面首先看一下这个框架的一些基本用法。

import tensorflow as tf
a = 3 # Python中普通的变量创建方式 # Create a variable.
w = tf.Variable([[0.5, 1.0]]) # tensorflow创建变量方式
x = tf.Variable([[2.0], [1.0]]) y = tf.matmul(w, x) # 矩阵内积 变量的操作
print(y) # tensor 里面没有具体的值 # variables have to be explicitly initialized before you can run Ops
# 初始化全局变量 w,x,y
init_op = tf.global_variables_initializer()
# 计算图
with tf.Session() as sess:
sess.run(init_op)
print(y.eval()) # 通过这种方式打印具体的值

得到的结果是:

Tensor("MatMul_2:0", shape=(1, 1), dtype=float32)
[[2.]]

通过上面可以看出,只是简单的一个矩阵的乘法,我们就写了这么多的代码,看起来比较麻烦,但是没有办法,要用这个框架就必须按照它的用法去用,但是在用这个框架来写深度学习里面的代码,那就不是很复杂了。上面的代码展示了TensorFlow框架的基本用法,导入库、变量定义、初始化变量、Session操作、然后才能进行具体的操作。

下面学习一下TensorFlow框架中一些函数的用法,可以和numpy库中的一些函数对比着学习。

from numpy import int32
# float32 在TensorFlow最好使用这种格式 # 创建都是0的矩阵
tf.zeros([3, 4], int32) # ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
# 矩阵格式相似
tf.zeros_like(tensor) # ==> [[0, 0, 0], [0, 0, 0]] # 矩阵元素都为1
tf.ones([2, 3], int32) # ==> [[1, 1, 1], [1, 1, 1]]
tf.ones_like(tensor) # ==> [[1, 1, 1], [1, 1, 1]] # Constant 1-D Tensor populated with value list.
# 创建一个常量,必须使用这种方式
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) # => [1 2 3 4 5 6 7] # Constant 2-D tensor populated with scalar value -1.
# 创建二维矩阵常量
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] # 'start' is 3
# 'limit' is 18
# 'delta' is 3
# tf.range(start, limit, delta)
tf.range(3, 18, 3)# ==> [3, 6, 9, 12, 15]

可以看出TensorFlow里面一些函数和numpy里面的用法差不多,下面看看TensorFlow中随机数的一些用法。

# 高斯分布的均值矩阵  指定均值和方差
norm = tf.random_normal([2, 3], mean=-1, stddev=4) # Shuffle the first dimension of a tensor
c = tf.constant([[1, 2], [3, 4], [5, 6]]) # shuffle操作
shuff = tf.random_shuffle(c) # Each time we run these ops, different results are generated
# 要执行这些操作的方法。推荐使用上面With结构
sess = tf.Session()
print(sess.run(norm))
print(sess.run(shuff))

运行得到的结果是

[[-2.4004993   5.356218    0.51297414]
[-4.353016 2.234075 -4.2948236 ]]
[[1 2]
[3 4]
[5 6]]

下面来看一个使用TensorFlow完成打印0到4之间的数字这样的一个小栗子,在原生Python中很简单,主要看看在TensorFlow中的用法。

# 打印0到4之间的的值
state = tf.Variable(0) # 初始化常量0
new_value = tf.add(state, tf.constant(1)) # 执行加1操作
update = tf.assign(state, new_value) # 将new_value赋给state # Session计算块
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(state))
for _ in range(3):
sess.run(update)
print(sess.run(state))

得到的结果是

0
1
2
3

下面再来看看在创建变量时将numpy里面的格式转换为tensor格式,但是并不推荐使用这种方法

import numpy as np
a = np.zeros((3,3)) # 将numpy里面的格式转换为tensor格式,并不推荐使用这种方法
# 推荐使用上面创建变量的方法
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:
print(sess.run(ta))

得到的结果是

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

下面再来看看TensorFlow中占位符的用法

# 创建占位符,用的时候再具体赋值。
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2) # 矩阵元素相乘
with tf.Session() as sess:
print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

得到的结果是

[array([14.], dtype=float32)]

总结,这篇博文包含了TensorFlow框架中一些常见的用法,但是肯定很多细节没有写全,只是写了一些大概的用法留作以后查看。

  

TensorFlow基本计算单元与基本操作的更多相关文章

  1. TensorFlow 基本变量定义,基本操作,矩阵基本操作

    使用 TensorFlow 进行基本操作的实例,这个实例主要是使用 TensorFlow 进行了加法运算. 包括使用 constant 常量进行加法运算和使用 placeholder 进行变量加法运算 ...

  2. TensorFlow基本计算单元——变量

    # -*- coding: utf-8 -*- import tensorflow as tf a = 3 # 创建变量 w = tf.Variable([[0.5, 1.0]]) #行向量 x = ...

  3. tensorflow学习笔记三----------基本操作

    tensorflow中的一些操作和numpy中的很像,下面列出几个比较常见的操作 import tensorflow as tf #定义三行四列的零矩阵 tf.zeros([3,4]) #定义两行三列 ...

  4. TensorFlow简易学习[2]:实现线性回归

    上篇介绍了TensorFlow基本概念和基本操作,本文将利用TensorFlow举例实现线性回归模型过程. 线性回归算法 线性回归算法是机器学习中典型监督学习算法,不同于分类算法,线性回归的输出是整个 ...

  5. TensorFlow从入门到实战资料汇总 2017-02-02 06:08 | 数据派

    TensorFlow从入门到实战资料汇总 2017-02-02 06:08 | 数据派 来源:DataCastle数据城堡 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学 ...

  6. 【干货】史上最全的Tensorflow学习资源汇总

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 作者:AI小昕 在之前的Tensorflow系列文章中,我们教大家 ...

  7. TensorFlow、numpy、matplotlib、基本操作

    一.常量的定义 import tensorflow as tf #类比 语法 api 原理 #基础数据类型 运算符 流程 字典 数组 data1 = tf.constant(2,dtype=tf.in ...

  8. Tensorflow从入门到精通之——Tensorflow基本操作

    前边的章节介绍了什么是Tensorflow,本节将带大家真正走进Tensorflow的世界,学习Tensorflow一些基本的操作及使用方法.同时也欢迎大家关注我们的网站和系列教程:http://ww ...

  9. Tensorflow基本操作理解

    1. TensorsTensorFlow的数据中央控制单元是tensor(张量),一个tensor由一系列的原始值组成,这些值被形成一个任意维数的数组.一个tensor的列就是它的维度. 2. The ...

随机推荐

  1. Android定时锁屏功能实现(AlarmManager定时部分)

    菜鸟入坑记——第一篇 关键字:AlarmManager 一.AlarmManager简介: 参考网址:https://www.jianshu.com/p/8a2ce9d02640        参考网 ...

  2. log4j配置相对路径

    整理自网上: 一般在我们开发项目过程中,log4j日志输出路径固定到某个文件夹,这样如果我换一个环境,日志路径又需要重新修改,比较不方便, 1.log4j的FileAppender本身就有这样的机制, ...

  3. HDU1814Peaceful Commission求2-sa最小字典序

    #include <iostream> #include <cstdio> #include <vector> #include <cstring> # ...

  4. Codeforces 734D. Anton and Chess(模拟)

    Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the pro ...

  5. CF1005D Polycarp and Div 3 思维

    Polycarp and Div 3 time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  6. PHP-02.文件上传、php保存/转移上传的文件、常见的网络传输协议、请求报文及属性、响应报文及属性

    关系数组 array("key"=>"value",...) ; get没有数据大小的限制 post上传大小没有限制 不指定上传方式,默认是get 文件上 ...

  7. 大型公司java架构师面试实战讲解高清视频教程 15课

    目录: 01.面试必考之HashMap源码分析与实现02.探索JVM底层奥秘ClassLoader源码分析与案例讲解03.大型网站数据库瓶颈之数据库分库分表方案实践04.资料为图灵学院所有05.大型公 ...

  8. Mybatis系列(二)配置

    Mybatis系列(二)配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configu ...

  9. STL中区间最值max_element和min_element的用法

    前面的博客已经讲解了nth_element寻找区间第K大的用法,现在我们来说说这两个找区间最值的用法.两个函数都包含在algorithm库中. 一.函数原型 max_element template& ...

  10. 各种浏览器UA值

    UA  User-Agent:用户代理,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CPU 类型.浏览器及版本.浏览器引擎.浏览器语言.浏览器插件等. 标准格式为: 浏览器标识 ...