Multiple output values - predictions nowhere near what I was expecting

Hi,

I’m trying to get the sentiment + polarity from some sentences using the code below. I want to use the universal multilingual vectors to get the vectors so that I can explore training on one language and applying to multiple languages.

I know it’s a very small data set for the example, but I was expecting that running model.predict on a sentence it was trained on would give the correct results (or very similar). The last couple of lines below show what I was expecting and what I’m getting.

Does anyone know what I’m doing wrong?

Thanks,
Huw

import tensorflow as tf
import tensorflow_hub as hub
import numpy as np

Load the universal sentence encoder multilingual

module = hub.load(‘TensorFlow Hub’)

def get_sentences_vectors(sentences, batch: int = 1000):
lower = 0
upper = batch
sent_vectors = module.signatures’question_encoder’[“outputs”]
while upper < len(sentences):
lower += batch
upper += batch
print(lower)
if sentences:
sent_vectors = np.concatenate(
(sent_vectors, module.signatures’question_encoder’[“outputs”]))
return sent_vectors

sentences = [
“I really like this coat”,
“I really hate this coat”,
“I really like this jacket”,
“I really hate this jacket”,
“I really like this cat”,
“I really hate this cat”,
“I really like this dog”,
“I really hate this dog”,
“I really like this spaniel”,
“I really hate this spaniel”,
“I really like this terrier”,
“I really hate this terrier”,
“I really like this chair”,
“I really hate this chair”,
“I really like this beer”,
“I really hate this beer”,
“I really like this food”,
“I really hate this food”,
“I really like this chicken”,
“I really hate this chicken”,
]

subjectivity_scores = [0.2, 0.9, 0.2, 0.9, 0.2, 0.9, 0.2, 0.9, 0.2, 0.9,
0.2, 0.9, 0.2, 0.9, 0.2, 0.9, 0.2, 0.9, 0.2, 0.9]
polarity_scores = [0.2, -0.8, 0.2, -0.8, 0.2, -0.8, 0.2, -0.8, 0.2, -0.8,
0.2, -0.8, 0.2, -0.8, 0.2, -0.8, 0.2, -0.8, -0.2, -0.8]

sentences_vectors = get_sentences_vectors(sentences)
sentence_vector_size = 512

Split the dataset into training, validation, and test sets

train_size = int(0.8 * len(sentences))
val_size = int(0.1 * len(sentences))
test_size = len(sentences) - train_size - val_size

train_sentences = np.asarray(sentences_vectors[:train_size])
train_subjectivity = np.asarray(subjectivity_scores[:train_size])
train_polarity = np.asarray(polarity_scores[:train_size])

val_sentences = np.asarray(sentences_vectors[train_size:train_size + val_size])
val_subjectivity = np.asarray(subjectivity_scores[train_size:train_size + val_size])
val_polarity = np.asarray(polarity_scores[train_size:train_size + val_size])

test_sentences = np.asarray(sentences_vectors[train_size + val_size:])
test_subjectivity = np.asarray(subjectivity_scores[train_size + val_size:])
test_polarity = np.asarray(polarity_scores[train_size + val_size:])

Define the model architecture

model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation=‘relu’, input_shape=(sentence_vector_size,)),
tf.keras.layers.Dense(32, activation=‘relu’),
tf.keras.layers.Dense(2, activation=‘linear’) # 2 outputs: subjectivity and polarity
])

Compile the model

model.compile(optimizer=‘adam’, loss=‘mse’)

Train the model

model.fit(train_sentences, [train_subjectivity, train_polarity], epochs=10, batch_size=32,
validation_data=(val_sentences, [val_subjectivity, val_polarity]))

Evaluate the model on the test set

loss = model.evaluate(train_sentences, [train_subjectivity, train_polarity])

Extract subjectivity and polarity predictions

vectors = get_sentences_vectors([“I really hate this coat”]).numpy()
predictions = model.predict(vectors)
subjectivity_predictions = predictions[0][0]
polarity_predictions = predictions[0][1]
print(f"subjectivity_predictions : {subjectivity_predictions}“) # Thought this would return (0.9) - gives 0.31
print(f"polarity_predictions : {polarity_predictions}”) # Thought this would return (-0.8) - gives 0.35