TensorFlow
TensorFlow
ml5.js Getting Started
はじめに  |  TensorFlow.js
Machine Learning Glossary  |  Google Developers すごい
Tensor の操作
テンソルの基礎  |  TensorFlow Core
shape
tf.constant(3) / shape ()
tf.constant([3,5,7]) / shape (3,)
tf.constant([[3,5,7],[4,6,8]]) / shape (2,3)
...
tf.constant / tf.Variable
定数と変数
code:tensor.py
import tensorflow as tf
x = tf.constat([
3 ,5, 7,
4 ,6, 8
])
y = x:, 1 # => 5,6
y = tf.reshape(x, 3, 2) # => 3,5],7,4,[6,8
x = tf.Variable(2.0, dtype=tf.float32, name="my_variable")
x.assign(45.8)
x.assign_add(4)
x.assign_sub(3)
# w * x
w = tf.Variable(1.], [2.)
x = tf.constant(3., 4.)
tf.matmul(w, x)
自動微分と勾配テープ  |  TensorFlow Core
code:gradient
def compute_gradients(X, Y, w0, w1):
with tf.GradientTape() as tape:
loss = loss_mse(X, Y, w0, w1)
return tape.gradient(loss, w0, w1)
GoogleCloudPlatform/training-data-analyst@master - courses/machine_learning/deepdive2/introduction_to_tensorflow/labs/tensors-variables.ipynb
dtype=tf.float16
np.array(tensor) や tensor.numpy() で numpy へ
+, * といったオペレータある、 a @ b は行列の掛け算
tf.argmax(tensor)
tf.nn.softmax(tensor)
tensor.ndim / tensor.shape
(3, 2, 5) の tensor から tensor[:,:,4] でスライス
https://gyazo.com/424479461551a5c84e03b7c67192846e
tensor.shape.as_list()
tf.reshape(tensor, [shape])
-1 渡すと残りの要素に合わせて埋める
tf.cast(tensor, dtype=type) で dtype の変換
Module: tf.dtypes  |  TensorFlow v2.12.0 type 一覧
ブロードキャスト
掛け算等の操作で行列のように大きい形に引き伸ばされる
(3,1) と (4,) を掛けたら (3,4) に引き伸ばされる
tf.cloatcast_to(tensor, shape) で shape になるように引き伸ばす
tf.convert_to_tensor
list などを渡した時に暗黙に呼び出されて tf.Tensor に変換しているやつ
tf.ragged.RaggedTensor
サイズが揃っていない tensor
shape に None が入る
tf.string は文字列の dtype
Module: tf.strings  |  TensorFlow v2.12.0
tf.strings.split などで文字列分割して RaggedTensor に
tf.strings.to_number で数値型に
tf.strings.unicode_split
tf.sparse.SparseTensor 疎な tensor
tf.sparse.to_dense(tensor) で 0 埋め
tf.Variable
変数の概要  |  TensorFlow Core