tensorflow 线性代数

# -*- coding: utf-8 -*-
"""
Created on Mon Feb  6 07:00:26 2023

@author: Dell
"""

import tensorflow as tf
import numpy as np

x = tf.constant(3.0)
y = tf.constant(2.0)

print('标量计算')
print('x + y',x+y)
print('x * y',x*y)
print('x / y',x/y)
print('x ** y',x**y)

x = tf.range(4)
print('x的形状',x.shape)
print('x的长度',len(x))

A = tf.reshape(tf.range(20),(4,5))
print('4*5 矩阵',A)
B = tf.transpose(A)
print('A 的转置矩阵',B)

print('两个矩阵的按元素乘法称为Hadamard积 ,或者圈乘')

print('通过降维求和函数降维')
A = tf.reshape(tf.range(60),(3,4,5))
print(A)
print('对A的第二个维度进行降维处理',tf.reduce_sum(A,axis=1))

print('非降维求和')
A_sum = tf.reduce_sum(A,axis = 1,keepdims=True)
print('对A进行非降维求和',)

print('计算指定维度(axis=1)上的均值',A/A_sum)

print('\ntensorflow 中向量默认是指的列向量,生成的一维张量默认是行向量,但是在进行向量积的计算时候\
      ,B输入的却是一个行向量,线性代数中矩阵乘法定义的是个列向量,因此会引入歧义,\
      需要在实际使用的时候仔细分辨')
A = tf.reshape(tf.range(12),(3,4))
B = tf.range(4)
C = tf.linalg.matvec(A,B)
print(A)
print(B)
print('\n矩阵向量积',C)

SD1 = tf.reduce_sum(tf.tensordot(A,B,axes = 1))
print('\n点乘tf.reduce_sum(tf.tensordot(A,B,axes = 1))',SD1)
SD2 = tf.reduce_sum(A*B)
print('\n点乘tf.reduce_sum(A*B)=',SD2)

A = tf.reshape(tf.range(12),(3,4))
B = tf.reshape(tf.range(20),(4,5))
C = tf.matmul(A,B)
print('\n矩阵向量积(矩阵乘法)tf.matmul(A,B) = ',C)


print('\n使用tensorflow计算向量范数')
Cmod = tf.norm(tf.range(12,dtype=float))
print('\n范数只接受浮点数向量,tf.norm(C)=',Cmod)
print('int型张量报错,Value for attr "T "of int32 is not in the list of allowed values: bfloat16, half, float, double, complex64, complex128')

print('\n使用numpy计算矩阵范数')
Aarray = np.ones((4,9))
print(Aarray)
Anorm = np.linalg.norm(Aarray)
print('矩阵佛罗奔尼斯范数np.linalg.norm(Aarray) = ',Anorm)
标量计算
x + y tf.Tensor(5.0, shape=(), dtype=float32)
x * y tf.Tensor(6.0, shape=(), dtype=float32)
x / y tf.Tensor(1.5, shape=(), dtype=float32)
x ** y tf.Tensor(9.0, shape=(), dtype=float32)
x的形状 (4,)
x的长度 4
4*5 矩阵 tf.Tensor(
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]], shape=(4, 5), dtype=int32)
A 的转置矩阵 tf.Tensor(
[[ 0  5 10 15]
 [ 1  6 11 16]
 [ 2  7 12 17]
 [ 3  8 13 18]
 [ 4  9 14 19]], shape=(5, 4), dtype=int32)
两个矩阵的按元素乘法称为Hadamard积 ,或者圈乘
通过降维求和函数降维
tf.Tensor(
[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]
  [10 11 12 13 14]
  [15 16 17 18 19]]

 [[20 21 22 23 24]
  [25 26 27 28 29]
  [30 31 32 33 34]
  [35 36 37 38 39]]

 [[40 41 42 43 44]
  [45 46 47 48 49]
  [50 51 52 53 54]
  [55 56 57 58 59]]], shape=(3, 4, 5), dtype=int32)
对A的第二个维度进行降维处理 tf.Tensor(
[[ 30  34  38  42  46]
 [110 114 118 122 126]
 [190 194 198 202 206]], shape=(3, 5), dtype=int32)
非降维求和
对A进行非降维求和
计算指定维度(axis=1)上的均值 tf.Tensor(
[[[0.         0.02941176 0.05263158 0.07142857 0.08695652]
  [0.16666667 0.17647059 0.18421053 0.19047619 0.19565217]
  [0.33333333 0.32352941 0.31578947 0.30952381 0.30434783]
  [0.5        0.47058824 0.44736842 0.42857143 0.41304348]]

 [[0.18181818 0.18421053 0.18644068 0.18852459 0.19047619]
  [0.22727273 0.22807018 0.22881356 0.2295082  0.23015873]
  [0.27272727 0.27192982 0.27118644 0.2704918  0.26984127]
  [0.31818182 0.31578947 0.31355932 0.31147541 0.30952381]]

 [[0.21052632 0.21134021 0.21212121 0.21287129 0.21359223]
  [0.23684211 0.2371134  0.23737374 0.23762376 0.23786408]
  [0.26315789 0.2628866  0.26262626 0.26237624 0.26213592]
  [0.28947368 0.28865979 0.28787879 0.28712871 0.28640777]]], shape=(3, 4, 5), dtype=float64)

tensorflow 中向量默认是指的列向量,生成的一维张量默认是行向量,但是在进行向量积的计算时候      ,B输入的却是一个行向量,线性代数中矩阵乘法定义的是个列向量,因此会引入歧义,      需要在实际使用的时候仔细分辨
tf.Tensor(
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]], shape=(3, 4), dtype=int32)
tf.Tensor([0 1 2 3], shape=(4,), dtype=int32)

矩阵向量积 tf.Tensor([14 38 62], shape=(3,), dtype=int32)

点乘tf.reduce_sum(tf.tensordot(A,B,axes = 1)) tf.Tensor(114, shape=(), dtype=int32)

点乘tf.reduce_sum(A*B)= tf.Tensor(114, shape=(), dtype=int32)

矩阵向量积(矩阵乘法)tf.matmul(A,B) =  tf.Tensor(
[[ 70  76  82  88  94]
 [190 212 234 256 278]
 [310 348 386 424 462]], shape=(3, 5), dtype=int32)

使用tensorflow计算向量范数

范数只接受浮点数向量,tf.norm(C)= tf.Tensor(22.494444, shape=(), dtype=float32)
int型张量报错,Value for attr "T "of int32 is not in the list of allowed values: bfloat16, half, float, double, complex64, complex128

使用numpy计算矩阵范数
[[1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1.]]
矩阵佛罗奔尼斯范数np.linalg.norm(Aarray) =  6.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值