Python技巧

判断并输出

self.output = (lin_out if activation is None else activation(lin_out))

误分率

T.mean(T.neg(y_pred, y))

索引矩阵

对于一个多分类(multi-class)问题,也即对于输入样本(特征向量, xi )可能有多个输出,如如下的三输出问题。

>>> y = np.array([[0, 1, 1, 0, 2, 1, 2, 0, 1, 0]])
>>> X = np.random.randn(10, 3)
>>> X
array([[ 1.38232274, 0.32375763, -0.40038536], [-0.94959131, 1.66809094, -0.21389261], [-1.95956896, -0.59328881, 1.86772354], [-0.58905357, -0.33650614, 0.59462115], [ 0.13233892, 0.51155292, 1.39934502], [ 0.44779463, -0.9933518 , 0.50386344], [-2.80636655, 1.12979371, -0.73580525], [ 0.52145466, 0.8580254 , 0.39475806], [-0.50306457, 0.3044355 , -3.38301569], [-2.01156907, 0.97843953, -0.60538344]])
>>> X[np.arange(y.shape[0]), y]

array([ 1.38232274, 1.66809094, -0.59328881, -0.58905357, 1.39934502, -0.9933518 , -0.73580525, 0.52145466, 0.3044355, -2.01156907])

你可能感兴趣的:(Python技巧)