tensorflow报错InvalidArgumentError?

发布于 2022-09-11 14:25:51 字数 8116 浏览 14 评论 0

用tensorflow构建了一个卷积神经网络,代码如下:
import tensorflow as tf
import numpy as np
import gym
from collections import deque

tf.enable_eager_execution()

定义超参数

定义神经网络

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()

报错:
/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
[[[203 90 114]
[244 94 254]
[ 27 114 205]
...
[ 39 21 205]
[228 239 170]
[179 155 149]]

[[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]]]
(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


tf.Tensor(
[[[[203 90 114]
[244 94 254]
[ 27 114 205]
...
[ 39 21 205]
[228 239 170]
[179 155 149]]

[[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

查看了很长时间,请问这是什么问题?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过度放纵 2022-09-18 14:25:51

我也出现了相同的问题:

  • tensorflow.python.framework.errorsimpl.InvalidArgumentError: Need minval < maxval, got 0 >= 0
    [[{{node randomuniform}}]]
    [[node IteratorGetNext (defined at /home/fh/code/Object-Detection-API-Tensorflow/PFPNetR.py:65) ]]
    [[node IteratorGetNext (defined at /home/fh/code/Object-Detection-API-Tensorflow/PFPNetR.py:65) ]]

python-BaseException

Process finished with exit code 1*
请问你有解决吗

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文