def lstm_step(x, prev_h, prev_c, Wx, Wh, b): """ Forward pass for a single timestep of an LSTM. The input data has dimension D, the hidden state has dimension H, and we use a minibatch size of N. Inputs: - x: Input data, of shape (N, D) - prev_h: Previous hidden state, of shape (N, H) - prev_c: previous cell state, of shape (N, H) - Wx: Input-to-hidden weights, of shape (D, 4H) - Wh: Hidden-to-hidden weights, of shape (H, 4H) - b: Biases, of shape (4H,) Returns a tuple of: - next_h: Next hidden state, of shape (N, H) - next_c: Next cell state, of shape (N, H) """ N, H = prev_c.shape # 1. activation vector a = np.dot(x, Wx) + np.dot(prev_h, Wh) + b # 2. gate fuctions i = sigmoid(a[:, 0:H]) f = sigmoid(a[:, H:2 * H]) o = sigmoid(a[:, 2 * H:3 * H]) g = np.tanh(a[:, 3 * H:4 * H]) # 3. next cell state next_c = f * prev_c + i * g next_h = o * np.tanh(next_c) return next_h, next_c
def lstm_step(x, prev_h, prev_c, Wx, Wh, b): """ Forward pass for a single timestep of an LSTM. The input data has dimension D, the hidden state has dimension H, and we use a minibatch size of N. Inputs: - x: Input data, of shape (N, D) - prev_h: Previous hidden state, of shape (N, H) - prev_c: previous cell state, of shape (N, H) - Wx: Input-to-hidden weights, of shape (D, 4H) - Wh: Hidden-to-hidden weights, of shape (H, 4H) - b: Biases, of shape (4H,) Returns a tuple of: - next_h: Next hidden state, of shape (N, H) - next_c: Next cell state, of shape (N, H) """ N, H = prev_c.shape # 1. activation vector a = np.dot(x, Wx) + np.dot(prev_h, Wh) + b # 2. gate fuctions i = sigmoid(a[:, 0:H]) f = sigmoid(a[:, H:2*H]) o = sigmoid(a[:, 2*H:3*H]) g = np.tanh(a[:, 3*H:4*H]) # 3. next cell state next_c = f * prev_c + i * g next_h = o * np.tanh(next_c) return next_h, next_c
def update_lstm(input, hiddens, cells, weights): """One iteration of an LSTM layer.""" change = np.tanh(activations(weights.change, input, hiddens)) forget = sigmoid(activations(weights.forget, input, cells, hiddens)) ingate = sigmoid(activations(weights.ingate, input, cells, hiddens)) cells = cells * forget + ingate * change outgate = sigmoid(activations(weights.outgate, input, cells, hiddens)) hiddens = outgate * np.tanh(cells) return hiddens, cells
def get_action(params, x, continuous_a): x = x[np.newaxis, :] x = np.tanh(x.dot(params[0]) + params[1]) x = np.tanh(x.dot(params[2]) + params[3]) x = x.dot(params[4]) + params[5] if not continuous_a[0]: return np.argmax(x, axis=1)[0] # for discrete action else: return continuous_a[1] * np.tanh(x)[0] # for continuous action
def gru_step(x, prev_h, Wx, Wh, b, Wxh, Whh, bh): """ Forward pass for a single timestep of an GRU. The input data has dimentsion D, the hidden state has dimension H, and we use a minibatch size of N. Parameters ---------- x : Input data, of shape (N, D) prev_h : Previous hidden state, of shape (N, H) prev_c : Previous hidden state, of shape (N, H) Wx : Input-to-hidden weights for r and z gates, of shape (D, 2H) Wh : Hidden-to-hidden weights for r and z gates, of shape (H, 2H) b : Biases for r an z gates, of shape (2H,) Wxh : Input-to-hidden weights for h', of shape (D, H) Whh : Hidden-to-hidden weights for h', of shape (H, H) bh : Biases, of shape (H,) Returns ------- next_h : Next hidden state, of shape (N, H) Notes ----- Implementation follows http://jmlr.org/proceedings/papers/v37/jozefowicz15.pdf """ N, H = prev_h.shape a = sigmoid(np.dot(x, Wx) + np.dot(prev_h, Wh) + b) r = a[:, 0:H] z = a[:, H:2*H] h_m = np.tanh(np.dot(x, Wxh) + np.dot(r * prev_h, Whh) + bh) next_h = z * prev_h + (1 - z) * h_m return next_h
def rnn_step_forward(x, prev_h, Wx, Wh, b): """ Run the forward pass for a single timestep of a vanilla RNN that uses a tanh activation function. The input data has dimension D, the hidden state has dimension H, and we use a minibatch size of N. Inputs: - x: Input data for this timestep, of shape (N, D). - prev_h: Hidden state from previous timestep, of shape (N, H) - Wx: Weight matrix for input-to-hidden connections, of shape (D, H) - Wh: Weight matrix for hidden-to-hidden connections, of shape (H, H) - b: Biases of shape (H,) Returns a tuple of: - next_h: Next hidden state, of shape (N, H) """ next_h = np.tanh(x.dot(Wx) + prev_h.dot(Wh) + b) return next_h
def gru_step(x, prev_h, Wx, Wh, b, Wxh, Whh, bh): """ Forward pass for a single timestep of an GRU. The input data has dimentsion D, the hidden state has dimension H, and we use a minibatch size of N. Parameters ---------- x Input data, of shape (N, D) prev_h Previous hidden state, of shape (N, H) prev_c Previous hidden state, of shape (N, H) Wx Input-to-hidden weights for r and z gates, of shape (D, 2H) Wh Hidden-to-hidden weights for r and z gates, of shape (H, 2H) b Biases for r an z gates, of shape (2H,) Wxh Input-to-hidden weights for h', of shape (D, H) Whh Hidden-to-hidden weights for h', of shape (H, H) bh Biases, of shape (H,) Returns ------- next_h Next hidden state, of shape (N, H) Notes ----- Implementation follows http://jmlr.org/proceedings/papers/v37/jozefowicz15.pdf """ N, H = prev_h.shape a = sigmoid(np.dot(x, Wx) + np.dot(prev_h, Wh) + b) r = a[:, 0:H] z = a[:, H:2*H] h_m = np.tanh(np.dot(x, Wxh) + np.dot(r * prev_h, Whh) + bh) next_h = z * prev_h + (1 - z) * h_m return next_h
def rnn_step(x, prev_h, Wx, Wh, b): """ Run the forward pass for a single timestep of a vanilla RNN that uses a tanh activation function. The input data has dimension D, the hidden state has dimension H, and we use a minibatch size of N. Inputs: - x: Input data for this timestep, of shape (N, D). - prev_h: Hidden state from previous timestep, of shape (N, H) - Wx: Weight matrix for input-to-hidden connections, of shape (D, H) - Wh: Weight matrix for hidden-to-hidden connections, of shape (H, H) - b: Biases of shape (H,) Returns a tuple of: - next_h: Next hidden state, of shape (N, H) """ next_h = np.tanh(x.dot(Wx) + prev_h.dot(Wh) + b) return next_h
def parse(self, tokens, oracle_actions=None): def _valid_actions(stack, buffer): valid_actions = [] if len(buffer) > 0: valid_actions += [SHIFT] if len(stack) >= 2: valid_actions += [REDUCE_L, REDUCE_R] return valid_actions if oracle_actions: oracle_actions = list(oracle_actions) buffer = StackRNN(self.buffRNN, self.params['empty_buffer_emb']) stack = StackRNN(self.stackRNN) # Put the parameters in the cg W_comp = self.params['pW_comp'] # syntactic composition b_comp = self.params['pb_comp'] W_s2h = self.params['pW_s2h'] # state to hidden b_s2h = self.params['pb_s2h'] W_act = self.params['pW_act'] # hidden to action b_act = self.params['pb_act'] emb = self.params['wemb'] # We will keep track of all the losses we accumulate during parsing. # If some decision is unambiguous because it's the only thing valid given # the parser state, we will not model it. We only model what is ambiguous. loss = 0. # push the tokens onto the buffer (tokens is in reverse order) for tok in tokens: # TODO: I remember numpy ndarray supports python built-in list indexing tok_embedding = emb[np.array([tok])] buffer.push(tok_embedding, (tok_embedding, self.vocab.i2w[tok])) while not (len(stack) == 1 and len(buffer) == 0): # compute probability of each of the actions and choose an action # either from the oracle or if there is no oracle, based on the model valid_actions = _valid_actions(stack, buffer) log_probs = None action = valid_actions[0] if len(valid_actions) > 1: p_t = np.transpose( np.concatenate([buffer.top(), stack.top()], axis=1)) h = np.tanh(np.dot(W_s2h, p_t) + b_s2h) logits = np.dot(W_act, h) + b_act log_probs = logsoftmax(logits, valid_actions) if oracle_actions is None: # Temporary work around by manually back-off to numpy https://github.com/dmlc/minpy/issues/15 action = numpy.argmax(map(lambda x: x[0], list(log_probs))) if oracle_actions is not None: action = oracle_actions.pop() if log_probs is not None: # append the action-specific loss # print action, log_probs[action], map(lambda x: x[0], list(log_probs)) loss += log_probs[action] # execute the action to update the parser state if action == SHIFT: tok_embedding, token = buffer.pop() stack.push(tok_embedding, (tok_embedding, token)) else: # one of the REDUCE actions right = stack.pop() # pop a stack state left = stack.pop() # pop another stack state # figure out which is the head and which is the modifier head, modifier = (left, right) if action == REDUCE_R else (right, left) # compute composed representation head_rep, head_tok = head mod_rep, mod_tok = modifier composed_rep = np.tanh( np.dot( W_comp, np.transpose( np.concatenate([head_rep, mod_rep], axis=1))) + b_comp) composed_rep = np.transpose(composed_rep) stack.push(composed_rep, (composed_rep, head_tok)) if oracle_actions is None: print('{0} --> {1}'.format(head_tok, mod_tok)) # the head of the tree that remains at the top of the stack is the root if oracle_actions is None: head = stack.pop()[1] print('ROOT --> {0}'.format(head)) return -loss
def minpy_rnn_step_forward(x, prev_h, Wx, Wh, b): next_h = mp.tanh(x.dot(Wx) + prev_h.dot(Wh) + b) return next_h
def test_ufunc(): x = np.array([-1.2, 1.2]) np.absolute(x) np.absolute(1.2 + 1j) x = np.linspace(start=-10, stop=10, num=101) np.add(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.add(x1, x2) np.arccos([1, -1]) x = np.linspace(-1, 1, num=100) np.arccosh([np.e, 10.0]) np.arccosh(1) np.arcsin(0) np.arcsinh(np.array([np.e, 10.0])) np.arctan([0, 1]) np.pi/4 x = np.linspace(-10, 10) x = np.array([-1, +1, +1, -1]) y = np.array([-1, -1, +1, +1]) np.arctan2(y, x) * 180 / np.pi np.arctan2([1., -1.], [0., 0.]) np.arctan2([0., 0., np.inf], [+0., -0., np.inf]) np.arctanh([0, -0.5]) np.bitwise_and(13, 17) np.bitwise_and(14, 13) # np.binary_repr(12) return str np.bitwise_and([14,3], 13) np.bitwise_and([11,7], [4,25]) np.bitwise_and(np.array([2,5,255]), np.array([3,14,16])) np.bitwise_and([True, True], [False, True]) np.bitwise_or(13, 16) # np.binary_repr(29) np.bitwise_or(32, 2) np.bitwise_or([33, 4], 1) np.bitwise_or([33, 4], [1, 2]) np.bitwise_or(np.array([2, 5, 255]), np.array([4, 4, 4])) # np.array([2, 5, 255]) | np.array([4, 4, 4]) np.bitwise_or(np.array([2, 5, 255, 2147483647], dtype=np.int32), np.array([4, 4, 4, 2147483647], dtype=np.int32)) np.bitwise_or([True, True], [False, True]) np.bitwise_xor(13, 17) # np.binary_repr(28) np.bitwise_xor(31, 5) np.bitwise_xor([31,3], 5) np.bitwise_xor([31,3], [5,6]) np.bitwise_xor([True, True], [False, True]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.ceil(a) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.trunc(a) np.cos(np.array([0, np.pi/2, np.pi])) np.cosh(0) x = np.linspace(-4, 4, 1000) rad = np.arange(12.)*np.pi/6 np.degrees(rad) out = np.zeros((rad.shape)) r = np.degrees(rad, out) # np.all(r == out) return bool np.rad2deg(np.pi/2) np.divide(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.divide(2, 4) np.divide(2, 4.) np.equal([0, 1, 3], np.arange(3)) np.equal(1, np.ones(1)) x = np.linspace(-2*np.pi, 2*np.pi, 100) np.exp2([2, 3]) np.expm1(1e-10) np.exp(1e-10) - 1 np.fabs(-1) np.fabs([-1.2, 1.2]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.floor(a) np.floor_divide(7,3) np.floor_divide([1., 2., 3., 4.], 2.5) np.fmod([-3, -2, -1, 1, 2, 3], 2) np.remainder([-3, -2, -1, 1, 2, 3], 2) np.fmod([5, 3], [2, 2.]) a = np.arange(-3, 3).reshape(3, 2) np.fmod(a, [2,2]) np.greater([4,2],[2,2]) a = np.array([4,2]) b = np.array([2,2]) a > b np.greater_equal([4, 2, 1], [2, 2, 2]) np.hypot(3*np.ones((3, 3)), 4*np.ones((3, 3))) np.hypot(3*np.ones((3, 3)), [4]) np.bitwise_not is np.invert np.invert(np.array([13], dtype=np.uint8)) # np.binary_repr(242, width=8) np.invert(np.array([13], dtype=np.uint16)) np.invert(np.array([13], dtype=np.int8)) # np.binary_repr(-14, width=8) np.invert(np.array([True, False])) # np.isfinite(1) # np.isfinite(0) # np.isfinite(np.nan) # np.isfinite(np.inf) # np.isfinite(np.NINF) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) np.isfinite(x, y) # np.isinf(np.inf) # np.isinf(np.nan) # np.isinf(np.NINF) # np.isinf([np.inf, -np.inf, 1.0, np.nan]) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) # np.isinf(x, y) # np.isnan(np.nan) # np.isnan(np.inf) # np.binary_repr(5) np.left_shift(5, 2) # np.binary_repr(20) np.left_shift(5, [1,2,3]) np.less([1, 2], [2, 2]) np.less_equal([4, 2, 1], [2, 2, 2]) x = np.array([0, 1, 2, 2**4]) xi = np.array([0+1.j, 1, 2+0.j, 4.j]) np.log2(xi) prob1 = np.log(1e-50) prob2 = np.log(2.5e-50) prob12 = np.logaddexp(prob1, prob2) prob12 np.exp(prob12) prob1 = np.log2(1e-50) prob2 = np.log2(2.5e-50) prob12 = np.logaddexp2(prob1, prob2) prob1, prob2, prob12 2**prob12 np.log1p(1e-99) np.log(1 + 1e-99) # np.logical_and(True, False) # np.logical_and([True, False], [False, False]) x = np.arange(5) # np.logical_and(x>1, x<4) # np.logical_not(3) # np.logical_not([True, False, 0, 1]) x = np.arange(5) # np.logical_not(x<3) # np.logical_or(True, False) # np.logical_or([True, False], [False, False]) x = np.arange(5) # np.logical_or(x < 1, x > 3) # np.logical_xor(True, False) # np.logical_xor([True, True, False, False], [True, False, True, False]) x = np.arange(5) # np.logical_xor(x < 1, x > 3) # np.logical_xor(0, np.eye(2)) np.maximum([2, 3, 4], [1, 5, 2]) # np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan]) # np.maximum(np.Inf, 1) np.minimum([2, 3, 4], [1, 5, 2]) # np.minimum([np.nan, 0, np.nan],[0, np.nan, np.nan]) # np.minimum(-np.Inf, 1) np.fmax([2, 3, 4], [1, 5, 2]) np.fmax(np.eye(2), [0.5, 2]) # np.fmax([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.fmin([2, 3, 4], [1, 5, 2]) np.fmin(np.eye(2), [0.5, 2]) # np.fmin([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.modf([0, 3.5]) np.modf(-0.5) np.multiply(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.multiply(x1, x2) np.negative([1.,-1.]) np.not_equal([1.,2.], [1., 3.]) np.not_equal([1, 2], [[1, 3],[1, 4]]) x1 = range(6) np.power(x1, 3) x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0] np.power(x1, x2) x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]]) np.power(x1, x2) deg = np.arange(12.) * 30. np.radians(deg) out = np.zeros((deg.shape)) ret = np.radians(deg, out) ret is out np.deg2rad(180) np.reciprocal(2.) np.reciprocal([1, 2., 3.33]) np.remainder([4, 7], [2, 3]) np.remainder(np.arange(7), 5) # np.binary_repr(10) np.right_shift(10, 1) # np.binary_repr(5) np.right_shift(10, [1,2,3]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.rint(a) np.sign([-5., 4.5]) np.sign(0) # np.sign(5-2j) # np.signbit(-1.2) np.signbit(np.array([1, -2.3, 2.1])) np.copysign(1.3, -1) np.copysign([-1, 0, 1], -1.1) np.copysign([-1, 0, 1], np.arange(3)-1) np.sin(np.pi/2.) np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180. ) x = np.linspace(-np.pi, np.pi, 201) np.sinh(0) # np.sinh(np.pi*1j/2) np.sqrt([1,4,9]) np.sqrt([4, -1, -3+4J]) np.cbrt([1,8,27]) np.square([-1j, 1]) np.subtract(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.subtract(x1, x2) np.tan(np.array([-pi,pi/2,pi])) np.tanh((0, np.pi*1j, np.pi*1j/2)) x = np.arange(5) np.true_divide(x, 4) x = np.arange(9) y1, y2 = np.frexp(x) y1 * 2**y2 np.ldexp(5, np.arange(4)) x = np.arange(6) np.ldexp(*np.frexp(x))
def tanh(x): return np.tanh(x)
def sigmoid(x): return 0.5*(np.tanh(x) + 1.0) # Output ranges from 0 to 1.
def forward(self, inputs, *args): return np.tanh(inputs)
def test_ufunc(): x = np.array([-1.2, 1.2]) np.absolute(x) np.absolute(1.2 + 1j) x = np.linspace(start=-10, stop=10, num=101) np.add(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.add(x1, x2) np.arccos([1, -1]) x = np.linspace(-1, 1, num=100) np.arccosh([np.e, 10.0]) np.arccosh(1) np.arcsin(0) np.arcsinh(np.array([np.e, 10.0])) np.arctan([0, 1]) np.pi / 4 x = np.linspace(-10, 10) x = np.array([-1, +1, +1, -1]) y = np.array([-1, -1, +1, +1]) np.arctan2(y, x) * 180 / np.pi np.arctan2([1., -1.], [0., 0.]) np.arctan2([0., 0., np.inf], [+0., -0., np.inf]) np.arctanh([0, -0.5]) np.bitwise_and(13, 17) np.bitwise_and(14, 13) # np.binary_repr(12) return str np.bitwise_and([14, 3], 13) np.bitwise_and([11, 7], [4, 25]) np.bitwise_and(np.array([2, 5, 255]), np.array([3, 14, 16])) np.bitwise_and([True, True], [False, True]) np.bitwise_or(13, 16) # np.binary_repr(29) np.bitwise_or(32, 2) np.bitwise_or([33, 4], 1) np.bitwise_or([33, 4], [1, 2]) np.bitwise_or(np.array([2, 5, 255]), np.array([4, 4, 4])) # np.array([2, 5, 255]) | np.array([4, 4, 4]) np.bitwise_or(np.array([2, 5, 255, 2147483647], dtype=np.int32), np.array([4, 4, 4, 2147483647], dtype=np.int32)) np.bitwise_or([True, True], [False, True]) np.bitwise_xor(13, 17) # np.binary_repr(28) np.bitwise_xor(31, 5) np.bitwise_xor([31, 3], 5) np.bitwise_xor([31, 3], [5, 6]) np.bitwise_xor([True, True], [False, True]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.ceil(a) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.trunc(a) np.cos(np.array([0, np.pi / 2, np.pi])) np.cosh(0) x = np.linspace(-4, 4, 1000) rad = np.arange(12.) * np.pi / 6 np.degrees(rad) out = np.zeros((rad.shape)) r = np.degrees(rad, out) # np.all(r == out) return bool np.rad2deg(np.pi / 2) np.divide(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.divide(2, 4) np.divide(2, 4.) np.equal([0, 1, 3], np.arange(3)) np.equal(1, np.ones(1)) x = np.linspace(-2 * np.pi, 2 * np.pi, 100) np.exp2([2, 3]) np.expm1(1e-10) np.exp(1e-10) - 1 np.fabs(-1) np.fabs([-1.2, 1.2]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.floor(a) np.floor_divide(7, 3) np.floor_divide([1., 2., 3., 4.], 2.5) np.fmod([-3, -2, -1, 1, 2, 3], 2) np.remainder([-3, -2, -1, 1, 2, 3], 2) np.fmod([5, 3], [2, 2.]) a = np.arange(-3, 3).reshape(3, 2) np.fmod(a, [2, 2]) np.greater([4, 2], [2, 2]) a = np.array([4, 2]) b = np.array([2, 2]) a > b np.greater_equal([4, 2, 1], [2, 2, 2]) np.hypot(3 * np.ones((3, 3)), 4 * np.ones((3, 3))) np.hypot(3 * np.ones((3, 3)), [4]) np.bitwise_not is np.invert np.invert(np.array([13], dtype=np.uint8)) # np.binary_repr(242, width=8) np.invert(np.array([13], dtype=np.uint16)) np.invert(np.array([13], dtype=np.int8)) # np.binary_repr(-14, width=8) np.invert(np.array([True, False])) # np.isfinite(1) # np.isfinite(0) # np.isfinite(np.nan) # np.isfinite(np.inf) # np.isfinite(np.NINF) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) np.isfinite(x, y) # np.isinf(np.inf) # np.isinf(np.nan) # np.isinf(np.NINF) # np.isinf([np.inf, -np.inf, 1.0, np.nan]) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) # np.isinf(x, y) # np.isnan(np.nan) # np.isnan(np.inf) # np.binary_repr(5) np.left_shift(5, 2) # np.binary_repr(20) np.left_shift(5, [1, 2, 3]) np.less([1, 2], [2, 2]) np.less_equal([4, 2, 1], [2, 2, 2]) x = np.array([0, 1, 2, 2**4]) xi = np.array([0 + 1.j, 1, 2 + 0.j, 4.j]) np.log2(xi) prob1 = np.log(1e-50) prob2 = np.log(2.5e-50) prob12 = np.logaddexp(prob1, prob2) prob12 np.exp(prob12) prob1 = np.log2(1e-50) prob2 = np.log2(2.5e-50) prob12 = np.logaddexp2(prob1, prob2) prob1, prob2, prob12 2**prob12 np.log1p(1e-99) np.log(1 + 1e-99) # np.logical_and(True, False) # np.logical_and([True, False], [False, False]) x = np.arange(5) # np.logical_and(x>1, x<4) # np.logical_not(3) # np.logical_not([True, False, 0, 1]) x = np.arange(5) # np.logical_not(x<3) # np.logical_or(True, False) # np.logical_or([True, False], [False, False]) x = np.arange(5) # np.logical_or(x < 1, x > 3) # np.logical_xor(True, False) # np.logical_xor([True, True, False, False], [True, False, True, False]) x = np.arange(5) # np.logical_xor(x < 1, x > 3) # np.logical_xor(0, np.eye(2)) np.maximum([2, 3, 4], [1, 5, 2]) # np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan]) # np.maximum(np.Inf, 1) np.minimum([2, 3, 4], [1, 5, 2]) # np.minimum([np.nan, 0, np.nan],[0, np.nan, np.nan]) # np.minimum(-np.Inf, 1) np.fmax([2, 3, 4], [1, 5, 2]) np.fmax(np.eye(2), [0.5, 2]) # np.fmax([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.fmin([2, 3, 4], [1, 5, 2]) np.fmin(np.eye(2), [0.5, 2]) # np.fmin([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.modf([0, 3.5]) np.modf(-0.5) np.multiply(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.multiply(x1, x2) np.negative([1., -1.]) np.not_equal([1., 2.], [1., 3.]) np.not_equal([1, 2], [[1, 3], [1, 4]]) x1 = range(6) np.power(x1, 3) x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0] np.power(x1, x2) x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]]) np.power(x1, x2) deg = np.arange(12.) * 30. np.radians(deg) out = np.zeros((deg.shape)) ret = np.radians(deg, out) ret is out np.deg2rad(180) np.reciprocal(2.) np.reciprocal([1, 2., 3.33]) np.remainder([4, 7], [2, 3]) np.remainder(np.arange(7), 5) # np.binary_repr(10) np.right_shift(10, 1) # np.binary_repr(5) np.right_shift(10, [1, 2, 3]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.rint(a) np.sign([-5., 4.5]) np.sign(0) # np.sign(5-2j) # np.signbit(-1.2) np.signbit(np.array([1, -2.3, 2.1])) np.copysign(1.3, -1) np.copysign([-1, 0, 1], -1.1) np.copysign([-1, 0, 1], np.arange(3) - 1) np.sin(np.pi / 2.) np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.) x = np.linspace(-np.pi, np.pi, 201) np.sinh(0) # np.sinh(np.pi*1j/2) np.sqrt([1, 4, 9]) np.sqrt([4, -1, -3 + 4J]) np.cbrt([1, 8, 27]) np.square([-1j, 1]) np.subtract(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.subtract(x1, x2) np.tan(np.array([-pi, pi / 2, pi])) np.tanh((0, np.pi * 1j, np.pi * 1j / 2)) x = np.arange(5) np.true_divide(x, 4) x = np.arange(9) y1, y2 = np.frexp(x) y1 * 2**y2 np.ldexp(5, np.arange(4)) x = np.arange(6) np.ldexp(*np.frexp(x))
def sigmoid(x): return 0.5 * (np.tanh(x / 2.0) + 1.0) # Output ranges from 0 to 1.
def sigmoid(x): return np.multiply(0.5, np.add(np.tanh(x), 1))
def sigmoid(x): return 0.5 * (np.tanh(x / 2) + 1)
def nonlinearity(self, x): return np.tanh(x)