Using Keras to predict whether two numbers have the same "oddness" using an embedding, am I on the right track?

170 views Asked by At

I'm new to machine learning, and I'm working on a simple example as practice. I generate pairs of numbers, and assign the label '1' if both values are even or odd, and the label '0' if one is even and the other is odd.

My model sometimes gets to around ~75%, but I'm fairly sure it's simply memorizing which pairs of numbers lead to 1 and which lead to 0. I want the model to learn that there are two categories of numbers, for example, knowing that [1, 4] = 0 and [1, 6] = 0, therefor 4 and 6 are in the same category.

Am I on the right track? Is this even a reasonable problem to solve with ML?

Here is my code:

num_examples = 500000
input_dim = 300

# Generate the randomized training data
data = (input_dim * numpy.random.random((num_examples, 2))).astype(int)
labels = []

# Generate the correct labels for the training data
for example in data:
    if example[0] % 2 == example[1] % 2:
        labels.append([1, 0])
    else:
        labels.append([0, 1])


model = Sequential()
model.add(Embedding(input_dim, 70, input_length=2))
model.add(Flatten())
model.add(Dense(20))
model.add(Dense(2, activation='sigmoid'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(data, labels, nb_epoch=900, batch_size=10000)
1

There are 1 answers

0
Yahia Zakaria On

This model won't be able to generalize to unseen numbers since the embedding layer assigns for each number a feature vector. if a number is not in any training example, it will never get updated and will keep its initial random value. I think supplying the numbers in binary form (e.g. supply '9' as 8 inputs of value 00001001) will help. anyway this is a dirty hack in my opinion since all the network is required to do now is to learn to detect when the last bit of each input are equal.