-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbasic_op.py
More file actions
125 lines (96 loc) · 3.72 KB
/
basic_op.py
File metadata and controls
125 lines (96 loc) · 3.72 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: UTF-8 -*-
# File Name:01basic_op
# Author : Chen Quan
# Date:2019/2/26
# Description : 基础的运算操作
from __future__ import print_function
import numpy as np
import tensorflow as tf
tf.set_random_seed(2019)
__author__ = 'Chen Quan'
"""
标量运算
"""
a = tf.constant(2)
b = tf.constant(3)
with tf.Session() as sess:
print("a=2, b=3")
print("加法操作: ", sess.run(a + b))
print("加法操作: ", sess.run(tf.add(a, b)))
print("减法操作: ", sess.run(a - b))
print("减法操作: ", sess.run(tf.subtract(a, b)))
print("乘法操作: ", sess.run(a * b))
print("乘法操作: ", sess.run(tf.multiply(a, b)))
print("除法操作: ", sess.run(a / b))
print("除法操作: ", sess.run(tf.divide(a, b)))
"""
矩阵运算
"""
a_numpy = np.random.randn(4, 3)
b_numpy = np.random.randn(3, 5)
c_numpy = np.random.randn(4, 3)
a = tf.constant(a_numpy)
b = tf.constant(b_numpy)
c = tf.constant(c_numpy)
with tf.Session() as sess:
print("a", a)
print("b", b)
print("c", c)
print("=================")
print("矩阵点乘法操作: ", sess.run(a @ b)) # Since python >= 3.5 the @ operator is supported (see PEP 465).
print("矩阵点乘法操作: ", sess.run(tf.matmul(a, b)))
# 两个矩阵对应元素相乘
print("矩阵叉乘法操作: ", sess.run(a * c))
print("矩阵叉乘法操作: ", sess.run(tf.multiply(a, c)))
# 两个矩阵对应元素相除
print("矩阵除法操作: ", sess.run(a / c))
print("矩阵除法操作: ", sess.run(tf.divide(a, c)))
# 两个矩阵对应元素相加
print("矩阵加法操作:", sess.run(a + c))
print("矩阵加法操作:", sess.run(tf.add(a, c)))
# 两个矩阵对应元素相减
print("矩阵加法操作:", sess.run(a - c))
print("矩阵加法操作:", sess.run(tf.subtract(a, c)))
a_numpy = np.random.randn(4, 2)
b_numpy = np.random.randn(4, 1)
c_numpy = np.random.randn(1, 2)
a = tf.constant(a_numpy)
b = tf.constant(b_numpy)
c = tf.constant(c_numpy)
with tf.Session() as sess:
print("a", a)
print("b", b)
print("c", c)
# 叉乘广播 a.shape=[4,2]
print("矩阵叉乘法操作: ", sess.run(a * b)) # b.shape=[4,1]
print("矩阵叉乘法操作: ", sess.run(tf.multiply(a, b)))
print("矩阵叉乘法操作: ", sess.run(a * c)) # c.shape=[1,2]
print("矩阵叉乘法操作: ", sess.run(tf.multiply(a, c)))
# 除法的广播
print("矩阵除法操作: ", sess.run(a / b))
print("矩阵除法操作: ", sess.run(tf.divide(a, b)))
print("矩阵除法操作: ", sess.run(a / c))
print("矩阵除法操作: ", sess.run(tf.divide(a, c)))
# 矩阵加法的广播
print("矩阵加法操作:", sess.run(a + b))
print("矩阵加法操作:", sess.run(tf.add(a, b)))
print("矩阵加法操作:", sess.run(a + c))
print("矩阵加法操作:", sess.run(tf.add(a, c)))
# 减法的广播
print("矩阵加法操作:", sess.run(a - b))
print("矩阵加法操作:", sess.run(tf.subtract(a, b)))
print("矩阵加法操作:", sess.run(a - c))
print("矩阵加法操作:", sess.run(tf.subtract(a, c)))
"""
使用占位符传输数据
"""
# 创建占位符,形状为[1,]
a = tf.placeholder(tf.int8, shape=[1]) # 传入一个8位整数
b = tf.placeholder(tf.int8, shape=[1]) # 传入一个32位浮点数
c = tf.placeholder(tf.float32, shape=[1]) # 传入一个32位浮点数
with tf.Session() as sess:
print("a=", a, "b=", b)
print('a={}'.format(sess.run([a], feed_dict={a: [1]})))
print('b={}'.format(sess.run([b], feed_dict={b: [1.1]})))
print('a*b={}'.format(sess.run([a * b], feed_dict={a: [1], b: [2]})))
# print('a * b={}'.format(sess.run([a * b], feed_dict={b: [1.1]}))) # 报错