Microsoft-cognitive-toolkit-logistic-regression-model

提供:Dev Guides
移動先:案内検索

CNTK-ロジスティック回帰モデル

この章では、CNTKでロジスティック回帰モデルを構築する方法について説明します。

ロジスティック回帰モデルの基礎

最も単純なML手法の1つであるロジスティック回帰は、特にバイナリ分類の手法です。 つまり、予測する変数の値が2つのカテゴリ値のいずれかになる可能性がある状況で予測モデルを作成します。 ロジスティック回帰の最も単純な例の1つは、人の年齢、声、髪の毛などに基づいて、その人が男性か女性かを予測することです。

別の例を使用して、ロジスティック回帰の概念を数学的に理解しましょう-

ローン申請の信用力を予測したいとします。申請者 debt、収入 および 信用格付け に基づいて、0は拒否、1は承認を意味します。 債務はX1、収入はX2、信用格付けはX3で表します。

ロジスティック回帰では、すべての特徴について w で表される重み値と b で表される単一のバイアス値を決定します。

いま、

X1 = 3.0
X2 = -2.0
X3 = 1.0

そして、私たちは次のように重みとバイアスを決定すると仮定します-

W1 = 0.65, W2 = 1.75, W3 = 2.05 and b = 0.33

ここで、クラスを予測するために、次の式を適用する必要があります-

Z = (X1*W1)+(X2*W2)+(X3+W3)+b
i.e. Z = (3.0)*(0.65) + (-2.0)*(1.75) + (1.0)*(2.05) + 0.33
= 0.83

次に、* P = 1.0/(1.0 + exp(-Z))*を計算する必要があります。 ここで、exp()関数はオイラーの数です。

P = 1.0/(1.0 + exp(-0.83)
= 0.6963

P値は、クラスが1である確率と解釈できます。 P <0.5の場合、予測はクラス= 0です。それ以外の場合、予測(P> = 0.5)はクラス= 1です。

重みとバイアスの値を決定するには、既知の入力予測値と既知の正しいクラスラベル値を持つトレーニングデータのセットを取得する必要があります。 その後、重みとバイアスの値を見つけるために、アルゴリズム(通常は勾配降下法)を使用できます。

LRモデルの実装例

このLRモデルでは、次のデータセットを使用します-

1.0, 2.0, 0
3.0, 4.0, 0
5.0, 2.0, 0
6.0, 3.0, 0
8.0, 1.0, 0
9.0, 2.0, 0
1.0, 4.0, 1
2.0, 5.0, 1
4.0, 6.0, 1
6.0, 5.0, 1
7.0, 3.0, 1
8.0, 5.0, 1

このLRモデルの実装をCNTKで開始するには、まず次のパッケージをインポートする必要があります-

import numpy as np
import cntk as C

プログラムは次のようにmain()関数で構成されています-

def main():
print("Using CNTK version = " + str(C.__version__) + "\n")

今、私たちは次のようにトレーニングデータをメモリにロードする必要があります-

data_file = ".\\dataLRmodel.txt"
print("Loading data from " + data_file + "\n")
features_mat = np.loadtxt(data_file, dtype=np.float32, delimiter=",", skiprows=0, usecols=[0,1])
labels_mat = np.loadtxt(data_file, dtype=np.float32, delimiter=",", skiprows=0, usecols=[2], ndmin=2)

ここで、トレーニングデータと互換性のあるロジスティック回帰モデルを作成するトレーニングプログラムを作成します-

features_dim = 2
labels_dim = 1
X = C.ops.input_variable(features_dim, np.float32)
y = C.input_variable(labels_dim, np.float32)
W = C.parameter(shape=(features_dim, 1)) # trainable cntk.Parameter
b = C.parameter(shape=(labels_dim))
z = C.times(X, W) + b
p = 1.0/(1.0 + C.exp(-z))
model = p

今、私たちは次のようにラーナーとトレーナーを作成する必要があります-

ce_error = C.binary_cross_entropy(model, y) # CE a bit more principled for LR
fixed_lr = 0.010
learner = C.sgd(model.parameters, fixed_lr)
trainer = C.Trainer(model, (ce_error), [learner])
max_iterations = 4000

LRモデルのトレーニング

LRモデルを作成したら、次に、トレーニングプロセスを開始します。

np.random.seed(4)
N = len(features_mat)
for i in range(0, max_iterations):
row = np.random.choice(N,1) # pick a random row from training items
trainer.train_minibatch({ X: features_mat[row], y: labels_mat[row] })
if i % 1000 == 0 and i > 0:
mcee = trainer.previous_minibatch_loss_average
print(str(i) + " Cross-entropy error on curr item = %0.4f " % mcee)

今、次のコードの助けを借りて、モデルの重みとバイアスを出力できます-

np.set_printoptions(precision=4, suppress=True)
print("Model weights: ")
print(W.value)
print("Model bias:")
print(b.value)
print("")
if __name__ == "__main__":
main()

ロジスティック回帰モデルのトレーニング-完全な例

import numpy as np
import cntk as C
   def main():
print("Using CNTK version = " + str(C.__version__) + "\n")
data_file = ".\\dataLRmodel.txt" # provide the name and the location of data file
print("Loading data from " + data_file + "\n")
features_mat = np.loadtxt(data_file, dtype=np.float32, delimiter=",", skiprows=0, usecols=[0,1])
labels_mat = np.loadtxt(data_file, dtype=np.float32, delimiter=",", skiprows=0, usecols=[2], ndmin=2)
features_dim = 2
labels_dim = 1
X = C.ops.input_variable(features_dim, np.float32)
y = C.input_variable(labels_dim, np.float32)
W = C.parameter(shape=(features_dim, 1)) # trainable cntk.Parameter
b = C.parameter(shape=(labels_dim))
z = C.times(X, W) + b
p = 1.0/(1.0 + C.exp(-z))
model = p
ce_error = C.binary_cross_entropy(model, y) # CE a bit more principled for LR
fixed_lr = 0.010
learner = C.sgd(model.parameters, fixed_lr)
trainer = C.Trainer(model, (ce_error), [learner])
max_iterations = 4000
np.random.seed(4)
N = len(features_mat)
for i in range(0, max_iterations):
row = np.random.choice(N,1) # pick a random row from training items
trainer.train_minibatch({ X: features_mat[row], y: labels_mat[row] })
if i % 1000 == 0 and i > 0:
mcee = trainer.previous_minibatch_loss_average
print(str(i) + " Cross-entropy error on curr item = %0.4f " % mcee)
np.set_printoptions(precision=4, suppress=True)
print("Model weights: ")
print(W.value)
print("Model bias:")
print(b.value)
if __name__ == "__main__":
  main()

出力

Using CNTK version = 2.7
1000 cross entropy error on curr item = 0.1941
2000 cross entropy error on curr item = 0.1746
3000 cross entropy error on curr item = 0.0563
Model weights:
[-0.2049]
   [0.9666]]
