Reputation: 133
The assignment is recognizing handwritten digits by BP neural network. I have tried add hidden layer, increase units and change activation, momentum, but the result shows:
Test Acc: 0.11142061281337047
the splitting dataset X:(1797,64) Y(1797,)
np.random.shuffle(X)
np.random.shuffle(y)
offset1=int(len(X)*0.6)
offset2=int(len(X)*0.8)
offset3=len(X)-1
X_train, y_train = X[0:offset1,:], y[0:offset1]
X_valid, y_valid = X[offset1:offset2,:], y[offset1:offset2]
X_test, y_test = X[offset2:offset3,:], y[offset2:offset3]
And my neural network:
def create_network():
# TODO
net = tf.keras.Sequential()
net.add(tf.keras.layers.Flatten())
net.add(tf.keras.layers.Dense(64, activation='relu'))
net.add(tf.keras.layers.Dense(64, activation='relu'))
net.add(tf.keras.layers.Dense(10, activation='softmax'))
net.compile(
optimizer='adam',
#tf.optimizers.SGD(learning_rate=0.01, momentum=0.0, nesterov=False),
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
return net
def train_network(network,
X_train,
y_train,
X_valid,
y_valid,
n_epoch=32,
batch_size=64):
n_iter = 0
network.fit(x=X_train, y=y_train, epochs=n_epoch, validation_data=(X_valid,y_valid))
#opt = tf.optimizers.SGD(learning_rate=0.01, momentum=0.0, nesterov=False) #SGD=Stochastic Gradient Descent
net = create_network()
train_network(net, X_train, y_train, X_valid, y_valid)
# evaluate on test set:
y_test_pred = net(X_test)
y_test_pred = np.argmax(y_test_pred.numpy(), axis=1)
print("Test Acc:", accuracy_score(y_true=y_test, y_pred=y_test_pred))
Upvotes: 0
Views: 92
Reputation: 800
Assuming your model and training are correct, one issue I see is, You are shuffling x
& y
separately. np.random.shuffle
will modify both arrays in place. After you do that the training points will no longer be correctly matched with the correct labels. Hence, your training and prediction won't work as expected. You can do something like,
# shuffle the ids
shuffled_ids = np.arange(len(x)))
np.random.shuffle(shuffled_ids)
# index x & y with the same shuffled numbers,
# so the correct relationship is maintained
shuf_x = x[shuffled_ids]
shuf_y = y[shuffled_ids]
Upvotes: 0
Reputation: 36
@NebiyouTen, I agree with what you said. But in fact np.random.shuffle
doesn't return anything. Maybe you need to do some modifications:
shuffled_ids = np.arange(len(x))
np.random.shuffle(shuffled_ids)
shuf_x = x[shuffled_ids]
shuf_y = y[shuffled_ids]
And here is an alternative way to shuffle the data:
random_state = np.random.get_state()
np.random.shuffle(x)
np.random.set_state(random_state)
np.random.shuffle(y)
Upvotes: 1