Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2from __future__ import division, print_function 

3import numpy as np 

4from theano import gof 

5import theano.tensor as tt 

6import theano.sparse as ts 

7 

8__all__ = ["dotROp", "tensordotRzOp"] 

9 

10 

11class dotROp(tt.Op): 

12 def __init__(self, func): 

13 self.func = func 

14 self._grad_op = dotRGradientOp(self) 

15 

16 def make_node(self, *inputs): 

17 inputs = [tt.as_tensor_variable(i) for i in inputs] 

18 outputs = [tt.TensorType(inputs[0].dtype, (False, False))()] 

19 return gof.Apply(self, inputs, outputs) 

20 

21 def infer_shape(self, node, shapes): 

22 return (shapes[0],) 

23 

24 def R_op(self, inputs, eval_points): 

25 if eval_points[0] is None: 

26 return eval_points 

27 return self.grad(inputs, eval_points) 

28 

29 def perform(self, node, inputs, outputs): 

30 outputs[0][0] = self.func(*inputs) 

31 

32 def grad(self, inputs, gradients): 

33 return self._grad_op(*(inputs + gradients)) 

34 

35 

36class dotRGradientOp(tt.Op): 

37 def __init__(self, base_op): 

38 self.base_op = base_op 

39 

40 def make_node(self, *inputs): 

41 inputs = [tt.as_tensor_variable(i) for i in inputs] 

42 outputs = [i.type() for i in inputs[:-1]] 

43 return gof.Apply(self, inputs, outputs) 

44 

45 def infer_shape(self, node, shapes): 

46 return shapes[:-1] 

47 

48 def perform(self, node, inputs, outputs): 

49 bM, bx, by, bz, btheta = self.base_op.func(*inputs) 

50 outputs[0][0] = np.reshape(bM, np.shape(inputs[0])) 

51 outputs[1][0] = np.reshape(bx, np.shape(inputs[1])) 

52 outputs[2][0] = np.reshape(by, np.shape(inputs[2])) 

53 outputs[3][0] = np.reshape(bz, np.shape(inputs[3])) 

54 outputs[4][0] = np.reshape(btheta, np.shape(inputs[4])) 

55 

56 

57class tensordotRzOp(tt.Op): 

58 def __init__(self, func): 

59 self.func = func 

60 self._grad_op = tensordotRzGradientOp(self) 

61 

62 def make_node(self, *inputs): 

63 inputs = [tt.as_tensor_variable(i) for i in inputs] 

64 outputs = [tt.TensorType(inputs[0].dtype, (False, False))()] 

65 return gof.Apply(self, inputs, outputs) 

66 

67 def infer_shape(self, node, shapes): 

68 return [[shapes[1][0], shapes[0][-1]]] 

69 

70 def R_op(self, inputs, eval_points): 

71 if eval_points[0] is None: 

72 return eval_points 

73 return self.grad(inputs, eval_points) 

74 

75 def perform(self, node, inputs, outputs): 

76 outputs[0][0] = self.func(*inputs) 

77 

78 def grad(self, inputs, gradients): 

79 return self._grad_op(*(inputs + gradients)) 

80 

81 

82class tensordotRzGradientOp(tt.Op): 

83 def __init__(self, base_op): 

84 self.base_op = base_op 

85 

86 def make_node(self, *inputs): 

87 inputs = [tt.as_tensor_variable(i) for i in inputs] 

88 outputs = [i.type() for i in inputs[:-1]] 

89 return gof.Apply(self, inputs, outputs) 

90 

91 def infer_shape(self, node, shapes): 

92 return shapes[:-1] 

93 

94 def perform(self, node, inputs, outputs): 

95 bM, btheta = self.base_op.func(*inputs) 

96 outputs[0][0] = np.reshape(bM, np.shape(inputs[0])) 

97 outputs[1][0] = np.reshape(btheta, np.shape(inputs[1]))