TensorFlow自学笔记-03 基于官方文档 矩阵基本操作
矩阵运算,例如执行乘法、加法和减法,是任何神经网络中信号传播的重要操作。通常在计算中需要随机矩阵、零矩阵、一矩阵或者单位矩阵。
import tensorflow as tf
# 开启一个交互式会话
sess = tf.InteractiveSession()
# 创建一个对角阵
I_matrix = tf.eye(5)
print(I_matrix.eval())
# 创建一个变量
X = tf.Variable(tf.eye(10))
X.initializer.run()
print(X.eval())
# 创建一个随机矩阵
A = tf.Variable(tf.random_normal([5, 10]))
A.initializer.run()
# 矩阵乘法
product = tf.matmul(A, X)
print(product.eval())
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", t_sum.eval())
print("A*X-b", t_sub.eval())
a = tf.Variable(tf.random_normal([4, 5], stddev=2))
b = tf.Variable(tf.random_normal([4, 5], stddev=2))
# 对应元素相乘
C = a * b
# 对应元素乘以2
D = tf.scalar_mul(2, C)
# 对应元素相除
E = tf.div(a, b)
# 暗元素取余
F = 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, C_R, D_R, E_R, F_R = sess.run([a, b, C, D, E, F])
print("a\n", a, "\nb\n", "b", "a*b\n", C_R, "\n2*a*b\n", D_R, "\na/b\n", E_R, "\na%b\n", D_R)
writer.close()
运行结果:
[[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.]]
[[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.]]
[[-0.32839566 0.70773065 1.566395 -0.38252068 -0.7719653 -0.5209189
0.85864055 2.096747 -0.82279724 -0.6647939 ]
[ 0.23995121 0.6506701 0.5211475 1.0676296 0.40433612 1.0071797
-0.7312989 0.33244786 1.2448255 -1.5655426 ]
[ 0.16492666 -1.3693453 0.5388546 0.5675936 0.4816498 -0.6679784
-0.49331537 0.8492635 -0.5760576 -1.6270505 ]
[ 1.2073728 -1.0819451 0.39811417 -0.06598005 -0.41204235 3.3360064
-0.63800156 0.8687895 0.531367 -0.5185357 ]
[-0.9538085 0.9306115 -1.1394081 2.7497325 1.0245194 1.7426227
-1.6228052 -1.207261 -0.81112486 1.0767409 ]]
[[1 1 0 1 0 1 1 1 0 1]
[0 1 0 1 0 0 1 0 1 0]
[0 0 1 1 0 0 1 1 0 1]
[1 1 0 0 0 0 0 1 0 1]
[0 1 0 0 1 0 1 0 0 1]]
A*X+b [[ 0.67160434 1.7077307 1.566395 0.6174793 -0.7719653 0.4790811
1.8586406 3.096747 -0.82279724 0.3352061 ]
[ 0.23995121 1.65067 0.5211475 2.0676296 0.40433612 1.0071797
0.26870108 0.33244786 2.2448254 -1.5655426 ]
[ 0.16492666 -1.3693453 1.5388546 1.5675936 0.4816498 -0.6679784
0.50668466 1.8492634 -0.5760576 -0.6270505 ]
[ 2.2073727 -0.08194506 0.39811417 -0.06598005 -0.41204235 3.3360064
-0.63800156 1.8687894 0.531367 0.48146433]
[-0.9538085 1.9306115 -1.1394081 2.7497325 2.0245194 1.7426227
-0.62280524 -1.207261 -0.81112486 2.0767407 ]]
A*X-b [[-1.3283956 -0.29226935 1.566395 -1.3825207 -0.7719653 -1.5209188
-0.14135945 1.0967469 -0.82279724 -1.664794 ]
[ 0.23995121 -0.3493299 0.5211475 0.06762958 0.40433612 1.0071797
-1.7312989 0.33244786 0.24482548 -1.5655426 ]
[ 0.16492666 -1.3693453 -0.4611454 -0.43240643 0.4816498 -0.6679784
-1.4933153 -0.15073651 -0.5760576 -2.6270504 ]
[ 0.20737278 -2.081945 0.39811417 -0.06598005 -0.41204235 3.3360064
-0.63800156 -0.1312105 0.531367 -1.5185356 ]
[-0.9538085 -0.06938851 -1.1394081 2.7497325 0.02451944 1.7426227
-2.622805 -1.207261 -0.81112486 0.07674086]]
WARNING:tensorflow:From D:/pythontest/picture/test7.py:42: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
a
[[ 0.82497346 1.3807483 -0.7351806 -1.2301304 0.42804122]
[ 2.3935492 0.81827664 -0.5669892 -1.9985942 0.97212964]
[-0.27384508 -1.987736 -0.49118406 -1.6789733 2.4437551 ]
[ 0.46587303 2.7760673 -0.3990933 1.9804771 -2.0173335 ]]
b
b a*b
[[-1.821212 0.9518396 -1.7641987 -2.8179405 -0.775763 ]
[ 4.738205 -1.3682789 0.5364556 -0.64807075 -1.0987829 ]
[ 0.4054848 -7.618403 0.30937684 2.0860884 0.03083204]
[ 0.12988795 -2.7496135 -0.8516452 3.0710206 0.79247415]]
2*a*b
[[ -3.642424 1.9036793 -3.5283973 -5.635881 -1.551526 ]
[ 9.47641 -2.7365577 1.0729111 -1.2961415 -2.1975658 ]
[ 0.8109696 -15.236806 0.6187537 4.172177 0.06166408]
[ 0.2597759 -5.499227 -1.7032903 6.142041 1.5849483 ]]
a/b
[[-3.7369683e-01 2.0029275e+00 -3.0636603e-01 -5.3699535e-01
-2.3617947e-01]
[ 1.2091241e+00 -4.8935688e-01 5.9926069e-01 -6.1634917e+00
-8.6007529e-01]
[ 1.8494189e-01 -5.1862502e-01 7.7983141e-01 1.3513097e+00
1.9369264e+02]
[ 1.6709609e+00 -2.8027756e+00 -1.8702093e-01 1.2771941e+00
5.1353531e+00]]
a%b
[[ -3.642424 1.9036793 -3.5283973 -5.635881 -1.551526 ]
[ 9.47641 -2.7365577 1.0729111 -1.2961415 -2.1975658 ]
[ 0.8109696 -15.236806 0.6187537 4.172177 0.06166408]
[ 0.2597759 -5.499227 -1.7032903 6.142041 1.5849483 ]]
Process finished with exit code 0