My simple 1D CNN only achieve 10% accuracy

Viewed 45

I'm learning 1D CNN and trying to build a simple model to understand it, but my model can only get 10% accuracy.

My problem is very simple. Given 3 random integers, take a sum of it and mod 10.

Example:
input = [4,5,10]
output = (4+5+10)%10 = 9

So here is my full code

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import (Conv1D, MaxPooling1D)
import random
import numpy as np 
from tensorflow.keras.utils import to_categorical

x,y =[],[]
samples = 3000
for i in range(samples):
    a = random.randrange(1, 100)
    b = random.randrange(1, 100)
    c = random.randrange(1, 100)
    
    out = (a+b+c)%10
    x.append([a,b,c])
    
    temp = to_categorical([out],num_classes=10)[0] # one hot encoding
    y.append(temp)
    
x = np.array(x)
x = x/100.0 # normalization
y = np.array(y)
x = x.reshape((x.shape[0], x.shape[1], 1))




model = Sequential()
model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(3,1)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.compile(loss="categorical_crossentropy", optimizer="adam",metrics=["accuracy"])

model.fit(x, y, epochs=100, batch_size=16, verbose=1)

After 100 epochs:

Epoch 99/100
188/188 [==============================] - 0s 2ms/step - loss: 2.2937 - accuracy: 0.1193
Epoch 100/100
188/188 [==============================] - 0s 2ms/step - loss: 2.2936 - accuracy: 0.1257

So did I do something wrong? Or 1D CNN cannot solve this type of problem?

EDIT: So my thinking is: given 3 random integers a,b,c. We can draw them in xy-plane at time step = 1. The coordinates for those numbers are: (a,1), (b,2),(c,3). So our job is to predict the next point in the graph. This will make the problem become a graph problem (time-series) and it will be suitable for 1D CNN. Am I correct?

0 Answers
Related