這個執行緒很好地解釋了使用tf.repeat()tensorflow 替代np.repeat(). 我無法弄清楚的一個功能,np.repeat()可以通過提供索引來復制特定的列/行/片。例如
import numpy as np
x = np.array([[1,2],[3,4]])
np.repeat(x, [1, 2], axis=0)
# Answer will be -> array([[1, 2],
# [3, 4],
# [3, 4]])
是否有任何 tensorflow 替代此功能np.repeat()?
uj5u.com熱心網友回復:
您可以使用以下repeats引數tf.repeat:
import tensorflow as tf
x = tf.constant([[1,2],[3,4]])
x = tf.repeat(x, repeats=[1, 2], axis=0)
print(x)
tf.Tensor(
[[1 2]
[3 4]
[3 4]], shape=(3, 2), dtype=int32)
您在張量中獲得第一行一次,第二行兩次。
或者你可以用tf.concat與tf.repeat:
import tensorflow as tf
x = tf.constant([[1,2],[3,4]])
x = tf.concat([x[:1], tf.repeat(x[1:], 2, axis=0)], axis=0)
print(x)
TensorFlow 1.14.0 解決方案:
import tensorflow as tf
x = tf.constant([[1,2],[3,4]])
x = tf.concat([x[:1], tf.tile(x[1:], multiples=[2, 1])], axis=0)
print(x)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/409763.html
標籤:
