-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathtutorial2-tensorbasics.py
82 lines (56 loc) · 1.25 KB
/
tutorial2-tensorbasics.py
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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
# Initialization
x = tf.constant(4, shape=(1, 1), dtype=tf.float32)
print(x)
x = tf.constant([[1, 2, 3], [4, 5, 6]], shape=(2, 3))
print(x)
x = tf.eye(3)
print(x)
x = tf.ones((4, 3))
print(x)
x = tf.zeros((3, 2, 5))
print(x)
x = tf.random.uniform((2, 2), minval=0, maxval=1)
print(x)
x = tf.random.normal((3, 3), mean=0, stddev=1)
print(tf.cast(x, dtype=tf.float64))
# tf.float (16,32,64), tf.int (8, 16, 32, 64), tf.bool
x = tf.range(9)
x = tf.range(start=0, limit=10, delta=2)
print(x)
# Math
x = tf.constant([1, 2, 3])
y = tf.constant([9, 8, 7])
z = tf.add(x, y)
z = x + y
z = tf.subtract(x, y)
z = x - y
z = tf.divide(x, y)
z = x / y
z = tf.multiply(x, y)
z = x * y
z = tf.tensordot(x, y, axes=1)
z = x ** 5
x = tf.random.normal((2, 3))
y = tf.random.normal((3, 2))
z = tf.matmul(x, y)
z = x @ y
x = tf.random.normal((2, 2))
# Indexing
x = tf.constant([0, 1, 1, 2, 3, 1, 2, 3])
print(x[:])
print(x[1:])
print(x[1:3])
print(x[::2])
print(x[::-1])
indices = tf.constant([0, 3])
x_indices = tf.gather(x, indices)
x = tf.constant([[1, 2], [3, 4], [5, 6]])
print(x[0, :])
print(x[0:2, :])
# Reshaping
x = tf.range(9)
x = tf.reshape(x, (3, 3))
x = tf.transpose(x, perm=[1, 0])