コード例 #1
0
import matplotlib.pyplot as plt
from costFunction import crossEntropyVectorized
from Data_Process import get_binaryData, get_data
from logisticRegression import classification_rate, sigmoid, forward

N = 50
D = 50

X = (np.random.random((N, D)) - 0.5) * 10
W = np.array([1, 0.5, -0.5] + [0] * (D - 3))
Y = np.round(sigmoid(X.dot(W) + np.random.randn(N) * 0.5))

costs = []
W_hat = np.random.randn(D)
b = 0
lr = 0.001
lamba = 5
for i in range(1000):
    Y_hat = forward(X, W_hat, b)
    delta = Y_hat - Y
    W_hat -= lr * (X.T.dot(delta) + lamba * np.sign(W_hat))
    cost = crossEntropyVectorized(Y, Y_hat) + lamba * np.mean(np.abs(W_hat))
    costs.append(cost)
plt.plot(costs)
plt.show()

plt.plot(W, label="Original W")
plt.plot(W_hat, label="L1 W")
plt.legend()
plt.savefig("images/L1 regularization logistic")
plt.show()
コード例 #2
0
from Data_Process import get_binaryData, get_data
from logisticRegression import classification_rate, sigmoid, forward

Xtrain, Ytrain, Xtest, Ytest = get_binaryData()
N, D = Xtrain.shape
W = np.random.randn(D)
b = 0

trainingCosts = []
testingCosts = []
lr = 0.001
lamba = 0.1
for t in range(1000):
    Y_hat = forward(Xtrain, W, b)
    Ytest_hat = forward(Xtest, W, b)
    trainingCosts.append(crossEntropyVectorized(Ytrain, Y_hat))
    testingCosts.append(crossEntropyVectorized(Ytest, Ytest_hat))
    if t % 200 == 0:
        print(trainingCosts[-1], testingCosts[-1], t)
    W -= lr * (Xtrain.T.dot(Y_hat - Ytrain) + lamba * W.dot(W))
    b -= lr * (Y_hat - Ytrain).sum()

print("Final Traing classification rate ",
      classification_rate(Ytrain, np.round(Y_hat)))
print("Final Testing classification rate ",
      classification_rate(Ytest, np.round(Ytest_hat)))

legend1 = plt.plot(trainingCosts)
legend2 = plt.plot(testingCosts)
plt.legend(legend1, legend2)
plt.show()
コード例 #3
0
theta = 2 * np.pi * np.random.random(N // 2)
X1 = np.concatenate([[X1 * np.cos(theta)], [X1 * np.sin(theta)]]).T
theta = 2 * np.pi * np.random.random(N // 2)
X2 = np.concatenate([[X2 * np.cos(theta)], [X2 * np.sin(theta)]]).T
X = np.concatenate([X1, X2])
T = np.array([0] * (N // 2) + [1] * (N // 2))
plt.scatter(X[:, 0], X[:, 1], c=T)
plt.show()
plt.savefig("images/donus")

bias = np.ones((N, 1))
r = np.sqrt((X * X).sum(axis=1)).reshape(-1, 1)
X = np.concatenate((bias, r, X), axis=1)
# print(X.shape)
W = np.random.randn(D + 2)
Y = sigmoid(X.dot(W))
lr = 0.001
costs = []
for i in range(3000):
    cost = crossEntropyVectorized(T, Y)
    costs.append(cost)
    if i % 500 == 0:
        print(cost)
    W += lr * (X.T.dot(T - Y) - 0.1 * W)
    Y = sigmoid(X.dot(W))

plt.plot(costs)
plt.title("DoughNut logistic")
plt.savefig("images/DoughNut logistic")
plt.show()
print(classification_rate(T, np.round(Y)))