import tensorflow as tf
import numpy as np

点乘,支持broadcasting

  • 乘号* 和 multiply等价
  • mul已经废弃不用了
  • matmul 是矩阵相乘

broadcasting参见:

http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

原则:

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when

  • they are equal, or
  • one of them is 1
a = tf.placeholder("float")
b = tf.placeholder("float")
y = a * b
y2 = tf.multiply(a,b)
sess = tf.Session()
r, r2 = sess.run([y,y2], feed_dict={a:[3,1], b:[[4],[1]]})
print r
print r2
sess.close()

切片 slice

  • begin 起点的坐标
  • size 切片的大小
  • begin[i] + size[i] <=input.shape[i]

分割 split, 沿着某一维度将tensor分割为n个tensor list

  • 分割后list中的每个tensor的维度不降
input = tf.placeholder('float')
begin = tf.placeholder('int32')
size = tf.placeholder('int32')
out = tf.slice(input, begin, size)
s = tf.split(input, num_or_size_splits=3, axis=0)
sess= tf.Session()
input_ = [[[1, 1, 1], [2, 2, 2]],[[3, 3, 3], [4, 4, 4]],[[5, 5, 5], [6, 6, 6]]]
begin_ = [1, 0, 0]
size_ = [1,1,3]
o = sess.run(out, feed_dict={input:input_, begin:begin_, size:size_})
print o
s_ = sess.run(s, feed_dict={input:input_})
print(s_)
print(type(s_))
print(s_[0])
print(type(s_[0]))
print(type(s))
print(type(out))
sess.close()
[[[ 3.  3.  3.]]]
[array([[[ 1., 1., 1.],
[ 2., 2., 2.]]], dtype=float32), array([[[ 3., 3., 3.],
[ 4., 4., 4.]]], dtype=float32), array([[[ 5., 5., 5.],
[ 6., 6., 6.]]], dtype=float32)]
<type 'list'>
[[[ 1. 1. 1.]
[ 2. 2. 2.]]]
<type 'numpy.ndarray'>
<type 'list'>
<class 'tensorflow.python.framework.ops.Tensor'>

tensor concat 沿着某一维度连接tensor

tensor stack 将一系列rank=R的tensor 打包为rank=R+1的tensor

tensor pack 已被废弃,用stack代替

t1 = tf.constant([[1,2,3],[4,5,6]])
t2 = tf.constant([[7,8,9],[10,11,12]])
c1= tf.concat([t1,t2], 0)
c2= tf.concat([t1,t2], 1)
r1 = tf.reshape(c1, [-1])
r2 = tf.reshape(c2, [-1]) p1 = tf.stack([t1,t2],0)
p2 = tf.stack([t1,t2],1) p3 = [t1, t2] with tf.Session() as sess:
print(sess.run([c1, c2]))
print(sess.run([tf.rank(t1),tf.rank(c1)]))
print("=======")
print(sess.run([r1, r2]))
print("=======")
print(sess.run([p1, p2]))
print(sess.run([tf.rank(t1),tf.rank(p1)]))
print(sess.run(tf.shape([p1, p2])))
print("=======")
print(sess.run(p3))
[array([[ 1,  2,  3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]], dtype=int32), array([[ 1, 2, 3, 7, 8, 9],
[ 4, 5, 6, 10, 11, 12]], dtype=int32)]
[2, 2]
=======
[array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=int32), array([ 1, 2, 3, 7, 8, 9, 4, 5, 6, 10, 11, 12], dtype=int32)]
=======
[array([[[ 1, 2, 3],
[ 4, 5, 6]], [[ 7, 8, 9],
[10, 11, 12]]], dtype=int32), array([[[ 1, 2, 3],
[ 7, 8, 9]], [[ 4, 5, 6],
[10, 11, 12]]], dtype=int32)]
[2, 3]
=======
[array([[1, 2, 3],
[4, 5, 6]], dtype=int32), array([[ 7, 8, 9],
[10, 11, 12]], dtype=int32)]
=======
[2 2 2 3]
c1 = tf.constant([1])
c2 = tf.constant([2])
c3 = tf.constant([3])
con = tf.concat([c1,c2,c3], 0)
with tf.Session() as sess:
print(sess.run(tf.shape(c1)[0]))
print(sess.run(con))
1
[1 2 3]

