-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
88 lines (78 loc) · 2.24 KB
/
test.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
83
84
85
86
87
88
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist
from tensorflow.python.client import device_lib
import os
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# def test():
# # Initialization of tensors
# w = tf.constant(4, shape=(1, 1), dtype=tf.float32)
# x = tf.constant(5, shape=(3, 3), dtype=tf.float32)
# y = tf.zeros((2, 3))
# z = tf.eye(3)
# a = tf.random.normal((3, 3), mean=0, stddev=1)
# b = tf.random.uniform((7, 15), minval=0, maxval=1)
#
# # Mathematical operations
#
# result = tf.subtract(x, a)
# result2 = tf.multiply(x, a)
# result3 = tf.tensordot(x, a, axes=1)
# print(result)
# print(result2)
# print(result3)
# print(b * 10)
#
# # Indexing
#
# val = tf.constant([0, 1, 1, 2, 3, 1, 2, 3])
# print(val[:])
# print(val[1:])
# print(val[1:3])
# print(val[::2])
# i = tf.constant([0, 3])
# indy_boi = tf.gather(val, i)
# print(indy_boi)
# x = tf.constant([[1, 2],
# [3, 4],
# [5, 6]])
# print(x[0, :])
# print(x[0:2, :])
#
# # Reshaping
#
# x = tf.range(9)
# print(x)
#
# x = tf.reshape(x, (3, 3))
# print(x)
#
# x = tf.transpose(x, perm=[1, 0])
# print(x)
def neural_test():
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape)
print(y_train.shape)
x_train = x_train.reshape(-1, 28*28).astype("float32") / 255.0
x_test = x_test.reshape(-1, 28*28).astype("float32") / 255.0
model = keras.Sequential(
[
layers.Dense(512, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(10),
]
)
model.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=keras.optimizers.Adam(lr=0.001),
metrics=["accuracy"],
)
model.fit(x_train, y_train, batch_size=32, epochs=5, verbose=2)
model.evaluate(x_test, y_test, batch_size=32, verbose=2)
# neural_test()
gpu = tf.test.gpu_device_name()
print('TensorFlow running on GPU: {}'.format(gpu))
print(tf.config.list_physical_devices('GPU'))
device_lib.list_local_devices()
print(tf.__version__)