1.安装验证代码
# 允许 Python 访问 TensorFlow 所有的类、方法和符号
import tensorflow as tf
import os
# 忽略级别 2 及以下的消息(级别 1 是提示,级别 2 是警告,级别 3 是错误)
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
# 图像定义,仅仅一个节点
message = tf.constant("welcome to the exciting of deep neural networks!")
# 通过会话执行计算图,这部分使用 with 关键字创建了会话,最后在会话中执行以上计算图
with tf.Session() as sess:
print(sess.run(message).decode())
输出收到的警告消息提醒TensorFlow代码可以以更快的速度运行,可以用以下代码忽略上述信息:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
打印结果:
b'Welcome to the exciting world of Deep Neural Networks!'
2.程序结构
每一段代码有对应的注释如下:
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
# 标量常量
t_1 = tf.constant(4)
# 常量向量
t_2 = tf.constant([4,3,2])
# 所有元素为0的张量
zero_t = tf.zeros([2,3],tf.int32)
# 所有元素为1的张量
ones_t = tf.ones([2,3],tf.int32)
# 在一定范围内生成一个从初值到终值等差排布的序列
range_t = tf.linspace(2.0,5.0,5)
# 开始(默认值=0)生成一个数字序列,增量为 delta,默认1,不包括终值
range_t2 = tf.range(10)
# 创建一个具有一定均值(默认值=0.0)和标准差(默认值=1.0)、形状为 [M,N] 的正态分布随机数组
tf_random1 = tf.random_normal([2,3],mean=2.0,stddev=4,seed=12)
# 创建一个具有一定均值(默认值=0.0)和标准差(默认值=1.0)、形状为 [M,N] 的截尾正态分布随机数组
tf_random2 = tf.truncated_normal([1,5],stddev=2,seed=12)
# 种子的 [minval(default=0),maxval] 范围内创建形状为 [M,N] 的给定伽马分布随机数组
tf_random3 = tf.random_uniform([2,3],maxval=4,seed=12)
# 将给定的张量裁剪为指定大小
tf.random_crop(tf_random1,[2,5],seed=12)
# 随机排列张量
tf.random_shuffle(tf_random2)
# 随机生成的张量受初始种子值的影响。要在多次运行或会话中获得相同的随机数,应该将种子设置为一个常数值
# 为所有随机产生的张量设置种子
tf.set_random_seed(54)
# 定义张量变量,初始化为形状为 [50,50] 的随机均匀分布,最小值=0,最大值=10
rand_t = tf.random_uniform([50,50],0,10,seed=0)
t_a = tf.Variable(rand_t)
# 定义变量的权重,权重变量使用正态分布随机初始化,均值为 0,标准差为 2,权重大小为 100×100
weights = tf.Variable(tf.random_normal([100,100],stddev=2))
# 定义变量的偏置,偏置由 100 个元素组成,每个元素初始化为 0;可选参数名
bias = tf.Variable(tf.zeros[100],name='biases')
# 变量初始化变量
weight2 = tf.Variable(weights.initialized_value(),name='w2')
# 显示初始化所有的声明变量
intial_op = tf.global_variables_initializer()
# 单独使用初始化
bias_2 = tf.Variable(tf.zeros([100,100]))
# 保存变量
saver = tf.train.Saver()
# 占位符
x = tf.placeholder("float")
# 优化内存,声明可训练标志为False的变量
t_large = tf.Variable([100,100],trainable=False)
具体运行结果可在你的PC机上试运行。
输出print(a.eval()),输出前设置默认会话,初始化变量等等需要注意。并且TensorFlow不能使循环操作。
3.矩阵操作
"""矩阵操作"""
# 所有加法、减、除、乘(按元素相乘)、取余等矩阵的算术运算都要求两个张量矩阵是相同的数据类型,否则就会产生错误。
# 可以使用 tf.cast() 将张量从一种数据类型转换为另一种数据类型
import tensorflow as tf
# 默认会话
sess = tf.InteractiveSession()
# 创建5x5对角矩阵
I_matrix = tf.eye(5)
print(I_matrix.eval())
# 创建10x10对角矩阵
X = tf.Variable(tf.eye(10))
X.initializer.run()
print(X.eval())
# 创建5x10矩阵
A = tf.Variable(tf.random_normal([5,10]))
A.initializer.run()
# 矩阵减
product = tf.matmul(A,X)
print(product.eval())
# 创建5x10的随机数矩阵
b = tf.Variable(tf.random_uniform([5,10],0,2,dtype=tf.int32))
b.initializer.run()
print(b.eval())
b_new = tf.cast(b,dtype=tf.float32)
# 矩阵相加减
t_sum = tf.add(product,b_new)
t_sub = product - b_new
print("A*X_b\n",t_sum.eval())
print("A*X-B\n",t_sub.eval())
# 创建正态分布随机数矩阵
a = tf.Variable(tf.random_normal([4,5],stddev=2))
b = tf.Variable(tf.random_normal([4,5],stddev=2))
# 矩阵相乘
A = a*b
# 矩阵乘标量
B = tf.scalar_mul(2,A)
# 矩阵按元素相除
# tf.div更新
C = tf.math.divide(a,b)
# 按照元素余数相除
D = tf.mod(a,b)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
writer = tf.summary.FileWriter('graphs',sess.graph)
a,b,A_R,B_R,C_R,D_R = sess.run([a,b,A,B,C,D])
print("a\n",a,"\nb\n",b,"a*b\n",A_R,"\n2*a*b\n",B_R,"\na/b\n",C_R,"\na%b\n",D_R)
writer.close()
运行结果:
>>>runfile('C:/Users/Administrator/PycharmProjects/tensorflow/tensorflow_first_step/004.py', wdir='C:/Users/Administrator/PycharmProjects/tensorflow/tensorflow_first_step')
2020-02-18 11:51:46.544521: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
# I_matrix
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
# X
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
# product
[[-0.40610123 -0.2704316 -0.30384514 0.525401 -0.88012385 0.4202014
0.29240692 1.1166872 1.0145204 -0.72506034]
[ 1.6676606 0.03016405 0.68296045 -0.31261164 0.5315036 -1.4977987
0.7476843 -0.62169176 -1.1161541 0.30952075]
[-0.5149915 -2.5708668 1.2171867 -0.7129367 -0.3718377 -0.02389234
-1.6044884 0.73351586 -0.6583927 1.6258613 ]
[-1.0015397 -0.3708995 -0.68311554 0.4567177 0.5170999 -1.2163455
-1.2903235 1.5428648 0.7781321 -0.08966139]
[ 0.17489289 -0.40651345 -0.45630482 -0.71860224 -0.12788357 1.2991335
0.26028943 0.65427196 0.5596626 -0.5658776 ]]
# b
[[1 0 1 0 1 0 0 1 1 1]
[1 0 1 0 0 0 1 0 1 1]
[1 1 1 0 1 1 0 1 1 0]
[0 0 0 1 0 1 0 1 0 1]
[0 0 1 1 0 1 0 1 0 0]]
A*X_b
[[ 0.5938988 -0.2704316 0.69615483 0.525401 0.11987615 0.4202014
0.29240692 2.1166873 2.0145204 0.27493966]
[ 2.6676607 0.03016405 1.6829605 -0.31261164 0.5315036 -1.4977987
1.7476842 -0.62169176 -0.11615407 1.3095207 ]
[ 0.48500848 -1.5708668 2.2171867 -0.7129367 0.62816226 0.97610766
-1.6044884 1.7335159 0.34160727 1.6258613 ]
[-1.0015397 -0.3708995 -0.68311554 1.4567177 0.5170999 -0.21634555
-1.2903235 2.5428648 0.7781321 0.91033864]
[ 0.17489289 -0.40651345 0.5436952 0.28139776 -0.12788357 2.2991335
0.26028943 1.654272 0.5596626 -0.5658776 ]]
A*X-B
[[-1.4061012 -0.2704316 -1.3038452 0.525401 -1.8801239 0.4202014
0.29240692 0.11668718 0.01452041 -1.7250603 ]
[ 0.6676606 0.03016405 -0.31703955 -0.31261164 0.5315036 -1.4977987
-0.2523157 -0.62169176 -2.1161542 -0.6904793 ]
[-1.5149915 -3.5708668 0.21718669 -0.7129367 -1.3718377 -1.0238923
-1.6044884 -0.26648414 -1.6583927 1.6258613 ]
[-1.0015397 -0.3708995 -0.68311554 -0.5432823 0.5170999 -2.2163455
-1.2903235 0.5428648 0.7781321 -1.0896614 ]
[ 0.17489289 -0.40651345 -1.4563048 -1.7186022 -0.12788357 0.29913354
0.26028943 -0.34572804 0.5596626 -0.5658776 ]]
a
[[ 3.5021663 -0.88131547 -1.3997164 1.191665 1.8879055 ]
[-2.8588586 -1.6479343 -0.6302295 -3.1965663 4.4284196 ]
[-4.2521834 -0.17339891 1.0759888 -1.5437734 -0.23892923]
[-0.6932632 2.7534182 -0.6618031 -3.5713363 -2.0886593 ]]
b
[[-1.7280556 -2.394157 -0.54763955 2.371407 1.5108099 ]
[ 1.7868623 1.2598823 0.9843019 -5.1865306 0.7296214 ]
[ 4.089147 -0.83099145 3.5361443 0.19071856 1.5570637 ]
[-1.9994446 2.514029 -1.5134727 -1.3500695 -2.3655868 ]] a*b
[[ -6.051938 2.1100075 0.76654005 2.825923 2.8522663 ]
[ -5.1083865 -2.0762033 -0.62033606 16.579088 3.2310698 ]
[-17.387804 0.144093 3.8048515 -0.29442623 -0.37202802]
[ 1.3861413 6.9221735 1.001621 4.8215523 4.9409046 ]]
2*a*b
[[-12.103876 4.220015 1.5330801 5.651846 5.7045326 ]
[-10.216773 -4.1524067 -1.2406721 33.158176 6.4621396 ]
[-34.775608 0.288186 7.609703 -0.58885247 -0.74405605]
[ 2.7722826 13.844347 2.003242 9.643105 9.881809 ]]
a/b
[[-2.0266514 0.36811098 2.5559082 0.50251395 1.2495983 ]
[-1.5999323 -1.3080065 -0.64028066 0.6163207 6.069476 ]
[-1.0398705 0.2086651 0.30428305 -8.094511 -0.15344858]
[ 0.34672788 1.0952213 0.43727458 2.645298 0.882935 ]]
a%b
[[-1.6820005 -0.88131547 -0.30443728 1.191665 0.37709558]
[ 0.7148659 0.87183034 0.35407245 -3.1965663 0.05069113]
[ 3.9261107 -0.17339891 1.0759888 0.17269364 1.3181344 ]
[-0.6932632 0.23938918 -0.6618031 -0.8711972 -2.0886593 ]]
- 说明:以下代码的意思:
2020-02-18 11:51:46.544521: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
提示计算机 CPU 支持AVX AVX2 (可以加速CPU计算),但你安装的 TensorFlow 版本不支持
解决办法(一):对于初学者,可以忽略,开头加下列代码:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
解决办法(二):如果需要对CPU进行优化,可以访问下面的github,重新编译tensorflow源码以兼容AVX
https://github.com/lakshayg/tensorflow-build
注意:https://github.com/lakshayg/tensorflow-build/blob/master/README.md访问这个网址查看对应的系统支持,特别注意一下。
- 警告:
WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
低版本向高版本过渡过程可能发生的变化,给你的提示,可以忽略不用管。