Artificial-intelligence-with-python-neural-networks
PythonとAI –ニューラルネットワーク
ニューラルネットワークは、脳のコンピューターモデルを作成しようとする並列コンピューティングデバイスです。 背後にある主な目的は、従来のシステムよりも高速にさまざまな計算タスクを実行するシステムを開発することです。 これらのタスクには、パターン認識と分類、近似、最適化、データクラスタリングが含まれます。
人工ニューラルネットワーク(ANN)とは
人工ニューラルネットワーク(ANN)は、その中心テーマが生物学的ニューラルネットワークの類推から借用されている効率的なコンピューティングシステムです。 ANNは、人工ニューラルシステム、並列分散処理システム、およびコネクショニストシステムとも呼ばれます。 ANNは、ユニット間の通信を可能にするために、あるパターンで相互接続されたユニットの大規模なコレクションを取得します。 これらのユニットは、*ノード*または*ニューロン*とも呼ばれ、並列で動作する単純なプロセッサーです。
すべてのニューロンは、*接続リンク*を介して他のニューロンと接続されています。 各接続リンクは、入力信号に関する情報を持つ重みに関連付けられています。 これは、ニューロンが特定の問題を解決するための最も有用な情報です。なぜなら、*重み*は通常、伝達されている信号を興奮または阻害するからです。 各ニューロンには、*活性化信号*と呼ばれる内部状態があります。 入力信号とアクティベーションルールを組み合わせた後に生成される出力信号は、他のユニットに送信できます。
ニューラルネットワークを詳しく調べたい場合は、リンクをたどることができます-リンク:/artificial_neural_network/index [Artificial Neural Network]。
便利なパッケージをインストールする
Pythonでニューラルネットワークを作成するには、 NeuroLab と呼ばれるニューラルネットワーク用の強力なパッケージを使用できます。 Python用の柔軟なネットワーク構成と学習アルゴリズムを備えた基本的なニューラルネットワークアルゴリズムのライブラリです。 あなたは、コマンドプロンプトで次のコマンドの助けを借りてこのパッケージをインストールすることができます-
pip install NeuroLab
Anaconda環境を使用している場合は、次のコマンドを使用してNeuroLabをインストールします-
conda install -c labfabulous neurolab
ニューラルネットワークの構築
このセクションでは、NeuroLabパッケージを使用して、Pythonでニューラルネットワークを構築します。
パーセプトロンベースの分類器
パーセプトロンは、ANNの構成要素です。 パーセプトロンについてもっと知りたい場合は、リンクをたどることができます-リンク:/artificial_neural_network/artificial_neural_network_supervised_learning [artificial_neural_network]
以下は、単純なニューラルネットワークパーセプトロンベースの分類器を構築するためのPythonコードの段階的な実行です-
示されているように必要なパッケージをインポートします-
import matplotlib.pyplot as plt
import neurolab as nl
入力値を入力します。 これは教師あり学習の例であるため、目標値も提供する必要があることに注意してください。
input = [[target = [[0], [0], [0], [1]]
2つの入力と1つのニューロンを持つネットワークを作成します-
net = nl.net.newp([[Now, train the network. Here, we are using Delta rule for training.
[source,prettyprint,notranslate]
error_progress = net.train(input、target、epochs = 100、show = 10、lr = 0.1)
Now, visualize the output and plot the graph −
[source,prettyprint,notranslate]
plt.figure()plt.plot(error_progress)plt.xlabel( 'Number of epochs')plt.ylabel( 'Training error')plt.grid()plt.show()
You can see the following graph showing the training progress using the error metric −
image:/artificial_intelligence_with_python/perceptron_based_classifier.jpg[Perceptron based Classifier]
=== Single - Layer Neural Networks
In this example, we are creating a single layer neural network that consists of independent neurons acting on input data to produce the output. Note that we are using the text file named *neural_simple.txt *as our input.
Import the useful packages as shown −
[source,prettyprint,notranslate]
numpyをnpとしてインポートmatplotlib.pyplotをpltとしてインポートneurolabをnlとしてインポート
Load the dataset as follows −
[source,prettyprint,notranslate]
input_data = np.loadtxt(“/Users/admin/neural_simple.txt ')
The following is the data we are going to use. Note that in this data, first two columns are the features and last two columns are the labels.
[source,prettyprint,notranslate]
array([[今、これらの4つの列を2つのデータ列と2つのラベルに分けます-
data = input_data[:, 0:2]
labels = input_data[:, 2:]
次のコマンドを使用して入力データをプロットします-
plt.figure()
plt.scatter(data[:,0], data[:,1])
plt.xlabel('Dimension 1')
plt.ylabel('Dimension 2')
plt.title('Input data')
今、ここに示すように各次元の最小値と最大値を定義します-
dim1_min, dim1_max = data[:,0].min(), data[:,0].max()
dim2_min, dim2_max = data[:,1].min(), data[:,1].max()
次に、次のように出力層のニューロンの数を定義します-
nn_output_layer = labels.shape[1]
今、単層ニューラルネットワークを定義します-
dim1 = [dim1_min, dim1_max]
dim2 = [dim2_min, dim2_max]
neural_net = nl.net.newp([dim1, dim2], nn_output_layer)
示されているようにエポックの数と学習率でニューラルネットワークを訓練します-
error = neural_net.train(data, labels, epochs = 200, show = 20, lr = 0.01)
今、次のコマンドを使用して、トレーニングの進捗状況を視覚化し、プロットします-
plt.figure()
plt.plot(error)
plt.xlabel('Number of epochs')
plt.ylabel('Training error')
plt.title('Training error progress')
plt.grid()
plt.show()
今、上記の分類器でテストデータポイントを使用します-
print('\nTest Results:')
data_test = [[for item in data_test:
print(item, '-->', neural_net.sim([item])[0])
ここに示すように、テスト結果を見つけることができます-
[1.5, 3.2] --> [1. 0.]
[3.6, 1.7] --> [1. 0.]
[3.6, 5.7] --> [1. 1.]
[1.6, 3.9] --> [1. 0.]
あなたは今まで議論したコードの出力として次のグラフを見ることができます-
多層ニューラルネットワーク
この例では、複数のレイヤーで構成されるマルチレイヤーニューラルネットワークを作成して、トレーニングデータの基になるパターンを抽出しています。 この多層ニューラルネットワークは、リグレッサーのように機能します。 方程式y = 2x ^ 2 ^ + 8に基づいていくつかのデータポイントを生成します。
示されているように必要なパッケージをインポートします-
import numpy as np
import matplotlib.pyplot as plt
import neurolab as nl
上記の方程式に基づいていくつかのデータポイントを生成します-
min_val = -30
max_val = 30
num_points = 160
x = np.linspace(min_val, max_val, num_points)
y = 2* np.square(x) + 8
y/= np.linalg.norm(y)
さて、次のようにこのデータセットを作り直します-
data = x.reshape(num_points, 1)
labels = y.reshape(num_points, 1)
次のコマンドを使用して入力データセットを視覚化し、プロットします-
plt.figure()
plt.scatter(data, labels)
plt.xlabel('Dimension 1')
plt.ylabel('Dimension 2')
plt.title('Data-points')
ここで、最初の隠れ層に ten ニューロン、2番目の隠れ層に 6 、出力層に one を含む neurolab の2つの隠れ層を持つニューラルネットワークを構築します。
neural_net = nl.net.newff([[min_val, max_val]], [10, 6, 1])
今勾配トレーニングアルゴリズムを使用します-
neural_net.trainf = nl.train.train_gd
次に、上記で生成されたデータを学習することを目標にネットワークをトレーニングします-
error = neural_net.train(data, labels, epochs = 1000, show = 100, goal = 0.01)
今、トレーニングデータポイントでニューラルネットワークを実行します-
output = neural_net.sim(data)
y_pred = output.reshape(num_points)
今プロットと可視化タスク-
plt.figure()
plt.plot(error)
plt.xlabel('Number of epochs')
plt.ylabel('Error')
plt.title('Training error progress')
今、実際の出力と予測された出力をプロットします-
x_dense = np.linspace(min_val, max_val, num_points * 2)
y_dense_pred = neural_net.sim(x_dense.reshape(x_dense.size,1)).reshape(x_dense.size)
plt.figure()
plt.plot(x_dense, y_dense_pred, '-', x, y, '.', x, y_pred, 'p')
plt.title('Actual vs predicted')
plt.show()
上記のコマンドの結果として、以下に示すようにグラフを観察することができます-
Multi Layer Neural Networks Training Error Progress image:/artificial_intelligence_with_python/images/actual_vdicted_predicted_vs_predicted