可以认为 tensor 是一个n维的数组(array)或列表(list), tensor具有静态的type 和动态的维度

一个scalar 就是0维的数组,rank = 0,shape是[]

下面的

c1 rank=0, shap=[]

c2 rank=1, shap=[1]

c3 rank=1, shap=[2]

c1 = tf.constant(1)
c2 = tf.constant([1])
c3 = tf.constant([1,2])
s1 = tf.shape(c1)
s2 = tf.shape(c2)
s3 = tf.shape(c3)
with tf.Session() as sess:
c1_, c2_, c3_ = sess.run([c1,c2,c3])
print(c1_, c2_, c3_)
print(type(c1_), type(c2_), type(c3_))
print(c1_.shape)
print("================")
o1, o2,o3 = sess.run([s1,s2,s3])
print(o1, o2, o3)
print(type(o1), type(o2), type(o3))
print(o1.shape)
(1, array([1], dtype=int32), array([1, 2], dtype=int32))
(<type 'numpy.int32'>, <type 'numpy.ndarray'>, <type 'numpy.ndarray'>)
()
================
(array([], dtype=int32), array([1], dtype=int32), array([2], dtype=int32))
(<type 'numpy.ndarray'>, <type 'numpy.ndarray'>, <type 'numpy.ndarray'>)
(0,)
li = np.zeros(5)
li[0] = tf.constant(1) # tensor是个数组,哪怕是纯量
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-66-07967146eb99> in <module>()
1 li = np.zeros(5)
----> 2 li[0] = tf.constant(1) ValueError: setting an array element with a sequence.

placeholder, 不指定shape时候,可以feed任何shape的数据;指定的话,必须按照指定的shape feed 数据

x = tf.placeholder(shape=(2,1), dtype=tf.int32)
m = tf.placeholder(dtype=tf.int32)
y = x*2
n = m*2
with tf.Session() as sess:
m_ = [[2.0],[2.0]]
m_o = sess.run(m, feed_dict={m:m_})
print(m_)
print(m_o)
print(m_ - m_o)
print(type(m_), type(m_o))
print(sess.run(n, feed_dict={m:[[2,2],[1,1]]}))
[[2.0], [2.0]]
[[2]
[2]]
[[ 0.]
[ 0.]]
(<type 'list'>, <type 'numpy.ndarray'>)
[[4 4]
[2 2]]
li = []
li.append([tf.constant(1)])
li.append([tf.constant(2)])
li2 = tf.concat(li, axis=0)
with tf.Session() as sess:
print(sess.run(li))
print(sess.run(li2))
[[1], [2]]
[1 2]
li = []
li.append(tf.constant(1))
li.append(tf.constant(2))
li2 = tf.stack(li, axis=0)
#li2 = tf.concat(li, axis=0) # 错误, 纯量不能使用cancat
with tf.Session() as sess:
print(sess.run(li))
print(sess.run(li2))
[1, 2]
[1 2]

