Tensorflow(一)Hello TensorFlow

重要概念:

一、基础概念

张量:使用张量(tensor)表示数据,用“阶”表示张量的维度。关于这一点需要展开一下

             0阶张量称为标量,表示单独的一个数

            1阶张量称为向量, 表示一个一维数组

            2阶张量称为矩阵,表示一个二维数组

            ……

数据流:数据流(data stream)是一组有序,有起点和终点的字节的数据序列。包括输入流和输出流。

二、TF基本概念

节点:nodes,

边:edges,分为正常边和特殊边。正常边数据可以自由流动,特殊边可以控制节点的互相依赖,使数据处理遵循一定的顺序。为多线程运行数据的执行,提高计算设备的效率。

会话:Session,是TF的主要交互方式。

TF处理数据的流程:

1.建立会话

2.生成一张空的图

3.添加各个节点和边

4.形成一个有连接点的图

.5.启动图对模型进行训练

 

变量的定义:

%静态
hello = tf.constant('Hello TensorFlow!' , dtype = tf.string)
a = tf.constant(1)
%动态
input = tf.variable(2,tf.int32)

    体验session:

import tensorflow as tf
input1 = tf.constant(1)
print(input1)

input2 = tf.Variable(2,tf.int32)
print(input2)

input2 = input1
sess = tf.Session()
print(input2)
print(sess.run(input2)) 

             

可以看到在未运行session时 变量全部是张量 当运行完毕输出的才是一个整型数字

矩阵运算:

tf.diag(diagonal,name=None) #生成对角矩阵

1

2

3

4

import tensorflowas tf;

diagonal=[1,1,1,1]

with tf.Session() as sess:

  print(sess.run(tf.diag(diagonal)))

1

2

3

4

#输出的结果为[[1 0 0 0]

   [0 1 0 0]

   [0 0 1 0]

   [0 0 0 1]]

tf.diag_part(input,name=None) #功能与tf.diag函数相反,返回对角阵的对角元素

1

2

3

4

import tensorflow as tf;

diagonal =tf.constant([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])

with tf.Session() as sess:

 print(sess.run(tf.diag_part(diagonal)))

1

#输出结果为[1,1,1,1]

tf.trace(x,name=None) #求一个2维Tensor足迹,即为对角值diagonal之和

1

2

3

4

import tensorflow as tf;

diagonal =tf.constant([[1,0,0,3],[0,1,2,0],[0,1,1,0],[1,0,0,1]])

with tf.Session() as sess:

 print(sess.run(tf.trace(diagonal)))#输出结果为4

tf.transpose(a,perm=None,name='transpose') #调换tensor的维度顺序,按照列表perm的维度排列调换tensor的顺序

1

2

3

4

5

6

7

import tensorflow as tf;

diagonal =tf.constant([[1,0,0,3],[0,1,2,0],[0,1,1,0],[1,0,0,1]])

with tf.Session() as sess:

 print(sess.run(tf.transpose(diagonal))) #输出结果为[[1 0 0 1]

                             [0 1 1 0]

                             [0 2 1 0]

                             [3 0 0 1]]

tf.matmul(a,b,transpose_a=False,transpose_b=False,a_is_sparse=False,b_is_sparse=False,name=None) #矩阵相乘

transpose_a=False,transpose_b=False #运算前是否转置

a_is_sparse=False,b_is_sparse=False #a,b是否当作系数矩阵进行运算

1

2

3

4

5

import tensorflow as tf;

A =tf.constant([1,0,0,3],shape=[2,2])

B =tf.constant([2,1,0,2],shape=[2,2])

with tf.Session() as sess:

 print(sess.run(tf.matmul(A,B)))

1

2

#输出结果为[[2 1]

   [0 6]]

tf.matrix_determinant(input,name=None) #计算行列式

1

2

3

4

import tensorflow as tf;

A =tf.constant([1,0,0,3],shape=[2,2],dtype=tf.float32)

with tf.Session() as sess:

 print(sess.run(tf.matrix_determinant(A)))

1

#输出结果为3.0

tf.matrix_inverse(input,adjoint=None,name=None)

adjoint决定计算前是否进行转置

1

2

3

4

import tensorflow as tf;

A =tf.constant([1,0,0,2],shape=[2,2],dtype=tf.float64)

with tf.Session() as sess:

 print(sess.run(tf.matrix_inverse(A)))

1

2

#输出结果为[[ 1. 0. ]

   [ 0. 0.5]]

tf.cholesky(input,name=None) #对输入方阵cholesky分解,即为将一个对称正定矩阵表示成一个下三角矩阵L和其转置的乘积德分解

1

2

3

4

import tensorflow as tf;

A =tf.constant([1,0,0,2],shape=[2,2],dtype=tf.float64)

with tf.Session() as sess:

 print(sess.run(tf.cholesky(A)))

1

2

#输出结果为[[ 1.   0.  ]

   [ 0.   1.41421356]]

                                             

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值