Python numpy.tanh() Examples

The following are 30 code examples for showing how to use numpy.tanh(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: numpynet   Author: uptake   File: common.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, choice="sigmoid"):
        """
        :param choice: Which activation function you want, must be in self.available
        """
        if choice not in self.available:
            msg = "Choice of activation (" + choice + ") not available!"
            log.out.error(msg)
            raise ValueError(msg)
        elif choice == "tanh":
            self.function = self._tanh
        elif choice == "tanhpos":
            self.function = self._tanhpos
        elif choice == "sigmoid":
            self.function = self._sigmoid
        elif choice == "softplus":
            self.function = self._softplus
        elif choice == "relu":
            self.function = self._relu
        elif choice == "leakyrelu":
            self.function = self._leakyrelu 
Example 2
Project: robosuite   Author: StanfordVL   File: panda_nut_assembly.py    License: MIT License 6 votes vote down vote up
def _check_success(self):
        """
        Returns True if task has been completed.
        """

        # remember objects that are on the correct pegs
        gripper_site_pos = self.sim.data.site_xpos[self.eef_site_id]
        for i in range(len(self.ob_inits)):
            obj_str = str(self.item_names[i]) + "0"
            obj_pos = self.sim.data.body_xpos[self.obj_body_id[obj_str]]
            dist = np.linalg.norm(gripper_site_pos - obj_pos)
            r_reach = 1 - np.tanh(10.0 * dist)
            self.objects_on_pegs[i] = int(self.on_peg(obj_pos, i) and r_reach < 0.6)

        if self.single_object_mode > 0:
            return np.sum(self.objects_on_pegs) > 0  # need one object on peg

        # returns True if all objects are on correct pegs
        return np.sum(self.objects_on_pegs) == len(self.ob_inits) 
Example 3
Project: robosuite   Author: StanfordVL   File: sawyer_pick_place.py    License: MIT License 6 votes vote down vote up
def _check_success(self):
        """
        Returns True if task has been completed.
        """

        # remember objects that are in the correct bins
        gripper_site_pos = self.sim.data.site_xpos[self.eef_site_id]
        for i in range(len(self.ob_inits)):
            obj_str = str(self.item_names[i]) + "0"
            obj_pos = self.sim.data.body_xpos[self.obj_body_id[obj_str]]
            dist = np.linalg.norm(gripper_site_pos - obj_pos)
            r_reach = 1 - np.tanh(10.0 * dist)
            self.objects_in_bins[i] = int(
                (not self.not_in_bin(obj_pos, i)) and r_reach < 0.6
            )

        # returns True if a single object is in the correct bin
        if self.single_object_mode == 1 or self.single_object_mode == 2:
            return np.sum(self.objects_in_bins) > 0

        # returns True if all objects are in correct bins
        return np.sum(self.objects_in_bins) == len(self.ob_inits) 
Example 4
Project: robosuite   Author: StanfordVL   File: panda_pick_place.py    License: MIT License 6 votes vote down vote up
def _check_success(self):
        """
        Returns True if task has been completed.
        """

        # remember objects that are in the correct bins
        gripper_site_pos = self.sim.data.site_xpos[self.eef_site_id]
        for i in range(len(self.ob_inits)):
            obj_str = str(self.item_names[i]) + "0"
            obj_pos = self.sim.data.body_xpos[self.obj_body_id[obj_str]]
            dist = np.linalg.norm(gripper_site_pos - obj_pos)
            r_reach = 1 - np.tanh(10.0 * dist)
            self.objects_in_bins[i] = int(
                (not self.not_in_bin(obj_pos, i)) and r_reach < 0.6
            )

        # returns True if a single object is in the correct bin
        if self.single_object_mode == 1 or self.single_object_mode == 2:
            return np.sum(self.objects_in_bins) > 0

        # returns True if all objects are in correct bins
        return np.sum(self.objects_in_bins) == len(self.ob_inits) 
