You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

inputs.py 2.0 kB

4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import tensorlayer as tl
  4. from tensorlayer import logging
  5. from tensorlayer.layers.core import Module
  6. __all__ = ['Input', '_InputLayer']
  7. class _InputLayer(Module):
  8. """
  9. The :class:`Input` class is the starting layer of a neural network.
  10. Parameters
  11. ----------
  12. shape : tuple (int)
  13. Including batch size.
  14. dtype: dtype
  15. The type of input values. By default, tf.float32.
  16. name : None or str
  17. A unique layer name.
  18. """
  19. def __init__(self, shape, dtype=tl.float32, name=None, init=None):
  20. super(_InputLayer, self).__init__(name)
  21. logging.info("Input %s: %s" % (self.name, str(shape)))
  22. self.shape = shape
  23. self.dtype = dtype
  24. self.shape_without_none = [_ if _ is not None else 1 for _ in shape]
  25. if init is None:
  26. self.outputs = tl.initializers.ones()(self.shape_without_none, dtype=self.dtype)
  27. else:
  28. self.outputs = init(self.shape_without_none, dtype=self.dtype)
  29. self._built = True
  30. def __repr__(self):
  31. s = 'Input(shape=%s' % str(self.shape)
  32. if self.name is not None:
  33. s += (', name=\'%s\'' % self.name)
  34. s += ')'
  35. return s
  36. def __call__(self, *args, **kwargs):
  37. return self.outputs
  38. def build(self, inputs_shape):
  39. pass
  40. def forward(self):
  41. return self.outputs
  42. def Input(shape, init=tl.initializers.ones(), dtype=tl.float32, name=None):
  43. """
  44. The :class:`Input` class is the starting layer of a neural network.
  45. Parameters
  46. ----------
  47. shape : tuple (int)
  48. Including batch size.
  49. name : None or str
  50. A unique layer name.
  51. Examples
  52. ---------
  53. With TensorLayer
  54. >>> ni = tl.layers.Input([10, 50, 50, 32], name='input')
  55. >>> output shape : [10, 50, 50, 32]
  56. """
  57. input_layer = _InputLayer(shape, dtype=dtype, name=name, init=init)
  58. outputs = input_layer()
  59. return outputs

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.