3.2_ 不同dim对应的具体含义
Scalar (dim=0)
- []
- 0, 1, 3.2, …
- l o s s = m s e ( o u t , y ) loss = mse(out, y) loss=mse(out,y)
- a c c u r a c y accuracy accuracy
out = tf.random.uniform([4,10])
y = tf.range(4)
y = tf.one_hot(y, depth=10)
loss = tf.keras.losses.mse(y, out)
loss = tf.reduce_mean(loss)
loss # tf.Tensor(0.2689442, shape=(), dtype=float32)
Vector (dim=1)
- Bias
- [out_put]
- [out_put]
net = layers.Dense(10)
net.build([4, 8])
net.kernel.shape # matrix
# (8, 10)
net.bias # vector
# <tf.Variable 'bias:0' shape=(10,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>
Matrix (dim=2)
- input x:[b, vec_dim]
- weight: [input_dim, output_dim]
x = tf.random.normal([4, 784])
net = layers.Dense(10) # 784 --> 10
net.build([4, 784])
net(x).shape
# (4, 10)
Tensor (dim=3)
- x: [b, seq_len, word_dim] [句子数目, 句子单词数目, 单词编码长度]
- [b, 5, 5]
- [b, 5, 5]
(X_train, y_train), (X_test, y_test) = keras.datasets.imdb.load_data(num_words=10000)
x_train = keras.preprocessing.sequence.pad_sequences(X_train, maxlen=80)
x_train.shape # (25000, 80)
emb = embedding(x_train)
emb.shape # (25000, 80, 100)
out = rnn(emb[:4])
out.shape # (4, 256)
Tensor (dim=4)
- images: [b, h, w, 3]
- feature maps: [b, h, w, c]
x = tf.random.normal([4,28,28,1])
net = layers.Conv2D(16, kernel_size=3)
net(x).shape # (4, 26, 26, 16)
Tensor (dim=5)
- Single task: [b, h, w, 3]
- meta-learning:
- [task_b, b, h, w, 3]