Example 5
Project: robosuite   Author: StanfordVL   File: sawyer_nut_assembly.py    License: MIT License 6 votes vote down vote up
def _check_success(self):
        """
        Returns True if task has been completed.
        """

        # remember objects that are on the correct pegs
        gripper_site_pos = self.sim.data.site_xpos[self.eef_site_id]
        for i in range(len(self.ob_inits)):
            obj_str = str(self.item_names[i]) + "0"
            obj_pos = self.sim.data.body_xpos[self.obj_body_id[obj_str]]
            dist = np.linalg.norm(gripper_site_pos - obj_pos)
            r_reach = 1 - np.tanh(10.0 * dist)
            self.objects_on_pegs[i] = int(self.on_peg(obj_pos, i) and r_reach < 0.6)

        if self.single_object_mode > 0:
            return np.sum(self.objects_on_pegs) > 0  # need one object on peg

        # returns True if all objects are on correct pegs
        return np.sum(self.objects_on_pegs) == len(self.ob_inits) 
Example 6
Project: exposure   Author: yuanming-hu   File: net.py    License: MIT License 6 votes vote down vote up
def draw_value_reward_score(self, img, value, reward, score):
    img = img.copy()
    # Average with 0.5 for semi-transparent background
    img[:14] = img[:14] * 0.5 + 0.25
    img[50:] = img[50:] * 0.5 + 0.25
    if self.cfg.gan == 'ls':
      red = -np.tanh(float(score) / 1) * 0.5 + 0.5
    else:
      red = -np.tanh(float(score) / 10.0) * 0.5 + 0.5
    top = '%+.2f %+.2f' % (value, reward)
    cv2.putText(img, top, (3, 7), cv2.FONT_HERSHEY_SIMPLEX, 0.25,
                (1.0, 1.0 - red, 1.0 - red))
    score = '%+.3f' % score
    cv2.putText(img, score, (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.35,
                (1.0, 1.0 - red, 1.0 - red))
    return img 
Example 7
Project: nsf   Author: bayesiains   File: nonlinearities.py    License: MIT License 6 votes vote down vote up
def forward(self, inputs, context=None):
        mask_right = (inputs > self.cut_point)
        mask_left = (inputs < -self.cut_point)
        mask_middle = ~(mask_right | mask_left)

        outputs = torch.zeros_like(inputs)
        outputs[mask_middle] = torch.tanh(inputs[mask_middle])
        outputs[mask_right] = self.alpha * torch.log(self.beta * inputs[mask_right])
        outputs[mask_left] = self.alpha * -torch.log(-self.beta * inputs[mask_left])

        logabsdet = torch.zeros_like(inputs)
        logabsdet[mask_middle] = torch.log(1 - outputs[mask_middle] ** 2)
        logabsdet[mask_right] = torch.log(self.alpha / inputs[mask_right])
        logabsdet[mask_left] = torch.log(-self.alpha / inputs[mask_left])
        logabsdet = utils.sum_except_batch(logabsdet, num_batch_dims=1)

        return outputs, logabsdet 
Example 8
Project: iGAN   Author: junyanz   File: iGAN_predict.py    License: MIT License 6 votes vote down vote up
def invert_bfgs(gen_model, invert_model, ftr_model, im, z_predict=None, npx=64):
    _f, z = invert_model
    nz = gen_model.nz
    if z_predict is None:
        z_predict = np_rng.uniform(-1., 1., size=(1, nz))
    else:
        z_predict = floatX(z_predict)
    z_predict = np.arctanh(z_predict)
    im_t = gen_model.transform(im)
    ftr = ftr_model(im_t)

    prob = optimize.minimize(f_bfgs, z_predict, args=(_f, im_t, ftr),
                             tol=1e-6, jac=True, method='L-BFGS-B', options={'maxiter': 200})
    print('n_iters = %3d, f = %.3f' % (prob.nit, prob.fun))
    z_opt = prob.x
    z_opt_n = floatX(z_opt[np.newaxis, :])
    [f_opt, g, gx] = _f(z_opt_n, im_t, ftr)
    gx = gen_model.inverse_transform(gx, npx=npx)
    z_opt = np.tanh(z_opt)
    return gx, z_opt, f_opt 
Example 9
Project: deepchem   Author: deepchem   File: transformers.py    License: MIT License 6 votes vote down vote up
def expand(self, X):
    """Binarize features.

    Parameters:
    ----------
    X: np.ndarray
      Features

    Returns:
    -------
    X: np.ndarray
      Binarized features

    """
    Xexp = []
    for i in range(X.shape[1]):
      for k in np.arange(0, self.max[i] + self.step, self.step):
        Xexp += [np.tanh((X[:, i] - k) / self.step)]
    return np.array(Xexp).T 
Example 10
Project: PRML   Author: aidiary   File: function_approximation.py    License: MIT License 6 votes vote down vote up
def sum_of_squares_error(xlist, tlist, w1, w2):
    """二乗誤差和を計算する"""
    error = 0.0
    for n in range(N):
        z = np.zeros(NUM_HIDDEN)
        y = np.zeros(NUM_OUTPUT)
        # バイアスの1を先頭に挿入
        x = np.insert(xlist[n], 0, 1)
        # 順伝播で出力を計算
        for j in range(NUM_HIDDEN):
            a = np.zeros(NUM_HIDDEN)
            for i in range(NUM_INPUT):
                a[j] += w1[j, i] * x[i]
            z[j] = np.tanh(a[j])

        for k in range(NUM_OUTPUT):
            for j in range(NUM_HIDDEN):
                y[k] += w2[k, j] * z[j]
        # 二乗誤差を計算
        for k in range(NUM_OUTPUT):
            error += 0.5 * (y[k] - tlist[n, k]) * (y[k] - tlist[n, k])
    return error 
Example 11
Project: PRML   Author: aidiary   File: function_approximation.py    License: MIT License 6 votes vote down vote up
def output(x, w1, w2):
    """xを入力したときのニューラルネットワークの出力を計算
    隠れユニットの出力も一緒に返す"""
    # 配列に変換して先頭にバイアスの1を挿入
    x = np.insert(x, 0, 1)
    z = np.zeros(NUM_HIDDEN)
    y = np.zeros(NUM_OUTPUT)
    # 順伝播で出力を計算
    for j in range(NUM_HIDDEN):
        a = np.zeros(NUM_HIDDEN)
        for i in range(NUM_INPUT):
            a[j] += w1[j, i] * x[i]
        z[j] = np.tanh(a[j])
    for k in range(NUM_OUTPUT):
        for j in range(NUM_HIDDEN):
            y[k] += w2[k, j] * z[j]
    return y, z 
Example 12
Project: PRML   Author: aidiary   File: animation.py    License: MIT License 6 votes vote down vote up
def sum_of_squares_error(xlist, tlist, w1, w2):
    """二乗誤差和を計算する"""
    error = 0.0
    for n in range(N):
        z = np.zeros(NUM_HIDDEN)
        y = np.zeros(NUM_OUTPUT)
        # バイアスの1を先頭に挿入
        x = np.insert(xlist[n], 0, 1)
        # 順伝播で出力を計算
        for j in range(NUM_HIDDEN):
            a = np.zeros(NUM_HIDDEN)
            a[j] = np.dot(w1[j, :], x)
            z[j] = np.tanh(a[j])
        for k in range(NUM_OUTPUT):
            y[k] = np.dot(w2[k, :], z)
        # 二乗誤差を計算
        for k in range(NUM_OUTPUT):
            error += 0.5 * (y[k] - tlist[n, k]) * (y[k] - tlist[n, k])
    return error 
Example 13
Project: knmt   Author: fabiencro   File: faster_gru.py    License: GNU General Public License v3.0 6 votes vote down vote up
def faster_call2(self, h, x):
        r_z_h_x = self.W_r_z_h(x)

        r_z_h = self.U_r_z(h)

        r_x, z_x, h_x = split_axis(r_z_h_x, (self.n_units, self.n_units * 2), axis=1)
        assert r_x.data.shape[1] == self.n_units
        assert z_x.data.shape[1] == self.n_units
        assert h_x.data.shape[1] == self.n_units

        r_h, z_h = split_axis(r_z_h, (self.n_units,), axis=1)
#         r = sigmoid.sigmoid(r_x + r_h)
#         z = sigmoid.sigmoid(z_x + z_h)
#         h_bar = tanh.tanh(h_x + self.U(sigm_a_plus_b_by_h(r_x, r_h, h)))
#         h_new = (1 - z) * h + z * h_bar
#         return h_new

        return compute_output_GRU(z_x, z_h, h_x, h, self.U(sigm_a_plus_b_by_h_fast(r_x, r_h, h))) 
Example 14
Project: comet-commonsense   Author: atcbosselut   File: demo_bilinear.py    License: Apache License 2.0 5 votes vote down vote up
def run(gens_file, theshold=None, flip_r_e1=False):
    model = pickle.load(open("ckbc-demo/Bilinear_cetrainSize300frac1.0dSize200relSize150acti0.001.1e-05.800.RAND.tanh.txt19.pickle",  "r"))

    Rel = model['rel']
    We = model['embeddings']
    Weight = model['weight']
    Offset = model['bias']
    words = model['words_name']
    rel = model['rel_name']

    results = []

    if type(gens_file) == list:
        gens = []
        for file_name in gens_file:
            gens += open(file_name, "r").read().split("\n")
    else:
        gens = open(gens_file, "r").read().split("\n")

    formatted_gens = [tuple(i.split("\t")[:4]) for i in gens if i]

    for i, gen in enumerate(formatted_gens):
        if gen == ('s', 'r', 'o', 'minED'):
            continue
        if flip_r_e1:
            relation = "_".join(gen[1].split(" "))
            subject_ = "_".join(gen[0].split(" "))
        else:
            relation = "_".join(gen[0].split(" "))
            subject_ = "_".join(gen[1].split(" "))
        object_ = "_".join(gen[2].split(" "))
        result = score(subject_, object_, words, We, rel, Rel, Weight, Offset, relation)

        results.append((gen, result))

    return results 
Example 15
Project: deep-learning-note   Author: wdxtub   File: 13_weight_init_activation_histogram.py    License: MIT License 5 votes vote down vote up
def tanh(x):
    return np.tanh(x) 
Example 16
Project: fuku-ml   Author: fukuball   File: NeuralNetwork.py    License: MIT License 5 votes vote down vote up
def score_function(self, x, W):

        y_predict = x[1:]
        for i in range(0, len(W), 1):
            y_predict = np.tanh(np.dot(np.hstack((1, y_predict)), W[i]))

        score = y_predict[0]

        return score 
Example 17
Project: fuku-ml   Author: fukuball   File: NeuralNetwork.py    License: MIT License 5 votes vote down vote up
def forward_process(self, x, y, W):
        forward_output = []
        pre_x = x
        for i in range(len(W)):
            pre_x = np.tanh(np.dot(pre_x, W[i]))
            forward_output.append(pre_x)
            pre_x = np.hstack((1, pre_x))
        return forward_output 
Example 18
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: VAE.py    License: Apache License 2.0 5 votes vote down vote up
def encoder(model, x):
        params = model.arg_params
        encoder_n = np.shape(params['encoder_h_bias'].asnumpy())[0]
        encoder_h = np.dot(params['encoder_h_weight'].asnumpy(), np.transpose(x)) \
                    + np.reshape(params['encoder_h_bias'].asnumpy(), (encoder_n,1))
        act_h = np.tanh(encoder_h)
        mu = np.transpose(np.dot(params['mu_weight'].asnumpy(),act_h)) + params['mu_bias'].asnumpy()
        logvar = np.transpose(np.dot(params['logvar_weight'].asnumpy(),act_h)) + params['logvar_bias'].asnumpy()
        return mu,logvar 
Example 19
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: VAE.py    License: Apache License 2.0 5 votes vote down vote up
def decoder(model, z):
        params = model.arg_params
        decoder_n = np.shape(params['decoder_z_bias'].asnumpy())[0]
        decoder_z = np.dot(params['decoder_z_weight'].asnumpy(),np.transpose(z)) \
                    + np.reshape(params['decoder_z_bias'].asnumpy(),(decoder_n,1))
        act_z = np.tanh(decoder_z)
        decoder_x = np.transpose(np.dot(params['decoder_x_weight'].asnumpy(),act_z)) + params['decoder_x_bias'].asnumpy()
        reconstructed_x = 1/(1+np.exp(-decoder_x))
        return reconstructed_x 
Example 20
Project: numpynet   Author: uptake   File: common.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _tanh(x, deriv=False):
        """
        Hyperbolic tangent activation
        """
        if deriv:
            return 1.0 - np.power(np.tanh(x), 2)
        return np.tanh(x) 
Example 21
Project: numpynet   Author: uptake   File: common.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _tanhpos(x, deriv=False):
        """
        Positive hyperbolic tangent activation
        """
        if deriv:
            return (1.0 - np.power(np.tanh(x), 2)) / 2.0
        return (np.tanh(x) + 1.0) / 2.0 
Example 22
Project: robosuite   Author: StanfordVL   File: baxter_peg_in_hole.py    License: MIT License 5 votes vote down vote up
def reward(self, action):
        """
        Reward function for the task.

        The sparse reward is 0 if the peg is outside the hole, and 1 if it's inside.
        We enforce that it's inside at an appropriate angle (cos(theta) > 0.95).

        The dense reward has four components.

            Reaching: in [0, 1], to encourage the arms to get together.
            Perpendicular and parallel distance: in [0,1], for the same purpose.
            Cosine of the angle: in [0, 1], to encourage having the right orientation.
        """
        reward = 0

        t, d, cos = self._compute_orientation()

        # Right location and angle
        if d < 0.06 and t >= -0.12 and t <= 0.14 and cos > 0.95:
            reward = 1

        # use a shaping reward
        if self.reward_shaping:
            # reaching reward
            hole_pos = self.sim.data.body_xpos[self.hole_body_id]
            gripper_site_pos = self.sim.data.body_xpos[self.cyl_body_id]
            dist = np.linalg.norm(gripper_site_pos - hole_pos)
            reaching_reward = 1 - np.tanh(1.0 * dist)
            reward += reaching_reward

            # Orientation reward
            reward += 1 - np.tanh(d)
            reward += 1 - np.tanh(np.abs(t))
            reward += cos

        return reward 
Example 23
Project: pymoo   Author: msu-coinlab   File: go_funcs_D.py    License: Apache License 2.0 5 votes vote down vote up
def fun(self, x, *args):
        self.nfev += 1

        t = 0.1 * arange(16)
        y = (53.81 * 1.27 ** t * tanh(3.012 * t + sin(2.13 * t))
             * cos(exp(0.507) * t))

        return sum((x[0] * (x[1] ** t) * tanh(x[2] * t + sin(x[3] * t))
                   * cos(t * exp(x[4])) - y) ** 2.0) 
Example 24
Project: lightnn   Author: l11x0m7   File: activations.py    License: Apache License 2.0 5 votes vote down vote up
def backward(self, z, *args, **kwargs):
        return delta_softmax(z)


# --- tanh functions ---* 
Example 25
Project: lightnn   Author: l11x0m7   File: activations.py    License: Apache License 2.0 5 votes vote down vote up
def tanh(z):
    z = np.asarray(z)
    return np.tanh(z) 
Example 26
Project: lightnn   Author: l11x0m7   File: activations.py    License: Apache License 2.0 5 votes vote down vote up
def delta_tanh(z):
    z = np.asarray(z)
    return 1- np.power(np.tanh(z), 2) 
Example 27
Project: lightnn   Author: l11x0m7   File: activations.py    License: Apache License 2.0 5 votes vote down vote up
def forward(self, z, *args, **kwargs):
        return tanh(z) 
Example 28
Project: lightnn   Author: l11x0m7   File: activations.py    License: Apache License 2.0 5 votes vote down vote up
def get(activator):
    if activator is None:
        return Identity()
    if isinstance(activator, str):
        activator = activator.lower()
        if activator in ('linear', 'identity'):
            return Identity()
        elif activator in ('sigmoid', ):
            return Sigmoid()
        elif activator in ('relu', ):
            return Relu()
        elif activator in ('softmax', ):
            return Softmax()
        elif activator in ('tanh', ):
            return Tanh()
        elif activator in ('leaky_relu', 'leakyrelu'):
            return LeakyRelu()
        elif activator in ('elu', ):
            return Elu()
        elif activator in ('selu', ):
            return Selu()
        elif activator in ('thresholded_relu', 'thresholdedrelu'):
            return ThresholdedReLU()
        elif activator in ('softplus', ):
            return Softplus()
        elif activator in ('softsign', ):
            return Softsign()
        elif activator in ('hard_sigmoid', 'hardsigmoid'):
            return HardSigmoid()
        else:
            raise ValueError('Unknown activator name `{}`'.format(activator))
    elif isinstance(activator, Activator):
        return activator
    else:
        raise ValueError('Unknown activator type `{}`'.format(activator.__class__.__name__)) 
Example 29
Project: chainerrl   Author: chainer   File: test_distribution.py    License: MIT License 5 votes vote down vote up
def test_most_probable(self):
        most_probable = self.distrib.most_probable
        self.assertTrue(isinstance(most_probable, chainer.Variable))
        self.assertEqual(most_probable.shape, (self.batch_size, self.ndim))
        np.testing.assert_allclose(
            most_probable.array, np.tanh(self.mean), rtol=1e-5)
        _assert_array_in_range(most_probable.array, low=-1, high=1) 
Example 30
Project: tangent   Author: google   File: functions.py    License: Apache License 2.0 5 votes vote down vote up
def tanh(a):
  return np.tanh(a)