-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensorflow_analysis.py
More file actions
58 lines (44 loc) · 1.9 KB
/
tensorflow_analysis.py
File metadata and controls
58 lines (44 loc) · 1.9 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
from __future__ import absolute_import, division, print_function, unicode_literals
import compile_training_data
import tensorflow as tf
from tensorflow import keras
import tensorflow_datasets as tfds
import tensorflow_hub as hub
import numpy as np
# print("Version: ", tf.__version__)
# print("Eager mode: ", tf.executing_eagerly())
# print("Hub version: ", hub.__version__)
# print("GPU is", "available" if tf.config.experimental.list_physical_devices("GPU") else "NOT AVAILABLE")
full_data = compile_training_data.get_dataset()
train_tuple = full_data[0]
test_tuple = full_data[1]
# train_dataset = tf.data.Dataset.from_tensor_slices((train_tuple[0], train_tuple[1]))
lens = []
# Prints and analyses data
# for i in range(len(train_tuple[0])):
# lens.append(len(train_tuple[0][i]))
# print(train_tuple[0][i])
# lens = np.array(lens)
# print(lens.max(), lens.min(), lens.mean(), len(lens > 300))
# print('\n\n\n', type(train_tuple[0][10]), "-->", type(train_tuple[1]))
# Builds he tensorflow datasets
train_data = tf.data.Dataset.from_tensor_slices(train_tuple)
test_data = tf.data.Dataset.from_tensor_slices(test_tuple)
# print(tf.shape(train_data))
# print(tf.shape(test_data))
embedding = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
model = keras.Sequential()
model.add(keras.layers.Embedding(4708, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.summary()
model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy'])
history = model.fit(train_data.shuffle(583).batch(32),
train_tuple[1],
epochs=100,
batch_size=32,
validation_data=test_data,
verbose=1)
results = model.evaluate(test_data, test_tuple[1])
print(results)