import numpy as np
import tensorflow as tf

import tensorflow_hub as hub
import tensorflow_datasets as tfds

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 AVALIABLE')

# download dataset

train_data, validation_data, test_data = tfds.load(name = 'imdb_reviews', split = ('train[:60%]', 'train[60%:]', 'test'), as_supervised = True)

train_examples_batch, train_labels_batch = next(iter(train_data.batch(10)))
print(train_examples_batch)
print(train_labels_batch)

# build model

## embedding layer
embedding  = "https://tfhub.dev/google/nnlm-en-dim50/2"
hub_layer = hub.KerasLayer(embedding, input_shape = [], dtype = tf.string, trainable =True)
print(hub_layer(train_examples_batch[:3]))

## dense layer
model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation = 'relu'))
model.add(tf.keras.layers.Dense(1))

print(model.summary())

# loss function and optimizer

model.compile(optimizer = 'adam', loss = tf.keras.losses.BinaryCrossentropy(from_logits = True), metrics = ['accuracy'])

# train model

history = model.fit(train_data.shuffle(10000).batch(512), epochs = 10, 
	validation_data = validation_data.batch(512), verbose = 1)

# evaluate model

results = model.evaluate(test_data.batch(512), verbose = 2)

for name, value in zip(model.metrics_names, results):
	print('%s: %.3f' % (name, value))


