question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] [feature request] support for Dict and Tuple spaces

See original GitHub issue

I want to train using two images from different cameras and an array of 1d data from a sensor. I’m passing these input as my env state. Obviously I need a cnn that can take those inputs, concatenate, and train on them. My question is how to pass these input to such a custom cnn in polocies.py. Also, I tried to pass two images and apparently dummy_vec_env.py had trouble with that. obs = env.reset() File "d:\resources\stable-baselines\stable_baselines\common\vec_env\dummy_vec_env.py", line 57, in reset self._save_obs(env_idx, obs) File "d:\resources\stable-baselines\stable_baselines\common\vec_env\dummy_vec_env.py", line 75, in _save_obs self.buf_obs[key][env_idx] = obs ValueError: cannot copy sequence with size 2 to array axis with dimension 80

I appreciate any thoughts or examples.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:37 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
pulver22commented, Dec 19, 2018

Hello, Dict and Tuple spaces are not supported for observations spaces. Did you try concatenating the images along the channel axis?

Why aren’t they supported? I also would like to pass image + scalars an input to the policy at the current stage this is not possible. I don’t know if it’s more convenient to write a code for this or just add a vector of scalar at the end of the image and then separate it later.

5reactions
Miffylicommented, Dec 4, 2019

@radusl

You can append the “direct features” (non-image) features on e.g. last channel of the image, and pad it with zeros to match the other dimensions. Then you can use a cnn_extractor like one returned by this function to process the actual image with convolutions and then append it with direct features:

def create_augmented_nature_cnn(num_direct_features):
    """
    Create and return a function for augmented_nature_cnn
    used in stable-baselines.

    num_direct_features tells how many direct features there
    will be in the image.
    """

    def augmented_nature_cnn(scaled_images, **kwargs):
        """
        Copied from stable_baselines policies.py.
        This is nature CNN head where last channel of the image contains
        direct features.

        :param scaled_images: (TensorFlow Tensor) Image input placeholder
        :param kwargs: (dict) Extra keywords parameters for the convolutional layers of the CNN
        :return: (TensorFlow Tensor) The CNN output layer
        """
        activ = tf.nn.relu

        # Take last channel as direct features
        other_features = tf.contrib.slim.flatten(scaled_images[..., -1])
        # Take known amount of direct features, rest are padding zeros
        other_features = other_features[:, :num_direct_features]

        scaled_images = scaled_images[..., :-1]

        layer_1 = activ(conv(scaled_images, 'cnn1', n_filters=32, filter_size=8, stride=4, init_scale=np.sqrt(2), **kwargs))
        layer_2 = activ(conv(layer_1, 'cnn2', n_filters=64, filter_size=4, stride=2, init_scale=np.sqrt(2), **kwargs))
        layer_3 = activ(conv(layer_2, 'cnn3', n_filters=64, filter_size=3, stride=1, init_scale=np.sqrt(2), **kwargs))
        layer_3 = conv_to_fc(layer_3)

        # Append direct features to the final output of extractor
        img_output = activ(linear(layer_3, 'cnn_fc1', n_hidden=512, init_scale=np.sqrt(2)))

        concat = tf.concat((img_output, other_features), axis=1)

        return concat

    return augmented_nature_cnn
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Tutorial: Object Types - Dictionaries and Tuples - 2021
Dictionary only support accessing items by key. They also support type-specific operation with method calls. Since dictionaries are not sequences, they don't ...
Read more >
Python Add to Dictionary [Easy Step-By-Step] - DigitalOcean
Python dictionary is one of the built-in data types. Dictionary elements are key-value pairs. You can add to dictionary in Python using ...
Read more >
Snakefiles and Rules — Snakemake 7.19.1 documentation
Snakefiles and Rules¶. A Snakemake workflow defines a data analysis in terms of rules that are specified in the Snakefile. Most commonly, rules...
Read more >
Tuples( or arrays ) as Dictionary keys in C# - Stack Overflow
9 Answers 9 · its much easier to implement a functionality that looks like: · Furthermore, if your composite key require one more...
Read more >
Differences and Applications of List, Tuple, Set and Dictionary ...
Tuple can be created using tuple() function. ... Dictionary can be created using dict() function. List is mutable i.e we can make any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found