Beispiel #1
0
    def TruncatedNormal(self, tf_node, inputs):
        """
        Outputs random values from a truncated normal distribution.
        `tf.truncated_normal()` call generates several ops, the
        The `TruncatedNormal` op is what we implement here.

        shape --> TruncatedNormal
                       |
                       V
        stddev -----> Mul
                       |
                       V
        mean -------> Add
                       |
                       V
                    (output)
        """
        # get inputs
        shape = tuple(inputs[0].const.astype(int))

        # generate truncated standard normal
        mu, sigma, lo, up = 0., 1., -2., 2
        generator = scipy.stats.truncnorm(
            (lo - mu) / sigma, (up - mu) / sigma, loc=mu, scale=sigma)
        np_val = generator.rvs(shape)
        return ns.constant(np_val, name=tf_node.name)
Beispiel #2
0
 def Const(self, tf_node, inputs):
     # convert to numpy value
     np_val = tensor_util.MakeNdarray(tf_node.attr['value'].tensor)
     if np_val.dtype == np.dtype('O'):
         return None
     else:
         return ns.constant(np_val, name=tf_node.name)
Beispiel #3
0
    def Fill(self, tf_node, inputs):
        # get inputs
        shape_op, const_val_op = inputs

        # get shape, const_val
        shape = tuple(shape_op.const.astype(int))
        const_val = const_val_op.const

        # convert to numpy value
        np_val = np.zeros(shape)
        np_val.fill(const_val)

        # create op
        return ns.constant(np_val, name=tf_node.name)
Beispiel #4
0
    def RandomStandardNormal(self, tf_node, inputs):
        """
        Outputs random values from a normal distribution. `tf.random_normal()`
        call generates several ops. The `RandomStandardNormal` op is what we
        implement here.

        Inputs to tf_node:
            shape, mean, dtype, seed, name
        """
        # get inputs
        shape = tuple(inputs[0].const.astype(int))

        # generate standard normal
        np_val = np.random.standard_normal(size=shape)
        return ns.constant(np_val, name=tf_node.name)
Beispiel #5
0
 def ZerosLike(self, tf_node, inputs):
     shape = inputs[0].axes.lengths
     np_val = np.zeros(shape)
     return ns.constant(np_val, name=tf_node.name)