Tensorflow-basics

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

TensorFlow-基本

この章では、TensorFlowの基本について学習します。 テンソルのデータ構造を理解することから始めます。

テンソルデータ構造

テンソルは、TensorFlow言語の基本的なデータ構造として使用されます。 テンソルは、データフローグラフと呼ばれるフロー図の接続エッジを表します。 テンソルは、多次元配列またはリストとして定義されます。

テンソルは、次の3つのパラメータによって識別されます-

Rank

テンソル内で記述される次元の単位はランクと呼ばれます。 テンソルの次元数を識別します。 テンソルのランクは、定義されたテンソルの次数またはn次元として説明できます。

形状

行と列の数が一緒になって、Tensorの形状を定義します。

Type

タイプは、Tensorの要素に割り当てられたデータタイプを示します。

ユーザーは、テンソルを構築するために次のアクティビティを考慮する必要があります-

  • n次元配列を作成する
  • n次元配列を変換します。

Tensorデータ構造

TensorFlowのさまざまな次元

TensorFlowにはさまざまなディメンションが含まれています。 寸法は以下に簡単に説明されています-

一次元テンソル

1次元テンソルは、同じデータ型の1セットの値を含む通常の配列構造です。

宣言

>>> import numpy as np
>>> tensor_1d = np.array([1.3, 1, 4.0, 23.99])
>>> print tensor_1d

出力の実装は、以下のスクリーンショットに示されています-

一次元テンソル

要素のインデックス付けは、Pythonリストと同じです。 最初の要素はインデックス0で始まります。インデックスを介して値を出力するには、インデックス番号を指定するだけです。

>>> print tensor_1d[0]
1.3
>>> print tensor_1d[2]
4.0

宣言

二次元テンソル

配列のシーケンスは、「2次元テンソル」の作成に使用されます。

二次元テンソルの作成は以下に説明されています-

二次元テンソル

以下は、二次元配列を作成するための完全な構文です-

>>> import numpy as np
>>> tensor_2d = np.array([(1,2,3,4),(4,5,6,7),(8,9,10,11),(12,13,14,15)])
>>> print(tensor_2d)
[[The specific elements of two dimensional tensors can be tracked with the help of row number and column number specified as index numbers.

[source,result,notranslate]

>>> tensor_2d [3] [2] 14

image:/tensorflow/two_dimensional_tensors_tracked.jpg[Two Dimensional Tensors Tracked]

=== Tensor Handling and Manipulations

In this section, we will learn about Tensor Handling and Manipulations.

To begin with, let us consider the following code −

[source,prettyprint,notranslate]

テンソルフローをtfとしてインポートします。

matrix1 = np.array([(2,2,2)、(2,2,2)、(2,2,2)]、dtype = 'int32')matrix2 = np.array([(1,1、 1)、(1,1,1)、(1,1,1)]、dtype = 'int32')

印刷(matrix1)印刷(matrix2)

matrix1 = tf.constant(matrix1)matrix2 = tf.constant(matrix2)matrix_product = tf.matmul(matrix1、matrix2)matrix_sum = tf.add(matrix1、matrix2)matrix_3 = np.array([(2,7,2) 、(1,4,2)、(9,0,2)]、dtype = 'float32')print(matrix_3)

matrix_det = tf.Session()をセッションとしてtf.matrix_determinant(matrix_3):result1 = sess.run(matrix_product)result2 = sess.run(matrix_sum)result3 = sess.run(matrix_det)

印刷(結果1)印刷(結果2)印刷(結果3)

*Output*

The above code will generate the following output −

image:/tensorflow/tensor_handling_and_manipulations.jpg[Tensor Handling and Manipulations]

==== Explanation

We have created multidimensional arrays in the above source code. Now, it is important to understand that we created graph and sessions, which manage the Tensors and generate the appropriate output. With the help of graph, we have the output specifying the mathematical calculations between Tensors.