创建零张量时的Keras TyperError

时间:2019-06-25 19:32:37

标签: python python-2.7 tensorflow keras

系统信息

  • 从第三方获得的自定义代码。

  • Ubuntu 16.04

  • TensorFlow后端(是/否):是

  • TensorFlow版本:('v1.14.0-rc1-22-gaf24dc91b5','1.14.0')

  • Keras版本:2.2.4

  • Python版本:2.7.12

  • CUDA / cuDNN版本:10.0 / 7.6.0.64

  • GPU模型和内存:GTX TITAN X

描述当前行为

当我尝试创建一个新的零张量时,我收到指出NoneType的类型错误。

用于重现问题的代码

from keras.layers.core import Layer
from tensorflow import Tensor as T
import tensorflow as tf
from keras import backend as K
#import theano.tensor as T

class LRN(Layer):

    def __init__(self, alpha=0.0001,k=1,beta=0.75,n=5, **kwargs):
        self.alpha = alpha
        self.k = k
        self.beta = beta
        self.n = n
        super(LRN, self).__init__(**kwargs)

    def call(self, x, mask=None):
        b, ch, r, c = x.shape
        half_n = self.n // 2 # half the local region
        input_sqr = K.square(x) #T.math.square(x) # square the input
        extra_channels = K.zeros((b, int(ch) + 2 * half_n, r, c))
        input_sqr = K.concatenate([extra_channels[:, :half_n, :, :],input_sqr, extra_channels[:, half_n + int(ch):, :, :]],axis = 1)

        scale = self.k # offset for the scale
        norm_alpha = self.alpha / self.n # normalized alpha
        for i in range(self.n):
            scale += norm_alpha * input_sqr[:, i:i+ch, :, :]
        scale = scale ** self.beta
        x = x / scale
        return x

    def get_config(self):
        config = {"alpha": self.alpha,
                  "k": self.k,
                  "beta": self.beta,
                  "n": self.n}
        base_config = super(LRN, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))


class PoolHelper(Layer):

    def __init__(self, **kwargs):
        super(PoolHelper, self).__init__(**kwargs)

    def call(self, x, mask=None):
        return x[:,:,1:,1:]

    def get_config(self):
        config = {}
        base_config = super(PoolHelper, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))


其他信息/日志

Using TensorFlow backend.
/usr/lib/python2.7/dist-packages/scipy/misc/pilutil.py:479: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int64 == np.dtype(int).type`.
  if issubdtype(ts, int):
/usr/lib/python2.7/dist-packages/scipy/misc/pilutil.py:482: 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`.
  elif issubdtype(type(size), float):
(8441, 224, 224, 3)
(224, 224, 3)
WARNING: Logging before flag parsing goes to stderr.
W0625 15:17:52.287296 140496712926976 deprecation_wrapper.py:119] From /home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

W0625 15:17:52.299669 140496712926976 deprecation_wrapper.py:119] From /home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0625 15:17:52.300952 140496712926976 deprecation_wrapper.py:119] From /home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

W0625 15:17:52.316469 140496712926976 deprecation_wrapper.py:119] From /home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

Traceback (most recent call last):
  File "neural_net.py", line 63, in <module>
    model = create_googlenet()
  File "/home/robotics/code/projects/husky-sim-test/learning/googlenet.py", line 19, in create_googlenet
    pool1_norm1 = LRN(name='pool1/norm1')(pool1_3x3_s2)
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/keras/engine/base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/robotics/code/projects/husky-sim-test/learning/googlenet_custom_layers.py", line 23, in call
    extra_channels = K.zeros((b, int(ch) + 2 * half_n, r, c))
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 702, in zeros
    v = tf.zeros(shape=shape, dtype=tf_dtype, name=name)
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1880, in zeros
    shape = ops.convert_to_tensor(shape, dtype=dtypes.int32)
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1087, in convert_to_tensor
    return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1145, in convert_to_tensor_v2
    as_ref=False)
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1224, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 305, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 246, in constant
    allow_broadcast=True)
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 284, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "/home/robotics/code/virtualenvs/ros-ml/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 467, in make_tensor_proto
    nparray = np.array(values, dtype=np_dt)
TypeError: __long__ returned non-long (type NoneType)


0 个答案:

没有答案