Dưới đây là triển khai có thể có:
import tensorflow as tf
def maxnorm_regularizer(threshold, axes=1, name="maxnorm", collection="maxnorm"):
def maxnorm(weights):
clipped = tf.clip_by_norm(weights, clip_norm=threshold, axes=axes)
clip_weights = tf.assign(weights, clipped, name=name)
tf.add_to_collection(collection, clip_weights)
return None # there is no regularization loss term
return maxnorm
Đây là cách bạn sẽ sử dụng nó:
from tensorflow.contrib.layers import fully_connected
from tensorflow.contrib.framework import arg_scope
with arg_scope(
[fully_connected],
weights_regularizer=max_norm_regularizer(1.5)):
hidden1 = fully_connected(X, 200, scope="hidden1")
hidden2 = fully_connected(hidden1, 100, scope="hidden2")
outputs = fully_connected(hidden2, 5, activation_fn=None, scope="outs")
max_norm_ops = tf.get_collection("max_norm")
[...]
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
for X_batch, y_batch in load_next_batch():
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
sess.run(max_norm_ops)
Điều này tạo ra một mạng lưới thần kinh 3 lớp và huấn luyện nó với chính quy định mức tối đa tại mỗi lớp (với một ngưỡng 1,5). Tôi chỉ thử nó, dường như làm việc. Hi vọng điêu nay co ich! Đề xuất cải tiến được hoan nghênh. :)
Ghi chú
Mã này được dựa trên tf.clip_by_norm()
:
>>> x = tf.constant([0., 0., 3., 4., 30., 40., 300., 400.], shape=(4, 2))
>>> print(x.eval())
[[ 0. 0.]
[ 3. 4.]
[ 30. 40.]
[ 300. 400.]]
>>> clip_rows = tf.clip_by_norm(x, clip_norm=10, axes=1)
>>> print(clip_rows.eval())
[[ 0. 0. ]
[ 3. 4. ]
[ 6. 8. ] # clipped!
[ 6.00000048 8. ]] # clipped!
Bạn cũng có thể cắt cột nếu bạn cần phải:
>>> clip_cols = tf.clip_by_norm(x, clip_norm=350, axes=0)
>>> print(clip_cols.eval())
[[ 0. 0. ]
[ 3. 3.48245788]
[ 30. 34.82457733]
[ 300. 348.24578857]]
# clipped!
Cảm ơn các con trỏ! Điều đó trông rất gần với những gì tôi muốn, chỉ áp dụng cho toàn bộ ma trận, trong khi tôi muốn chuẩn hóa lại từng cột một cách độc lập (đổi tên của Ala Torch). Điều này có thể không? – danvk
Có lẽ bằng cách thay thế cuộc gọi ['reduce_sum'] (https://github.com/tensorflow/tensorflow/blob/b11337ea5719a8799f279a41c2cd5c9e75a8acf7/tensorflow/python/ops/clip_ops.py#L90-L93) trong' clip_by_norm' bằng một thứ được đặt ['reduction_indices'] (https://www.tensorflow.org/versions/master/api_docs/python/math_ops.html#reduce_sum)? – danvk
Ah, từ câu hỏi có vẻ như bạn muốn bình thường hóa toàn bộ ma trận bằng cách sử dụng tiêu chuẩn của nó và không phải cột riêng biệt. –