只需在TensorFlow中查找等效的np.std()即可計算張量的標準偏差。TensorFlow中np.std()的等價物是什麼?
14
A
回答
22
3
您還可以在改編自Keras下面的代碼使用reduce_std
:
#coding=utf-8
import numpy as np
import tensorflow as tf
def reduce_var(x, axis=None, keepdims=False):
"""Variance of a tensor, alongside the specified axis.
# Arguments
x: A tensor or variable.
axis: An integer, the axis to compute the variance.
keepdims: A boolean, whether to keep the dimensions or not.
If `keepdims` is `False`, the rank of the tensor is reduced
by 1. If `keepdims` is `True`,
the reduced dimension is retained with length 1.
# Returns
A tensor with the variance of elements of `x`.
"""
m = tf.reduce_mean(x, axis=axis, keep_dims=True)
devs_squared = tf.square(x - m)
return tf.reduce_mean(devs_squared, axis=axis, keep_dims=keepdims)
def reduce_std(x, axis=None, keepdims=False):
"""Standard deviation of a tensor, alongside the specified axis.
# Arguments
x: A tensor or variable.
axis: An integer, the axis to compute the standard deviation.
keepdims: A boolean, whether to keep the dimensions or not.
If `keepdims` is `False`, the rank of the tensor is reduced
by 1. If `keepdims` is `True`,
the reduced dimension is retained with length 1.
# Returns
A tensor with the standard deviation of elements of `x`.
"""
return tf.sqrt(reduce_var(x, axis=axis, keepdims=keepdims))
if __name__ == '__main__':
x_np = np.arange(10).reshape(2, 5).astype(np.float32)
x_tf = tf.constant(x_np)
with tf.Session() as sess:
print(sess.run(reduce_std(x_tf, keepdims=True)))
print(sess.run(reduce_std(x_tf, axis=0, keepdims=True)))
print(sess.run(reduce_std(x_tf, axis=1, keepdims=True)))
print(np.std(x_np, keepdims=True))
print(np.std(x_np, axis=0, keepdims=True))
print(np.std(x_np, axis=1, keepdims=True))
+0
我使用tf1.4,由於某種原因tf.nn.moments不給我一個正確的結果...我試過你的版本,它在第一次嘗試:) +1 –
相關問題
- 1. 什麼是numpy.random.multivariate_normal的Tensorflow等價物?
- 2. Tensorflow:來自Tensorflow中Caffe的weight_filler「gaussian」的等價物是什麼?
- 3. TensorFlow上一個簡單的SGD的CNTK等價物是什麼?
- 4. 什麼是這種numpy陣列排列的TensorFlow等價物?
- 5. 什麼是android中的dataWithContentsOfURL等價物?
- 6. RDFlib中rdf:ID的等價物是什麼?
- 7. Monotouch中的CGPDFDocumentGetCatalog等價物是什麼?
- 8. 什麼是C++中的instanceof等價物?
- 9. 什麼是VC7中的strtok_s等價物?
- 10. jquery中Ajax.updater的等價物是什麼?
- 11. 什麼是JSP中的sendmail等價物?
- 12. jQuery中Class.create()的等價物是什麼?
- 13. python中'gem'的等價物是什麼?
- 14. 什麼是MSTest中MbUnit.Framework.RowAttribute的等價物?
- 15. JQuery中innerHTML的等價物是什麼?
- 16. WinRT中SecureString的等價物是什麼?
- 17. java中fopen_s()的等價物是什麼?
- 18. 什麼是perl中$ _的php等價物?
- 19. Bindingsource中EOF的等價物是什麼?
- 20. 什麼是Java中的「ByRef」等價物?
- 21. java中cin.ignore()的等價物是什麼?
- 22. Swift中@autoreleasepool的等價物是什麼?
- 23. 什麼是C#中的vbNullChar等價物?
- 24. Python中imadjust的等價物是什麼?
- 25. CoreFoundation中NSHomeDirectory()的等價物是什麼?
- 26. 什麼是Silverlight中的OnRender等價物?
- 27. 什麼是GraphicsMagick中的setCompression()等價物?
- 28. C#中bigint的等價物是什麼?
- 29. C#中TreeBidiMap的等價物是什麼?
- 30. C#中memset的等價物是什麼?
我怎樣才能做到這在C++ API? –
我只看到在C++ API中的平均值的文檔:https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/mean我想你必須自己計算方差。 sum [(x-u)^ 2]您可能可以通過python源代碼瞭解他們如何調用後端以瞭解如何更有效地計算方差。 – Steven