TensorFlow基础

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())

'''
 [0.40242875 0.64216566]]
 '''

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))

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))
'''
### TensorFlow 基础代码实例 对于初学者来说,理解如何创建张量并执行基本操作是非常重要的。下面是一个简单的例子来展示这些概念。 #### 创建常量和变量 在 TensorFlow 中,可以使用 `tf.constant` 来定义不可变的数据结构——即常量;而通过 `tf.Variable` 可以声明可训练参数或模型权重等需要更新的数值[^3]: ```python import tensorflow as tf # 定义两个浮点型常数 a = tf.constant(2.0, dtype=tf.float32) b = tf.constant(3.0) # 执行加法运算并将结果存储在一个新的张量c中 c = a + b print(c.numpy()) # 输出:5.0 ``` #### 使用会话运行图计算 早期版本的 TensorFlow 需要显式启动 Session 对象来进行计算,在 TensorFlow 2.x 版本之后,默认启用了即时执行模式(Eager Execution),因此不再需要手动管理 Sessions。 不过为了兼容旧版程序以及更深入地了解底层机制,这里还是给出基于 session 的实现方式: ```python with tf.compat.v1.Session() as sess: output = sess.run([c]) print(output) # 输出:[5.0] ``` #### 构建简单线性回归模型 接下来构建一个非常基础的学习案例—单特征输入的一元线性回归问题。此部分展示了如何利用梯度下降算法最小化损失函数从而拟合给定数据集中的关系[^2]: ```python import numpy as np # 准备一些模拟数据 X_data = np.random.rand(100).astype(np.float32) Y_data = X_data * 0.1 + 0.3 # 初始化权重W与偏置B Weights = tf.Variable(tf.random.uniform([1], -1.0, 1.0)) Biases = tf.Variable(tf.zeros([1])) # 定义预测值y_pred 和真实标签 y_true之间的均方误差作为loss function y_pred = Weights * X_data + Biases loss = tf.reduce_mean(tf.square(y_pred - Y_data)) # 设置优化器及其学习率 optimizer = tf.optimizers.SGD(learning_rate=0.5) for step in range(201): optimizer.minimize(loss, var_list=[Weights, Biases]) if not (step % 20): print(f'Step {step}, Loss={loss.numpy()}, W={Weights.numpy()}, B={Biases.numpy()}') ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真题OK撒

你的打赏将是我最大的创作

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值