Esempio n. 1
0
    def _build_module(self, input_layer):
        # This is almost exactly the same as Dueling Network but we predict the future measurements for each action

        multistep_measurements_size = self.measurements_size[0] * self.num_predicted_steps_ahead

        # actions expectation tower (expectation stream) - E
        with name_scope("expectation_stream"):
            expectation_stream = neon.Sequential([
                neon.Affine(nout=256, activation=neon.Rectlin(),
                            weight_init=self.weights_init, bias_init=self.biases_init),
                neon.Affine(nout=multistep_measurements_size,
                            weight_init=self.weights_init, bias_init=self.biases_init)
            ])(input_layer)

        # action fine differences tower (action stream) - A
        with name_scope("action_stream"):
            action_stream_unnormalized = neon.Sequential([
                neon.Affine(nout=256, activation=neon.Rectlin(),
                            weight_init=self.weights_init, bias_init=self.biases_init),
                neon.Affine(nout=self.num_actions * multistep_measurements_size,
                            weight_init=self.weights_init, bias_init=self.biases_init),
                neon.Reshape((self.num_actions, multistep_measurements_size))
            ])(input_layer)
            action_stream = action_stream_unnormalized - ng.mean(action_stream_unnormalized)

        repeated_expectation_stream = ng.slice_along_axis(expectation_stream, expectation_stream.axes[0], 0)
        repeated_expectation_stream = ng.expand_dims(repeated_expectation_stream, output_axis, 0)

        # merge to future measurements predictions
        self.output = repeated_expectation_stream + action_stream
Esempio n. 2
0
def test_nested_namescope():
    """
    Ops under multiple namescopes should have nested names. Namescope names don't nest
    so that they can be reused in different nestings.
    """
    with name_scope("scope1") as scope1:
        assert scope1.name == "scope1"
        val1 = ScopedNameableValue("val1")
        assert val1.name == "scope1/val1"
        with name_scope("scope2") as scope2:
            assert scope2.name == "scope2"
            val2 = ScopedNameableValue("val2")
            assert val2.name == "scope1/scope2/val2"
Esempio n. 3
0
def scope_ops(name=None, mode=None, subgraph=None, metadata=None):
    """
    All ops created within the context manager will be added to a subgraph

    Arguments:
        name (str): variable scope to use for all created ops
        mode (str): mode (e.g. "inference", "training") to annotate on all created ops
        subgraph (SubGraph): subgraph instance to add ops to. If not provided, one will be created
        metadata (dict): a dictionary of metadata to add to all created ops

    Yields:
        instance of SubGraph
    """
    if subgraph is None:
        subgraph = SubGraph()

    if metadata is None:
        metadata = dict()

    if mode is not None:
        metadata["mode"] = mode

    with name_scope(name=name, reuse_scope=True):
        with ng.Op.all_ops() as ops:
            with ng.metadata(**metadata):
                yield (subgraph)

    subgraph.ops.extend(ops)
Esempio n. 4
0
def test_scope_reuse():
    """
    Namescopes are only reused if explicitly requested. Otherwise, a uniquely named will
    be created.
    """

    with name_scope("scope") as scope1:
        val1 = ScopedNameableValue("val1")

    with name_scope("scope", reuse_scope=True) as scope2:
        val2 = ScopedNameableValue("val2")

    with name_scope("scope", reuse_scope=False) as scope3:
        val3 = ScopedNameableValue("val3")

    assert scope1 is scope2
    assert scope1 is not scope3
    assert val1.name == "scope/val1"
    assert val2.name == "scope/val2"
    assert val3.name != "scope/val3"
Esempio n. 5
0
    def __call__(self, prev_input_placeholder=None):
        with name_scope(self.get_name()):
            # create the input axes
            axes = []
            if len(self.input_size) == 2:
                axis_names = ['H', 'W']
            else:
                axis_names = ['C', 'H', 'W']
            for axis_size, axis_name in zip(self.input_size, axis_names):
                axes.append(ng.make_axis(axis_size, name=axis_name))
            batch_axis_full = ng.make_axis(self.batch_size, name='N')
            input_axes = ng.make_axes(axes)

            if prev_input_placeholder is None:
                self.input = ng.placeholder(input_axes + [batch_axis_full])
            else:
                self.input = prev_input_placeholder
            self._build_module()

        return self.input, self.output(self.input)
Esempio n. 6
0
    def __call__(self, input_layer):
        """
        Wrapper for building the module graph including scoping and loss creation
        :param input_layer: the input to the graph
        :return: the output of the last layer and the target placeholder
        """
        with name_scope(self.get_name()):
            self._build_module(input_layer)

            self.output = force_list(self.output)
            self.target = force_list(self.target)
            self.input = force_list(self.input)
            self.loss_type = force_list(self.loss_type)
            self.loss = force_list(self.loss)
            self.regularizations = force_list(self.regularizations)
            if self.is_local:
                self.set_loss()

        if self.is_local:
            return self.output, self.target, self.input
        else:
            return self.output, self.input
Esempio n. 7
0
    D2 = discriminator(generated)

    weight_clip_value = None  # no weight clipping
    gp_scale = args.gp_scale  # gradient penalty coefficient

    loss_d = D1 - D2
    loss_g = D2

# calculate gradient for all losses

x = ng.variable(initial_value=0.5, axes=[])
epsilon = ng.uniform(x)
interpolated = epsilon * image + (1 - epsilon) * generated
D3 = discriminator(interpolated)

with name_scope(name="GradientPenalty"):
    gradient = ng.deriv(ng.sum(D3, out_axes=[]), interpolated)
    grad_norm = ng.L2_norm(gradient)
    gradient_penalty = ng.square(grad_norm - 1)

# add gradient penalty
# TODO
# when gp_scale is set to 0 the behavior is not as expected
# loss_d = loss_d + 0 * gp is not loss_d = loss_d + 0
# we can get rid of if statement once this is fixed
# https://github.com/NervanaSystems/private-ngraph/issues/2145
if gp_scale:
    loss_d = loss_d + gp_scale * gradient_penalty

mean_cost_d = ng.mean(loss_d, out_axes=[])
mean_cost_g = ng.mean(loss_g, out_axes=[])
Esempio n. 8
0
    def __call__(self, input_layer):
        with name_scope(self.get_name()):
            self.input = input_layer
            self._build_module()

        return self.input, self.output(self.input)
Esempio n. 9
0
def mean_squared_error(targets, outputs, weights=1.0, scope=""):
    with name_scope(scope):
        # TODO: reduce mean over the action axis
        loss = ng.squared_L2(targets - outputs)
        weighted_loss = loss * weights
        return weighted_loss