Model bias:
[-2.2846]

訓練されたLRモデルを使用した予測

LRモデルがトレーニングされると、次のように予測に使用できます-

まず、評価プログラムはnumpyパッケージをインポートし、上記で実装したトレーニングプログラムと同じ方法で、トレーニングデータを特徴マトリックスとクラスラベルマトリックスに読み込みます-

import numpy as np
def main():
data_file = ".\\dataLRmodel.txt" # provide the name and the location of data file
features_mat = np.loadtxt(data_file, dtype=np.float32, delimiter=",",
skiprows=0, usecols=(0,1))
labels_mat = np.loadtxt(data_file, dtype=np.float32, delimiter=",",
skiprows=0, usecols=[2], ndmin=2)

次に、私たちのトレーニングプログラムによって決定された重みとバイアスの値を設定する時が来ました-

print("Setting weights and bias values \n")
weights = np.array([0.0925, 1.1722], dtype=np.float32)
bias = np.array([-4.5400], dtype=np.float32)
N = len(features_mat)
features_dim = 2

次に、私たちの評価プログラムは、次のように各トレーニング項目を歩くことによってロジスティック回帰確率を計算します-

print("item pred_prob pred_label act_label result")
for i in range(0, N): # each item
   x = features_mat[i]
   z = 0.0
   for j in range(0, features_dim):
   z += x[j] *weights[j]
   z += bias[0]
   pred_prob = 1.0/(1.0 + np.exp(-z))
  pred_label = 0 if pred_prob < 0.5 else 1
   act_label = labels_mat[i]
   pred_str = ‘correct’ if np.absolute(pred_label - act_label) < 1.0e-5 \
    else ‘WRONG’
  print("%2d %0.4f %0.0f %0.0f %s" % \ (i, pred_prob, pred_label, act_label, pred_str))

今、私たちは予測を行う方法を示しましょう-

x = np.array([9.5, 4.5], dtype=np.float32)
print("\nPredicting class for age, education = ")
print(x)
z = 0.0
for j in range(0, features_dim):
z += x[j]* weights[j]
z += bias[0]
p = 1.0/(1.0 + np.exp(-z))
print("Predicted p = " + str(p))
if p < 0.5: print("Predicted class = 0")
else: print("Predicted class = 1")

完全な予測評価プログラム

import numpy as np
def main():
data_file = ".\\dataLRmodel.txt" # provide the name and the location of data file
features_mat = np.loadtxt(data_file, dtype=np.float32, delimiter=",",
skiprows=0, usecols=(0,1))
labels_mat = np.loadtxt(data_file, dtype=np.float32, delimiter=",",
skiprows=0, usecols=[2], ndmin=2)
print("Setting weights and bias values \n")
weights = np.array([0.0925, 1.1722], dtype=np.float32)
bias = np.array([-4.5400], dtype=np.float32)
N = len(features_mat)
features_dim = 2
print("item pred_prob pred_label act_label result")
for i in range(0, N): # each item
   x = features_mat[i]
   z = 0.0
   for j in range(0, features_dim):
     z += x[j] *weights[j]
   z += bias[0]
   pred_prob = 1.0/(1.0 + np.exp(-z))
   pred_label = 0 if pred_prob < 0.5 else 1
   act_label = labels_mat[i]
   pred_str = ‘correct’ if np.absolute(pred_label - act_label) < 1.0e-5 \
     else ‘WRONG’
  print("%2d %0.4f %0.0f %0.0f %s" % \ (i, pred_prob, pred_label, act_label, pred_str))
x = np.array([9.5, 4.5], dtype=np.float32)
print("\nPredicting class for age, education = ")
print(x)
z = 0.0
for j in range(0, features_dim):
   z += x[j]* weights[j]
z += bias[0]
p = 1.0/(1.0 + np.exp(-z))
print("Predicted p = " + str(p))
if p < 0.5: print("Predicted class = 0")
else: print("Predicted class = 1")
if __name__ == "__main__":
  main()

出力

重みとバイアス値の設定。

Item  pred_prob  pred_label  act_label  result
0   0.3640         0             0     correct
1   0.7254         1             0      WRONG
2   0.2019         0             0     correct
3   0.3562         0             0     correct
4   0.0493         0             0     correct
5   0.1005         0             0     correct
6   0.7892         1             1     correct
7   0.8564         1             1     correct
8   0.9654         1             1     correct
9   0.7587         1             1     correct
10  0.3040         0             1      WRONG
11  0.7129         1             1     correct
Predicting class for age, education =
[9.5 4.5]
Predicting p = 0.526487952
Predicting class = 1