Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple' #1

Open
ronseg1 opened this issue Oct 10, 2018 · 3 comments

Comments

@ronseg1
Copy link

ronseg1 commented Oct 10, 2018

While trying to run the code: https://github.com/ShuaiW/teach-machine-to-trade/blob/master/run.py
with: python run.py --mode train

I get the error:

Traceback (most recent call last):
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 141, in make_shape
shape = tensor_shape.as_shape(v)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 946, in as_shape
return TensorShape(shape)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 541, in init
self._dims = [as_dimension(d) for d in dims_iter]
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 541, in
self._dims = [as_dimension(d) for d in dims_iter]
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 482, in as_dimension
return Dimension(value)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 37, in init
self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "run.py", line 38, in
agent = DQNAgent(state_size, action_size)
File "C:\Users\i\Q-Learning-for-Trading-master\agent.py", line 18, in init
self.model = mlp(state_size, action_size)
File "C:\Users\i\Q-Learning-for-Trading-master\model.py", line 11, in mlp
model.add(Dense(n_neuron_per_layer, input_dim=n_obs, activation=activation))
File "C:\Users\i\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 161, in add
name=layer.name + '_input')
File "C:\Users\i\Anaconda3\lib\site-packages\keras\engine\input_layer.py", line 178, in Input
input_tensor=tensor)
File "C:\Users\i\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\i\Anaconda3\lib\site-packages\keras\engine\input_layer.py", line 87, in init
name=self.name)
File "C:\Users\i\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 517, in placeholder
x = tf.placeholder(dtype, shape=shape, name=name)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1734, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 5925, in placeholder
shape = _execute.make_shape(shape, "shape")
File "C:\Users\i\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 143, in make_shape
raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'.


I am using python 3.7 instead of python 2.7

Thanks in advance

@jolivaresc
Copy link

In run.py I've changed this line:
state_size = env.observation_space.shape to this
state_size = env.observation_space.shape[0][0]

next_state, reward, done, info = env.step(action) to
next_state, reward, done, info = env._step(action)

Also in envs.py:
action_vec = action_combo[action] -> action_vec = list(action_combo)[action]

With this changes I managed to run the script.

@jolivaresc
Copy link

In run.py I've changed this line:
state_size = env.observation_space.shape to this
state_size = env.observation_space.shape[0][0]

next_state, reward, done, info = env.step(action) to
next_state, reward, done, info = env._step(action)

Also in envs.py:
action_vec = action_combo[action] -> action_vec = list(action_combo)[action]

With this changes I managed to run the script.

I added a new method in the agent class agent.py so now script model.py it is not necessary

self.model=self.brain()
...
def brain(self):
        model = tf.keras.models.Sequential([
            tf.keras.layers.Dense(units=20,
                                  #input_shape=(self.state_size,),
                                  activation=tf.nn.tanh),
            tf.keras.layers.Dense(units=20, activation=tf.nn.tanh),
            tf.keras.layers.Dense(units=self.action_size)
        ])
        model.compile(loss="mse",
                      optimizer=tf.keras.optimizers.RMSprop(1e-3))
        return model

@ronseg1
Copy link
Author

ronseg1 commented Oct 12, 2018

In run.py I've changed this line:
state_size = env.observation_space.shape to this
state_size = env.observation_space.shape[0][0]

next_state, reward, done, info = env.step(action) to
next_state, reward, done, info = env._step(action)

Also in envs.py:
action_vec = action_combo[action] -> action_vec = list(action_combo)[action]

With this changes I managed to run the script.

Hi
It seems that my mistake was using the current version of gym instead of gym==0.9.4.
, so after reinstall gym and convert the script to python 3 with 2to3 it works fine.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants