import numpy as np
def softmax(array):
array = np.array(array)
exp_array = np.exp(array)
exp_sum = np.sum(exp_array)
return exp_array / exp_sum
softmax([1, 3, 5])
softmax([1, 2, 3, 4, 5])
print(softmax([1, 3, 5]))
print(softmax([1, 2, 3, 4, 5]))
import numpy as np
from numpy import log
def categorical_cross_entropy(y, y_hat):
num_elements = len(y)
loss = 0
for i in range(num_elements):
loss += - y[i] * log(y_hat[i])
return loss
y = [0, 0, 1]
y_hat = [0.1, 0.1, 0.8]
loss = categorical_cross_entropy(y, y_hat)
print(loss)
y = [0, 0, 1]
y_hat = [0.1, 0.3, 0.6]
loss = categorical_cross_entropy(y, y_hat)
print(loss)
y = [0, 0, 1]
y_hat = [0.4, 0.5, 0.1]
loss = categorical_cross_entropy(y, y_hat)
print(loss)
import numpy as np
from numpy import log
def binary_cross_entropy(y, y_hat):
loss = -y * log(y_hat) - (1 - y) * log(1 - y_hat)
return loss
binary_cross_entropy(0, 0.01)
binary_cross_entropy(1, 0.99)
binary_cross_entropy(0, 0.3)
binary_cross_entropy(0, 0.8)
print(binary_cross_entropy(0, 0.01))
print(binary_cross_entropy(1, 0.99))
print(binary_cross_entropy(0, 0.3))
print(binary_cross_entropy(0, 0.8))
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical # One-Hot Encoding
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# print(X_train.shape)
# print(X_test.shape)
# print(y_train.shape)
# print(y_test.shape)
# plt.imshow(X_train[0])
# plt.show()
# print(y_train[0])
n_train = X_train.shape[0] # 60000
n_test = X_test.shape[0] # 10000
flatten_size = 28 * 28
# X_train(60000 * 28 * 28)
X_train = X_train.reshape((n_train, flatten_size))
# 0 ~ 255 --> 0 ~ 1
X_train = X_train / 255
# 0 ~ 9 --> One-Hot Encoding
# 3 --> [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
y_train = to_categorical(y_train)
X_test = X_test.reshape((n_test, flatten_size))
X_test = X_test / 255
y_test = to_categorical(y_test)
X_train.shape
# print(X_train.shape)
y_train.shape
# print(y_train.shape)
y_train[0]
# print(y_train[0])
def softmax(x):
exp_x = np.exp(x)
sum_e = np.sum(exp_x, axis=1)
for i in range(x.shape[0]):
exp_x[1, :] = exp_x[i, :] / sum_e[i]
return exp_x
W = np.zeros((784, 10))
b = np.zeros((1, 10))
N = 100
lr = 0.00001
for i in range(N):
det_w = np.zeros((784, 10))
det_b = np.zeros((1, 10))
logits = np.dot(X_train, W) + b
y_hat = softmax(logits)
det_w = np.dot(X_train.T, (y_hat - y_train))
det_b = np.sum((y_hat - y_train), axis=0)
W = W - lr * det_w
b = b - lr * det_b
logits_train = np.dot(X_train, W) + b
y_train_hat = softmax(logits_train)
y_hat = np.argmax(y_train_hat, axis=1)
y = np.argmax(y_train, axis=1)
count = 0
for i in range(len(y_hat)):
if y[i] == y_hat[i]:
count += 1
print('Accuracy On Training Set Is {}%'.format(round(count / n_train, 2) * 100))
logits_test = np.dot(X_test, W) + b
y_test_hat = softmax(logits_test)
y_hat = np.argmax(y_test_hat, axis=1)
y = np.argmax(y_test, axis=1)
count = 0
for i in range(len(y_hat)):
if y[i] == y_hat[i]:
count += 1
print('Accuracy On Testing Set Is {}%'.format(round(count / n_test, 2) * 100))
import numpy as np
import pandas as pd
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import RMSprop
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train / 255
X_test = X_test / 255
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(units=10, activation='softmax'))
# model.summary()
model.compile(loss='categorical_crossentropy',
metrics=['accuracy'],
optimizer=RMSprop())
model.fit(X_train, y_train, epochs=10, batch_size=64)
loss, accuracy = model.evaluate(X_test, y_test)
print(accuracy)
import numpy as np
import pandas as pd
from tensorflow.keras.datasets import fashion_mnist
import matplotlib.pyplot as plt
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import RMSprop
(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
X_train = X_train / 255
X_test = X_test / 255
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# plt.imshow(X_train[0])
# plt.show()
# plt.imshow(X_train[100])
# plt.show()
# X_train.shape
# X_test.shape
# print(X_train.shape)
# print(X_test.shape)
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(units=10, activation='softmax'))
# model.summary()
model.compile(loss='categorical_crossentropy',
metrics=['accuracy'],
optimizer=RMSprop())
model.fit(X_train, y_train, epochs=10, batch_size=64)
loss, accuracy = model.evaluate(X_test, y_test)
print(accuracy)
完整代码已上传至Github,各位下载时麻烦给个follow和star,感谢!
链接:SoftmaxMulti-ClassClassifier 多分类器