手寫softmax迴歸

from scipy.io import arff
import numpy as np
import pandas as pd

radius = 4  # search radius


def distance(point1, point2):
    return np.sqrt(np.sum([(point1[i] - point2[i]) ** 2 for i in range(4)]))


iris = arff.loadarff('../dataset/iris.arff')
df = pd.DataFrame(iris[0])
length = df.shape[0]
classes = list(set(df['class']))
classes_length = len(classes)
classes_dict = dict()
for i in range(classes_length):
    classes_dict[classes[i]] = i
for i in range(length):
    df.iloc[i, 4] = classes_dict[df.iloc[i, 4]]
# df = df.sample(frac=1)  # shuffle data randomly
train_data = df.iloc[0:100]
test_data = df.iloc[100:]
train_length = train_data.shape[0]
test_length = test_data.shape[0]
accuracy = 0


def my_iter(data, batch):
    X = []
    y = []
    length = data.shape[0]
    start = 0
    while length - batch > 0:
        X = data.iloc[start:start + batch, 0:4]
        y = data.iloc[start:start + batch, 4]
        yield X, y
        start += batch
        length -= batch
    X = data.iloc[start:, 0:4]
    y = data.iloc[start:, 4]
    yield X, y


model_class_4 = np.random.rand(classes_length, 4)
learning_rate = 0.1
batch = 30
lam = 1e-4


# predict
def h(x):
    global model_class_4
    y_predict = []
    batch = x.shape[0]
    for i in range(batch):
        x_i = np.array(x.iloc[i])
        exp_array = np.sum(np.exp(model_class_4 * x_i), axis=1)  # 3
        y_predict_i = exp_array / np.sum(exp_array, axis=0)
        y_predict.append(y_predict_i)
    y_predict = np.array(y_predict)  # batch 3
    return y_predict


def one(x):
    ones = np.ones(classes_length)
    ones[x] = 1
    return ones


# loss function
def J(x, y_true, batch):
    global model_class_4
    loss_sum = 0
    derivative_loss_sum = 0
    derivative_lam_sum = 0
    y_predict = h(x)
    lam_sum = 0
    lam_sum_2 = 0
    for i in range(batch):
        y_true_i = y_true.iloc[i]
        one_i = one(y_true_i)
        loss_sum += np.sum(one_i * np.log(y_predict[y_true_i] / np.sum(y_predict)))
        lam_sum += np.sum(model_class_4)
        lam_sum_2 += np.sum(model_class_4 ** 2)
        derivative_loss_sum += (1 - y_predict[i][y_true_i])
        derivative_lam_sum += model_class_4[y_true_i]
    return -(1 / batch) * derivative_loss_sum + lam * derivative_lam_sum, -(
            1 / batch) * loss_sum + lam / 2 * lam_sum_2


for i, (X, y) in enumerate(my_iter(train_data, batch)):
    y_predict = np.array([np.argmax(y_p) for y_p in h(X)])
    delta, loss = J(X, y, X.shape[0])
    print('loss ', loss)
    print('acc ', np.sum(y_predict == y) / X.shape[0])
    model_class_4 -= learning_rate * delta
print('test accuracy')
y_predict_test = np.array([np.argmax(y_p) for y_p in h(test_data.iloc[:, 0:4])])
print('acc %.2f' % (np.sum(y_predict_test == test_data.iloc[:, 4]) / test_length))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章