forked from AIspeakeryhl/tensorflow-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf2.0
68 lines (52 loc) · 1.49 KB
/
tf2.0
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
import tensorflow as tf
tf.__version__
!pip install tensorflow
#张量
#常量
a = tf.constant(2, name='a')
b = tf.constant(3, name='b')
x = tf.add(a, b)
print(x)
print(a+b)
a.get_shape()
a.numpy()
#变量
s = tf.Variable(2, name="scalar")
m = tf.Variable([[0, 1], [2, 3]], name="matrix")
W = tf.Variable(tf.zeros([784,10]))
s.assign(3)
s.assign_add(3)
class MyModule(tf.Module):
def __init__(self):
self.v0 = tf.Variable(1.0)
self.vs = [tf.Variable(x) for x in range(10)]
m = MyModule()
m.variables
#tf.data
dataset = tf.data.Dataset.from_tensors([1,2,3,4,5])
for element in dataset:
print(element.numpy())
it = iter(dataset)
print(next(it).numpy())
dataset = tf.data.Dataset.from_tensor_slices([1,2,3,4,5])
for element in dataset:
print(element.numpy())
it = iter(dataset)
print(next(it).numpy())
features = tf.data.Dataset.from_tensor_slices([1,2,3,4,5])
labels = tf.data.Dataset.from_tensor_slices([6,7,8,9,10])
dataset = tf.data.Dataset.zip((features,labels))
for element in dataset3:
print(element)
inc_dataset = tf.data.Dataset.range(100)
dec_dataset = tf.data.Dataset.range(0, -100, -1)
dataset = tf.data.Dataset.zip((inc_dataset, dec_dataset))
batched_dataset = dataset.batch(4)
for batch in batched_dataset.take(4):
print([arr.numpy() for arr in batch])
shuffle_dataset = dataset.shuffle(buffer_size=10)
for element in shuffle_dataset:
print(element)
shuffle_dataset = dataset.shuffle(buffer_size=100)
for element in shuffle_dataset:
print(element)