tensorflow基本操作(1)的更多相关文章

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

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

  2. Tensorflow基本操作理解

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

  3. 深度学习原理与框架-Tensorflow基本操作-mnist数据集的逻辑回归 1.tf.matmul(点乘操作) 2.tf.equal(对应位置是否相等) 3.tf.cast(将布尔类型转换为数值类型) 4.tf.argmax(返回最大值的索引) 5.tf.nn.softmax(计算softmax概率值) 6.tf.train.GradientDescentOptimizer(损失值梯度下降器)

    1. tf.matmul(X, w) # 进行点乘操作 参数说明:X,w都表示输入的数据, 2.tf.equal(x, y) # 比较两个数据对应位置的数是否相等,返回值为True,或者False 参 ...

  4. 深度学习原理与框架-Tensorflow基本操作-实现线性拟合

    代码:使用tensorflow进行数据点的线性拟合操作 第一步:使用np.random.normal生成正态分布的数据 第二步:将数据分为X_data 和 y_data 第三步:对参数W和b, 使用t ...

  5. 深度学习原理与框架-Tensorflow基本操作-变量常用操作 1.tf.random_normal(生成正态分布随机数) 2.tf.random_shuffle(进行洗牌操作) 3. tf.assign(赋值操作) 4.tf.convert_to_tensor(转换为tensor类型) 5.tf.add(相加操作) tf.divide(相乘操作) 6.tf.placeholder(输入数据占位

    1. 使用tf.random_normal([2, 3], mean=-1, stddev=4) 创建一个正态分布的随机数 参数说明:[2, 3]表示随机数的维度,mean表示平均值,stddev表示 ...

  6. 深度学习原理与框架-Tensorflow基本操作-Tensorflow中的变量

    1.tf.Variable([[1, 2]])  # 创建一个变量 参数说明:[[1, 2]] 表示输入的数据,为一行二列的数据 2.tf.global_variables_initializer() ...

  7. tensorflow基本操作介绍

    1.tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf #定义‘符号’变量,也称为占位符 a = tf. ...

  8. TF Boys (TensorFlow Boys ) 养成记(一):TensorFlow 基本操作

    本资料是在Ubuntu14.0.4版本下进行,用来进行图像处理,所以只介绍关于图像处理部分的内容,并且默认TensorFlow已经配置好,如果没有配置好,请参考官方文档配置安装,推荐用pip安装.关于 ...

  9. 53、tensorflow基本操作

    import tensorflow as tf import numpy as np x_data = np.float32(np.random.rand(2,100)) print(x_data) ...

随机推荐

  1. 出大问题!webpack 多入口&&html模板在后端

    新公司前后端不分离,后端用的是php的twig 我用webpack做多入口文件的打包,虽然成功了.但是引入js和css是在twig上写死的根据文件名. 一开始没问题,因为就定死了那么几个global. ...

  2. npm学习(十二)之高级用法

    如何使用距离标记标记包 如何使用双因素身份验证 如何使用安全令牌 如何从CLI更改配置文件设置 理解包和模块

  3. 解决错误:无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://shiro.apache.org/tags]

    服务器错误信息如下: 解决方法: 把shiro包中的tld文件(shiro.tld)解压出来放到WEB-INF文件夹下即解决问题. 参考:http://blog.sina.com.cn/s/blog_ ...

  4. python子进程模块subprocess详解

    subprocess--子进程管理器一.subprocess 模块简介subprocess最早是在2.4版本中引入的.subprocess模块用来生成子进程,并可以通过管道连接它们的输入/输出/错误, ...

  5. Python控制语句执行流程

    if语句: if<条件>:#条件之后必须有“:”.  <语句> if语句的三元运算符: <表达式1>if<条件>else<表达式2>:其语义 ...

  6. 机器学习聚类算法之DBSCAN

    一.概念 DBSCAN是一种基于密度的聚类算法,DBSCAN需要两个参数,一个是以P为中心的邻域半径:另一个是以P为中心的邻域内的最低门限点的数量,即密度. 优点: 1.不需要提前设定分类簇数量,分类 ...

  7. (转) MiniUI使用

    来源于https://my.oschina.net/yunsy/blog/542597 1.MiniUI页签定位 <body> <input name = "bizType ...

  8. Atcoder grand 025 组合数学塔涂色 贪心走路博弈

    A 略 B 题意:给你N个数(3e5) 每个数可以是0,a,b,a+b(3e5) 但是总数加起来要是定值K(18e10) 问总方法数mod 998244353 解: 把a+b的看成是一个a加上一个b的 ...

  9. kettle批量导入json数据

    kettle新手上路,烦死了,工具好用,批量导入数据也快,就是有很多小细节需要注意. 使用kettle进行数据导入时,因为最近在做json数据的入库,以JSON Input为例进行说明: 首先是大概流 ...

  10. CentOS7安装并使用Ceph

    1.准备工作1.1 安装配置NTP官方建议在所有 Ceph 节点上安装 NTP 服务(特别是 Ceph Monitor 节点),以免因时钟漂移导致故障. ln -sf /usr/share/zonei ...