第2章 TensorFlow 基础

简介: 第2章 TensorFlow 基础

第2章 TensorFlow 基础

2.1 张量
import tensorflow as tf
x = tf.constant([3.0, 1.0], name = "x")
y = tf.constant([4.0, 5.0], name = "y")
result = tf.add(x, y, name = "add")
print(result)
'''
Tensor("add_1:0", shape=(2,), dtype=float32)
'''
import tensorflow as tf
sess = tf.Session()  # 通过 tf.Session() 的方式来获取计算图的结果
# 创建张量
x = tf.constant([3.0, 1.0], name = "x")
y = tf.constant([4.0, 5.0], name = "y")
result = tf.add(x, y, name = "add")
print(sess.run(result))
'''
[7. 6.]
'''
1. 固定张量
import tensorflow as tf
ones_tensor = tf.ones([2, 3])
filled_tensor = tf.fill([2, 2], 3)
constant_tensor = tf.constant([1, 2, 3])
2. 相似的形状和张量
import tensorflow as tf
constant_tensor = tf.constant([1, 2, 3])
zeros_similar = tf.zeros_like(constant_tensor)
one_similar = tf.ones_like(constant_tensor)
'''
'''
3. 序列张量
import tensorflow as tf
linear_tensor = tf.linspace(start=0.0, stop=1.0, num = 3)
# 所创建的张量的值为:[0.0,0.5,1.0]
sequence_tensor = tf.range(start=0, limit=10, delta=3)
# 所创建张量的值为[0, 3, 6, 9]
4. 随机张量
import tensorflow as tf
randuniform_tensor = tf.random_uniform([2, 2], minval=0.0, maxval=1.0)
# 本次创建的均匀分布随机张量为[[0.42212788, 0.96672773],[0.6716949, 0.25362217]]
randormal_tensor = tf.random_normal([2, 2], mean=0.0, stddev=1.0)
# 本次创建的正态分布随机张量为[[0.06353379 0.13682544][0.73617303, 0.13042414]]
truncnormal_tensor = tf.truncated_normal([2,2], mean=0.0, stddev=1.0)
# 本次创建正态分布随机张量为[[0.65179425 -0.7365027][0.73617303 0.13042414]]
shuffle_output = tf.random_shuffle([[1,2],[3,4],[5,6]])
#随机打乱后的张量为[[1 2][5 6][3 4]]
corpped_output = tf.random_crop([[1,2],[3,4],[5,6],[7,8]], [2,2])
# 随机裁剪得到得张量为[[3,4][5,6]]
2.2 会话
import tensorflow as tf
sess = tf.Session()
a = tf.constant([3.0, 4.0])
b = tf.constant([5.0, 6.0])
result = a + b
with sess.as_default():
    print(result.eval())
'''
[ 8. 10.]
'''
2.3 变量与占位符
import tensorflow as tf
# 定义变量并初始化为 0
variable_name = tf.Variable(tf.zeros([2, 2]))
# 创建会话并运行初始化操作
sess = tf.Session()
initalize_op = tf.global_variables_initializer()
sess.run(initalize_op)
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=(2,2))
y = tf.identity(x)
rand_array = np.random.rand(2,2)
merged = tf.summary.FileWriter("/tmp/variable_logs", sess.graph)
print(sess.run(y, feed_dict={x: rand_array}))
'''
[[0.2956999  0.10164797]
 [0.40242875 0.64216566]]
'''
import tensorflow as tf
sess = tf.Session()
x = tf.Variable(tf.zeros([1,2]))
print(sess.run(x.initializer))
y = tf.Variable(tf.zeros_like(x))
print(sess.run(y.initializer))
'''
None
None
'''
2.4 矩阵
2.4.1 创建矩阵
import numpy as np
import tensorflow as tf
sess = tf.Session()
# 创建一个 3x3 的全 0 矩阵
m1 = tf.zeros([3,3])
print(sess.run(m1))
# 创建一个 3x3 的全 1 矩阵
m2 = tf.ones([3,3])
print(sess.run(m2))
# 创建一个 3x3 的填充为 6 的矩阵
m3 = tf.fill([3,3],6)
print(sess.run(m3))
# 创建一个常量矩阵
m4 = tf.constant([1,2,3,4,5,6])
print(sess.run(m4))
# 创建一个 3x3 的随机矩阵
m5 = tf.truncated_normal([3,3])
# 创一个 3x3 的正态分布矩阵
m6 = tf.random_uniform([3,3])
print(sess.run(m6))
# 通过 numpy 数组创建矩阵
m7 = tf.convert_to_tensor(np.array([[1,2,3],[2,3,4],[3,4,5]]))
print(sess.run(m7))
'''
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[[6 6 6]
 [6 6 6]
 [6 6 6]]
[1 2 3 4 5 6]
[[0.13449848 0.64351976 0.99465334]
 [0.77233136 0.8220886  0.8941928 ]
 [0.65133333 0.7570833  0.07037628]]
[[1 2 3]
 [2 3 4]
 [3 4 5]]
'''
import tensorflow as tf
sess = tf.Session()
diagonal = [1,1,1,1]
print(sess.run(tf.diag(diagonal)))
'''
[[1 0 0 0]
 [0 1 0 0]
 [0 0 1 0]
 [0 0 0 1]]
 '''
import tensorflow as tf
sess = tf.Session()
diagonal = tf.constant([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])
print(sess.run(tf.diag_part(diagonal)))
'''
[1 1 1 1]
'''
2.4.2 矩阵的基本运算

矩阵的加减法

