I am trying to replicate a multiclass classification problem of a paper I am reading. So they provided with the exact matrices and bias vector values and have proved in the paper why there will 100% accuracy. My issues are:
- I am getting around 75% accuracy, instead of 100.
- The accuracy is changing for each epoch, which shouldn't happen as my weights are fixed and non trainable.
- I passed the training data again individually in "model.predict()" and compared it with the correct predictions, using if-else statement, there i got 0 mismatches or in other words, the training data is getting classified correctly, but accuracy is shown to be 75% as i said earlier.
Here is the snippet of my code:
W1 = np.array([ [0,1,-2,1], [1, -2, 1, 0], [0,-1, 2, -1], [-1,2,-1,0], ]) W1 = W1.T W2 = np.array([ [-1, 1, -1,1], [1,-1,1,-1] ]) W2 = W2.T W3 = np.array([ [1,0], [0,1], ]) W3 = W3.T model = Sequential([ tf.keras.Input(shape=(4,)), Dense(4, activation='relu', kernel_initializer=tf.keras.initializers.Constant(W1), bias_initializer=tf.keras.initializers.Zeros(), trainable=False), Dense(2, activation='relu', kernel_initializer=tf.keras.initializers.Constant(W2), bias_initializer=tf.keras.initializers.Zeros(), trainable=False), Dense(2, activation='softmax', kernel_initializer=tf.keras.initializers.Constant(W3), bias_initializer=tf.keras.initializers.Zeros(), trainable=False, name='dense_2'), ]) model.compile( loss=tf.keras.losses.SparseCategoricalCrossentropy(), # Use sparse labels (integer form) optimizer=tf.keras.optimizers.Adam(0.01), metrics=['sparse_categorical_accuracy'] ) model.summary() history = model.fit( X_train, r, # Sparse labels (integer form) epochs=50, verbose=1 Here I am generating and passing the training data, which is a 4*1 vector that needs to be mapped to a class 0 or 1.
for k in range(s-3): X = x[k:k+4] for i in range(4): if (X[i]>0.5): Y[i]=1 elif (X[i]<-0.5): Y[i]=-1 elif(X[i] == 0): Y[i]=0 else: Y[i] = np.sin(1/X[i]) X_test = Y.reshape(1, -1) Y_test = np.array([r[k]]) predictions[k] = np.argmax(model.predict(X_test)) # Get the class with highest probability temp = 0 for i in range(len(r)): if r[i] != predictions[i]: temp += 1 # More concise way to increment print(i) print(f"Total mismatches: {temp}") The following is the output:
16/16 ━━━━━━━━━━━━━━━━━━━━ 2s 29ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7555 Epoch 2/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 1s 15ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7378 Epoch 3/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7542 Epoch 4/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7465 Epoch 5/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 11ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7733 Epoch 6/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 21ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7469 Epoch 7/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 16ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7343 Epoch 8/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 1s 13ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7215 Epoch 9/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 1s 14ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7699 Epoch 10/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7491 Epoch 11/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7632 Epoch 12/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7369 Epoch 13/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7303 Epoch 14/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7539 Epoch 15/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7530 Epoch 16/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7466 Epoch 17/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7461 Epoch 18/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7393 Epoch 19/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7397 Epoch 20/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7583 Epoch 21/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7446 Epoch 22/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7507 Epoch 23/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7242 Epoch 24/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7238 Epoch 25/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7826 Epoch 26/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7471 Epoch 27/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7477 Epoch 28/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7350 Epoch 29/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7578 Epoch 30/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7669 Epoch 31/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7306 Epoch 32/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7580 Epoch 33/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7495 Epoch 34/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7416 Epoch 35/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7346 Epoch 36/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7346 Epoch 37/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7420 Epoch 38/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7570 Epoch 39/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7490 Epoch 40/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7414 Epoch 41/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7496 Epoch 42/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7323 Epoch 43/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7517 Epoch 44/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7747 Epoch 45/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7309 Epoch 46/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7513 Epoch 47/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7577 Epoch 48/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7466 Epoch 49/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7546 Epoch 50/50 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.7784 And the output for mismatches is
Total mismatches: 0 Where am I going wrong?