# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import tensorflow as tf
# 为EagerTensor对象启用numpy行为,否则tf对象的size、reshape等功能无法发使用
from tensorflow.python.ops.numpy_ops import np_config
np_config.enable_numpy_behavior()
print('生成一个元素个数为12的随机行向量(一维张量)')
x = tf.range(12)
print(x)
print('\n查看一维张量x的形状')
print(x.shape)
print(tf.shape(x))
print('\n查看一维张量x的元素个数')
print(x.size)
print(tf.size(x))
print('\n一维张量x整形')
x1 = x.reshape((3,4))
print(x1)
x2 = x.reshape((3,-1))
print(x2)
x3 = x.reshape((-1,4))
print(x3)
print('\n生成元素0张量,括号从外到里,分别是一维,二维,三维,不能简单的用行、列概念来理解')
x = tf.zeros((2,3,4))
print(x)
print(x.shape)
print('\n生成单位张量')
x = tf.ones((2,3,4))
print(x)
print(x.shape)
print('\n生成单位张量')
x = tf.eye(2,3, batch_shape=[2,3])
print(x)
print(x.shape)
print('\n生成一个均值为0,标准差为1的正态分布张量')
x = tf.random.normal((3,4),0,1)
print(x)
print('\n运算')
x = tf.random.normal((3,4),2,0.5)
print(x)
y = tf.random.uniform((3,4),2,0)
print(y)
zp = x + y
print('\n按元素相加x+y\n',zp)
zm = x*y
print('\n按元素相乘x*y\n',zm)
zd = x/y
print('\n按元素相除x/y\n',zd)
zr = x-y
print('\n按元素相减x-y\n',zr)
ze = x**y
print('\n按元素进行幂运算x**y\n',ze)
ze = tf.exp(x)
print('\n按元素求幂e^x=tf.exp(x)\n',ze)
le = x == y
print('\n按元素逻辑运算 le = (x==y?true,false)\n',le)
ll = x<y
print('\n按元素逻辑运算<\n',ll)
print('\n矩阵运算')
zc0 = tf.concat([x,y],axis = 0)
zc1 = tf.concat([x,y],axis = 1)
print('沿0轴连结张量concat([(3,4),(3,4)],axis = 0) = (6,4)\n',zc0)
print('\n沿1轴连结张良concat([(3,4),(3,4)],axis = 1) = (3,8)\n',zc1)
zrs = tf.reduce_sum(x,1,keepdims = True)
print('\n沿着指定的轴reduction_indices对矩阵进行求和运算,可以用list指定一系列的轴\n',zrs)
print('\n广播机制')
x = tf.range(3).reshape(3,-1)
y = tf.range(2).reshape(1,2)
print(x)
print(y)
bp = x+y
print('\nshape(2,3,4)+shape(2,3) = shape(4,6,4)')
print('\n广播加法需要维度相同,广播算法对张量的维度有严格的限制,不是任何形状的张良之间都可以\n',bp)
print('\ntensorflow 中的张量可以通过索引访问,但是不可变,不可以被赋值或者修改')
print('因为这个特性性,X = X + Y 计算时候实际会新分配一块内存存放结果,并将 X 指向这块新内存')
print('而不是在原来 X 的内存上直接加 Y 计算')
idb = id(x)
x = x + y
idp = id(x)
print('计算之前的 x 地址',idb)
print('计算之后的 x 地址',idp)
print(x)
print(x[2])
# x[2] = 5 # TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment
# print(x[2])
# x[2].assign(5) # 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign'
# print(x)
print('tensorflow 中的变量variables可变、可以被赋值或者修改\n')
x_var = tf.Variable(x)
print('\n创建一个张量变量(第一个字母大写),并将张量x赋值给它\n',x_var)
x_var[2].assign(5)
print('\n修改后的张量变量x_var\n',x_var)
print('\n修改后的张量x\n',x)
输出结果
生成一个元素个数为12的随机行向量(一维张量)
tf.Tensor([ 0 1 2 3 4 5 6 7 8 9 10 11], shape=(12,), dtype=int32)
查看一维张量x的形状
(12,)
tf.Tensor([12], shape=(1,), dtype=int32)
查看一维张量x的元素个数
12
tf.Tensor(12, shape=(), dtype=int32)
一维张量x整形
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]
[ 4 5 6 7]
[ 8 9 10 11]], shape=(3, 4), dtype=int32)
tf.Tensor(
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]], shape=(3, 4), dtype=int32)
生成元素0张量,括号从外到里,分别是一维,二维,三维,不能简单的用行、列概念来理解
tf.Tensor(
[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]], shape=(2, 3, 4), dtype=float32)
(2, 3, 4)
生成单位张量
tf.Tensor(
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]], shape=(2, 3, 4), dtype=float32)
(2, 3, 4)
生成单位张量
tf.Tensor(
[[[[1. 0. 0.]
[0. 1. 0.]]
[[1. 0. 0.]
[0. 1. 0.]]
[[1. 0. 0.]
[0. 1. 0.]]]
[[[1. 0. 0.]
[0. 1. 0.]]
[[1. 0. 0.]
[0. 1. 0.]]
[[1. 0. 0.]
[0. 1. 0.]]]], shape=(2, 3, 2, 3), dtype=float32)
(2, 3, 2, 3)
生成一个均值为0,标准差为1的正态分布张量
tf.Tensor(
[[-0.3970528 -0.55146474 -1.505552 1.1496323 ]
[ 0.27481812 -0.8336468 0.2977442 -0.16180256]
[ 0.57853585 0.01625594 -0.15270518 -3.0285838 ]], shape=(3, 4), dtype=float32)
运算
tf.Tensor(
[[2.113207 2.2268403 1.2627335 1.3833572]
[1.3802514 2.235139 2.0277615 1.4960109]
[2.6769288 1.9036808 2.144087 2.114948 ]], shape=(3, 4), dtype=float32)
tf.Tensor(
[[0.66726255 1.6175761 1.8788452 1.1592681 ]
[1.262851 0.14430022 1.4007611 1.4351525 ]
[1.25595 1.6337941 1.1845465 1.8138402 ]], shape=(3, 4), dtype=float32)
按元素相加x+y
tf.Tensor(
[[2.7804697 3.8444164 3.1415787 2.5426254]
[2.6431024 2.379439 3.4285226 2.9311633]
[3.9328787 3.5374749 3.3286335 3.9287882]], shape=(3, 4), dtype=float32)
按元素相乘x*y
tf.Tensor(
[[1.410064 3.6020837 2.3724806 1.6036819 ]
[1.7430519 0.32253104 2.8404095 2.147004 ]
[3.3620887 3.1102223 2.5397708 3.8361776 ]], shape=(3, 4), dtype=float32)
按元素相除x/y
tf.Tensor(
[[ 3.1669798 1.3766525 0.67207956 1.1933022 ]
[ 1.0929645 15.489504 1.4476141 1.0424055 ]
[ 2.1313975 1.1651902 1.8100489 1.1660057 ]], shape=(3, 4), dtype=float32)
按元素相减x-y
tf.Tensor(
[[ 1.4459445 0.60926414 -0.61611176 0.22408903]
[ 0.11740041 2.0908387 0.62700033 0.06085837]
[ 1.4209788 0.26988673 0.9595406 0.30110788]], shape=(3, 4), dtype=float32)
按元素进行幂运算x**y
tf.Tensor(
[[1.6474857 3.6510143 1.5500617 1.4567354]
[1.5022643 1.1230645 2.6918898 1.7826171]
[3.4442165 2.8628542 2.4681528 3.8908246]], shape=(3, 4), dtype=float32)
按元素求幂e^x=tf.exp(x)
tf.Tensor(
[[ 8.274736 9.270527 3.5350714 3.9882684]
[ 3.9759011 9.34778 7.597061 4.4638467]
[14.540367 6.7105494 8.534246 8.289155 ]], shape=(3, 4), dtype=float32)
按元素逻辑运算 le = (x==y?true,false)
tf.Tensor(
[[False False False False]
[False False False False]
[False False False False]], shape=(3, 4), dtype=bool)
按元素逻辑运算<
tf.Tensor(
[[False False True False]
[False False False False]
[False False False False]], shape=(3, 4), dtype=bool)
矩阵运算
沿0轴连结张量concat([(3,4),(3,4)],axis = 0) = (6,4)
tf.Tensor(
[[2.113207 2.2268403 1.2627335 1.3833572 ]
[1.3802514 2.235139 2.0277615 1.4960109 ]
[2.6769288 1.9036808 2.144087 2.114948 ]
[0.66726255 1.6175761 1.8788452 1.1592681 ]
[1.262851 0.14430022 1.4007611 1.4351525 ]
[1.25595 1.6337941 1.1845465 1.8138402 ]], shape=(6, 4), dtype=float32)
沿1轴连结张良concat([(3,4),(3,4)],axis = 1) = (3,8)
tf.Tensor(
[[2.113207 2.2268403 1.2627335 1.3833572 0.66726255 1.6175761
1.8788452 1.1592681 ]
[1.3802514 2.235139 2.0277615 1.4960109 1.262851 0.14430022
1.4007611 1.4351525 ]
[2.6769288 1.9036808 2.144087 2.114948 1.25595 1.6337941
1.1845465 1.8138402 ]], shape=(3, 8), dtype=float32)
沿着指定的轴reduction_indices对矩阵进行求和运算,可以用list指定一系列的轴
tf.Tensor(
[[6.9861383]
[7.1391625]
[8.839645 ]], shape=(3, 1), dtype=float32)
广播机制
tf.Tensor(
[[0]
[1]
[2]], shape=(3, 1), dtype=int32)
tf.Tensor([[0 1]], shape=(1, 2), dtype=int32)
shape(2,3,4)+shape(2,3) = shape(4,6,4)
广播加法需要维度相同,广播算法对张量的维度有严格的限制,不是任何形状的张良之间都可以
tf.Tensor(
[[0 1]
[1 2]
[2 3]], shape=(3, 2), dtype=int32)
tensorflow 中的张量可以通过索引访问,但是不可变,不可以被赋值或者修改
因为这个特性性,X = X + Y 计算时候实际会新分配一块内存存放结果,并将 X 指向这块新内存
而不是在原来 X 的内存上直接加 Y 计算
计算之前的 x 地址 578543680
计算之后的 x 地址 578546320
tf.Tensor(
[[0 1]
[1 2]
[2 3]], shape=(3, 2), dtype=int32)
tf.Tensor([2 3], shape=(2,), dtype=int32)
tensorflow 中的变量variables可变、可以被赋值或者修改
创建一个张量变量(第一个字母大写),并将张量x赋值给它
<tf.Variable 'Variable:0' shape=(3, 2) dtype=int32, numpy=
array([[0, 1],
[1, 2],
[2, 3]])>
修改后的张量变量x_var
<tf.Variable 'Variable:0' shape=(3, 2) dtype=int32, numpy=
array([[0, 1],
[1, 2],
[5, 5]])>
修改后的张量x
tf.Tensor(
[[0 1]
[1 2]
[2 3]], shape=(3, 2), dtype=int32)