import tensorflow as tf
m1 = tf.zeros([3,3])
m2 = tf.ones([3,3])
print(sess.run(m1+m2))
print(sess.run(m1-m2))
'''
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[[-1. -1. -1.]
 [-1. -1. -1.]
 [-1. -1. -1.]]
 '''

矩阵乘法

import tensorflow as tf
m1 = tf.zeros([3,3])
m2 = tf.ones([3,3])
print(sess.run(tf.matmul(m1, m2)))
'''
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
'''

矩阵转置

import  numpy as np
import tensorflow as tf
sess = tf.Session()
m6 = tf.constant(np.array([[1,2,3],[4,5,6],[7,8,9]]))
print(sess.run(m6),'\n')
print(sess.run(tf.transpose(m6)))
'''[[1 2 3]
 [4 5 6]
 [7 8 9]] 
[[1 4 7]
 [2 5 8]
 [3 6 9]]'''

矩阵行列式

import tensorflow as tf
sess = tf.Session()
m3 = tf.fill([3,3],6.0)
print(sess.run(m3))
print(sess.run(tf.matrix_determinant(m3)))
'''
[[6. 6. 6.]
 [6. 6. 6.]
 [6. 6. 6.]]
 0.0
 '''

逆矩阵

import tensorflow as tf
import numpy as np
sess = tf.Session()
# create a 3x3 matrix filled with random values
# m = tf.constant(np.random.rand(3,3))
m = tf.constant([[0.06507223, 0.41723821, 0.39361987],
 [0.80711338, 0.79708773, 0.78492795],
 [0.77092674, 0.65978775, 0.46901021]] )
print(sess.run(m), '\n')
# calculate the inverse of the matrix
m_inv = tf.matrix_inverse(m)
with tf.Session() as sess:
    # evaluate the inverse matrix
    inv = sess.run(m_inv)
    print(inv)
    
'''
[[0.06507223 0.4172382  0.39361987]
 [0.8071134  0.7970877  0.78492796]
 [0.7709267  0.6597878  0.4690102 ]] 
[[-2.7230706   1.210201    0.25998154]
 [ 4.2833314  -5.159649    5.0402927 ]
 [-1.5496508   5.269176   -5.3857045 ]]
  '''

矩阵分解

如果输入矩阵不满足正定性条件,那么 tf.cholesky() 函数将会抛出一个异常。

在这种情况下,你可以使用其他方法来分解或近似矩阵。以下代码演示了如何使用 TensorFlow 中的奇异值分解 (SVD) 来近似输入矩阵:

import tensorflow as tf
sess = tf.Session()
# 定义输入矩阵
#A = tf.constant([[0.06507223, 0.41723821, 0.39361987],
#                  [0.80711338, 0.79708773, 0.78492795],
#                  [0.77092674, 0.65978775, 0.46901021]])
A = tf.fill([3,3],9.0)
# 奇异值分解
s, u, v = tf.svd(A)
# 构造对角矩阵
s_mat = tf.diag(s)
# 近似重构原始矩阵
A_approx = tf.matmul(tf.matmul(u, s_mat), v, transpose_b=True)
# 打印结果
print(sess.run(A_approx))
'''
[[8.999999 8.999999 8.999999]
 [8.999999 8.999999 8.999999]
 [8.999999 8.999999 8.999999]]
 '''

特征值与特征向量

import tensorflow as tf
m6 = tf.fill([3,3],5.0)
print(sess.run(tf.self_adjoint_eig(m6)))
'''
(array([-5.9604645e-07, -0.0000000e+00,  1.4999999e+01], dtype=float32), array([[ 0.        , -0.81649655,  0.57735026],
       [-0.70710677,  0.4082483 ,  0.57735026],
       [ 0.7071068 ,  0.40824828,  0.57735026]], dtype=float32))
 '''


目录
相关文章
|
5月前
|
机器学习/深度学习 存储 TensorFlow
TensorFlow 基础实战
TensorFlow 基础实战
|
13天前
|
机器学习/深度学习 PyTorch TensorFlow
NumPy与TensorFlow/PyTorch的集成实践
【4月更文挑战第17天】本文探讨了NumPy与主流深度学习框架TensorFlow和PyTorch的集成实践,阐述了它们如何通过便捷的数据转换提升开发效率和模型性能。在TensorFlow中,NumPy数组可轻松转为Tensor,反之亦然,便于原型设计和大规模训练。PyTorch的张量与NumPy数组在内存中共享,实现无缝转换。尽管集成带来了性能和内存管理的考量,但这种结合为机器学习流程提供了强大支持,促进了AI技术的发展。
|
2月前
|
TensorFlow 算法框架/工具
TensorFlow基础
TensorFlow基础
27 0
|
11月前
|
机器学习/深度学习 人工智能 并行计算
【PyTorch】Pytorch基础第0章
【PyTorch】Pytorch基础第0章
61 0
|
12月前
|
机器学习/深度学习 分布式计算 自然语言处理
「技术选型」Keras、TensorFlow和PyTorch的区别
「技术选型」Keras、TensorFlow和PyTorch的区别
|
存储 SQL 机器学习/深度学习
|
机器学习/深度学习 Web App开发 物联网
|
机器学习/深度学习 算法框架/工具 C++
第3章 TensorFlow入门
第3章 TensorFlow入门 TensorFlow是Google创造的数值运算库,作为深度学习的底层使用。本章包括: TensorFlow介绍 如何用TensorFlow定义、编译并运算表达式 如何寻求帮助 注意:TensorFlow暂时不支持Windows,你可以用Docker或虚拟机。
1560 0