A convolutional neural network is constructed with tensorflow. The code is as follows:
import tensorflow as tf
import numpy as np
import gym
from collections import deque
tf.enable_eager_execution ()
< H1 > define hyperparameters < / H1 > < H1 > define neural network < / H1 >class Ppo_net (tf.keras.Model):
def __init__(self):
super().__init__()
self.conv1 = tf.keras.layers.Conv2D(
filters=32,
kernel_size=[5,5],
padding = "same",
data_format= "channels_last",
activation = tf.nn.relu
)
self.pool1 = tf.keras.layers.MaxPool2D(pool_size=[2,2],strides=2)
self.conv2 = tf.keras.layers.Conv2D(
filters=64,
kernel_size=[5,5],
padding ="same",
activation=tf.nn.relu
)
self.pool2 = tf.keras.layers.MaxPool2D(pool_size=[2,2], strides=2)
self.flatten = tf.keras.layers.Reshape(target_shape=(24*24*64, ))
self.dense1 = tf.keras.layers.Dense(units = 512,activation = tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(units=3, activation = tf.nn.relu)
def call(self, inputs):
inputs = tf.reshape(inputs,[-1,96,96,3])
print("***********************")
print(inputs)
x= self.conv1(inputs)
x= self.pool1(x)
x= self.conv2(x)
x= self.pool2(x)
x= self.flatten(x)
x= dense1(x)
x= dense2(x)
print("******************88")
return x
def predict(self,inputs):
x = self.call(inputs)
def _ test ():
x = np.random.randint(1,256,(96,96,3))
print(x)
print(x.shape)
print(type(x))
model = Ppo_net()
x = model(x)
print (x.get_shape())
print("***********************************")
return x
_ test ()
error report:
/ home/kalarea/.conda/envs/py35/bin/python / home/kalarea/PycharmProjects/car_racing_ppo_demo/ppo_demo_1.py
/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from float
to np.floating
is deprecated. In future, it will be treated as np.float64 = = np.dtype (float) .type
.
from. _ conv import register_converters as _ register_converters
[20390114]
[24494254]
[271144205]
.
[39 21205]
[228239170]
[179155149]]
[[157 16 28]
[63 227 237]
[161 112 156]
.
[53 82 117]
[11 174 103]
[211 54 82]
[[96 125 148]
[153 247 255]
[129 143 134]
.
[47 69 78]
[197 83 235]
[115 34 186]]
.
[[28 90 96]
[45 78 243]
[82 13 250]
.
[38 89 91]
[218 227 173]
[199 4 199]
[[190 233 27]
[126 22 190]
[94 41 223]
.
[13 26 1]
[95 254 248]
[123 167 178]]
[199 225 156]
[83 172 191]
[210 45 90]
.
[194 155 110]
[215 53 172]
[91 53 172]
(96,96, 3)
< class" numpy.ndarray" >
Traceback (most recent call last):
File "/ home/kalarea/PycharmProjects/car_racing_ppo_demo/ppo_demo_1.py", line 67, in < module >
_test()
File "/ home/kalarea/PycharmProjects/car_racing_ppo_demo/ppo_demo_1.py", line 60, in _ test
x = model(x)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 769, in call
outputs = self.call(inputs, *args, **kwargs)
File "/ home/kalarea/PycharmProjects/car_racing_ppo_demo/ppo_demo_1.py", line 40, in call
x= self.conv1(inputs)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 759, in call
self.build(input_shapes)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/keras/layers/convolutional.py", line 161, in build
dtype=self.dtype)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 586, in add_weight
aggregation=aggregation)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/training/checkpointable/base.py", line 591, in _ add_variable_with_custom_getter
**kwargs_for_getter)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1986, in make_variable
aggregation=aggregation)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 145, in call
return cls._variable_call(*args, **kwargs)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 141, in _ variable_call
aggregation=aggregation)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 120, in < lambda >
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 2434, in default_variable_creator
< hr > tf.Tensor (
[20390114]
[24494254]
[27114205]
.
[39 21205]
[228239170]
[179155149]]
[[157 16 28]
[63 227 237]
[161 112 156]
.
[53 82 117]
[11 174 103]
[211 54 82]
[[96 125 148]
[153 247 255]
[129 143 134]
.
[47 69 78]
[197 83 235]
[115 34 186]]
.
[[28 90 96]
[45 78 243]
[82 13 250]
.
[38 89 91]
[218 227 173]
[199 4 199]
[[190 233 27]
[126 22 190]
[94 41 223]
.
[13 26 1]
[95 254 248]
[123 167 178]]
[199 225 156]
[83 172 191]
[210 45 90]
.
[194 155 110]
[215 125 64]
[91 53 172], shape= (1,96,96, 3), dtype=int64)
import_scope=import_scope)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 147, in call
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 297, in init
constraint=constraint)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 420, in _ init_from_args
initial_value = initial_value()
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1970, in < lambda >
shape, dtype=dtype, partition_info=partition_info)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/init_ops.py", line 483, in call
shape, -limit, limit, dtype, seed=self.seed)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/random_ops.py", line 240, in random_uniform
shape, minval, maxval, seed=seed1, seed2=seed2, name=name)
File "/ home/kalarea/.conda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/gen_random_ops.py", line 848, in random_uniform_int
_six.raise_from(_core._status_to_exception(e.code, message), None)
File "< string >", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Need minval < maxval, got 0 > = 0 [Op:RandomUniformInt] name: ppo_net/conv2d/kernel/random_uniform/
Process finished with exit code 1
I"ve been looking at it for a long time. What"s the problem?