Theano.tensor.round函数学习,同时解决输出Elemwise{xxx,no_inplace}.0的问题

1. 出现Elemwise{xxx,no_inplace}.0

  这是因为没有定义theano.function所致,参考下面错误示范

y = np.random.normal(size=(2,2))
z1 = T.round(y, mode='half_to_even')
print(z1)

# 输 出
Elemwise{round_half_to_even,no_inplace}.0

正确操作如下

x = T.dmatrix('x')
y = np.random.normal(size=(2,2))
z = T.round(x, mode='half_to_even')
f = function([x], z)
print(f(y))

# 输 出
[[ 0.,  1.],
[-1., -1.]]

2. tensor.round(a, mode='half_to_even')函数学习

  其实就是对a近似,离哪个整数值近就近似成那个值,有2种模式(我的theano版本默认为half_to_even),具体区别见下面代码:

# -*- coding:utf-8 -*-
# Theano learning
# 文档位置:http://deeplearning.net/software/theano/library/tensor/basic.html?highlight=round#theano.tensor.round
import theano
import numpy as np
import theano.tensor as T
from theano import function

# version 1.0.4
if __name__ == '__main__':
    """
    theano.tensor.round(a, mode='half_to_even')
    mode='half_to_even'(default) or 'half_away_from_zero'
    """
    x = T.dmatrix('x')
    y = np.random.normal(size=(2,2))

    z1 = T.round(x, mode='half_to_even')
    z2 = T.round(x, mode='half_away_from_zero')
    f1 = function([x], z1)
    f2 = function([x], z2)

    print(y)
    print(f1(y))
    print(f2(y))
    
    
    # 输 出
    # 试了好几个输入,可以看出2种模式近似几乎没区别,只有在近似到0时会出现-0这样
    [[-0.06255656 -0.44239336]
    [-2.25829238 -0.23422771]]
    
    [[ 0.  0.]
     [-2.  0.]]
    
    [[-0. -0.]
     [-2. -0.]]

转载于:https://www.cnblogs.com/huxiaozhouzhou/p/10734590.html

你可能感兴趣的:(Theano.tensor.round函数学习,同时解决输出Elemwise{xxx,no_inplace}.0的问题)