• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python testing_utils.layer_test函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tensorflow.python.keras.testing_utils.layer_test函数的典型用法代码示例。如果您正苦于以下问题:Python layer_test函数的具体用法?Python layer_test怎么用?Python layer_test使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了layer_test函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: _testKerasLayer

  def _testKerasLayer(self, layer_class):
    def kernel_posterior_fn(dtype, shape, name, trainable, add_variable_fn):
      """Set trivially. The function is required to instantiate layer."""
      del name, trainable, add_variable_fn  # unused
      # Deserialized Keras objects do not perform lexical scoping. Any modules
      # that the function requires must be imported within the function.
      import tensorflow as tf  # pylint: disable=g-import-not-at-top,redefined-outer-name
      tfd = tf.contrib.distributions  # pylint: disable=redefined-outer-name

      dist = tfd.Normal(loc=tf.zeros(shape, dtype), scale=tf.ones(shape, dtype))
      batch_ndims = tf.size(dist.batch_shape_tensor())
      return tfd.Independent(dist, reinterpreted_batch_ndims=batch_ndims)

    kwargs = {'units': 3,
              'kernel_posterior_fn': kernel_posterior_fn,
              'kernel_prior_fn': None,
              'bias_posterior_fn': None,
              'bias_prior_fn': None}
    with tf.keras.utils.CustomObjectScope({layer_class.__name__: layer_class}):
      with self.test_session():
        testing_utils.layer_test(
            layer_class,
            kwargs=kwargs,
            input_shape=(3, 2))
        testing_utils.layer_test(
            layer_class,
            kwargs=kwargs,
            input_shape=(None, None, 2))
开发者ID:lewisKit,项目名称:probability,代码行数:28,代码来源:dense_variational_test.py


示例2: test_locallyconnected_2d

  def test_locallyconnected_2d(self):
    with self.cached_session():
      num_samples = 8
      filters = 3
      stack_size = 4
      num_row = 6
      num_col = 10

      for padding in ['valid', 'same']:
        for strides in [(1, 1), (2, 2)]:
          for implementation in [1, 2]:
            if padding == 'same' and strides != (1, 1):
              continue

            kwargs = {
                'filters': filters,
                'kernel_size': 3,
                'padding': padding,
                'kernel_regularizer': 'l2',
                'bias_regularizer': 'l2',
                'strides': strides,
                'data_format': 'channels_last',
                'implementation': implementation
            }

            if padding == 'same' and implementation == 1:
              self.assertRaises(ValueError,
                                keras.layers.LocallyConnected2D,
                                **kwargs)
            else:
              testing_utils.layer_test(
                  keras.layers.LocallyConnected2D,
                  kwargs=kwargs,
                  input_shape=(num_samples, num_row, num_col, stack_size))
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:34,代码来源:local_test.py


示例3: test_cudnn_rnn_basics

 def test_cudnn_rnn_basics(self):
   if test.is_gpu_available(cuda_only=True):
     with self.test_session(use_gpu=True):
       input_size = 10
       timesteps = 6
       units = 2
       num_samples = 32
       for layer_class in [keras.layers.CuDNNGRU, keras.layers.CuDNNLSTM]:
         for return_sequences in [True, False]:
           with keras.utils.CustomObjectScope(
               {'keras.layers.CuDNNGRU': keras.layers.CuDNNGRU,
                'keras.layers.CuDNNLSTM': keras.layers.CuDNNLSTM}):
             testing_utils.layer_test(
                 layer_class,
                 kwargs={'units': units,
                         'return_sequences': return_sequences},
                 input_shape=(num_samples, timesteps, input_size))
         for go_backwards in [True, False]:
           with keras.utils.CustomObjectScope(
               {'keras.layers.CuDNNGRU': keras.layers.CuDNNGRU,
                'keras.layers.CuDNNLSTM': keras.layers.CuDNNLSTM}):
             testing_utils.layer_test(
                 layer_class,
                 kwargs={'units': units,
                         'go_backwards': go_backwards},
                 input_shape=(num_samples, timesteps, input_size))
开发者ID:AnishShah,项目名称:tensorflow,代码行数:26,代码来源:cudnn_recurrent_test.py


示例4: test_lambda

  def test_lambda(self):
    testing_utils.layer_test(
        keras.layers.Lambda,
        kwargs={'function': lambda x: x + 1},
        input_shape=(3, 2))

    testing_utils.layer_test(
        keras.layers.Lambda,
        kwargs={
            'function': lambda x, a, b: x * a + b,
            'arguments': {
                'a': 0.6,
                'b': 0.4
            }
        },
        input_shape=(3, 2))

    # test serialization with function
    def f(x):
      return x + 1

    ld = keras.layers.Lambda(f)
    config = ld.get_config()
    ld = keras.layers.deserialize({
        'class_name': 'Lambda',
        'config': config
    })

    # test with lambda
    ld = keras.layers.Lambda(
        lambda x: keras.backend.concatenate([math_ops.square(x), x]))
    config = ld.get_config()
    ld = keras.layers.Lambda.from_config(config)
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:33,代码来源:core_test.py


示例5: test_locallyconnected_1d

  def test_locallyconnected_1d(self):
    with self.cached_session():
      num_samples = 2
      num_steps = 8
      input_dim = 5
      filter_length = 3
      filters = 4

      for padding in ['valid', 'same']:
        for strides in [1]:
          if padding == 'same' and strides != 1:
            continue
          for data_format in ['channels_first', 'channels_last']:
            for implementation in [1, 2]:
              kwargs = {
                  'filters': filters,
                  'kernel_size': filter_length,
                  'padding': padding,
                  'strides': strides,
                  'data_format': data_format,
                  'implementation': implementation
              }

              if padding == 'same' and implementation == 1:
                self.assertRaises(ValueError,
                                  keras.layers.LocallyConnected1D,
                                  **kwargs)
              else:
                testing_utils.layer_test(
                    keras.layers.LocallyConnected1D,
                    kwargs=kwargs,
                    input_shape=(num_samples, num_steps, input_dim))
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:32,代码来源:local_test.py


示例6: test_spatial_dropout

  def test_spatial_dropout(self):
    testing_utils.layer_test(
        keras.layers.SpatialDropout1D,
        kwargs={'rate': 0.5},
        input_shape=(2, 3, 4))

    testing_utils.layer_test(
        keras.layers.SpatialDropout2D,
        kwargs={'rate': 0.5},
        input_shape=(2, 3, 4, 5))

    testing_utils.layer_test(
        keras.layers.SpatialDropout2D,
        kwargs={'rate': 0.5, 'data_format': 'channels_first'},
        input_shape=(2, 3, 4, 5))

    testing_utils.layer_test(
        keras.layers.SpatialDropout3D,
        kwargs={'rate': 0.5},
        input_shape=(2, 3, 4, 4, 5))

    testing_utils.layer_test(
        keras.layers.SpatialDropout3D,
        kwargs={'rate': 0.5, 'data_format': 'channels_first'},
        input_shape=(2, 3, 4, 4, 5))
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:25,代码来源:core_test.py


示例7: test_basic_batchnorm

 def test_basic_batchnorm(self):
   testing_utils.layer_test(
       keras.layers.BatchNormalization,
       kwargs={
           'momentum': 0.9,
           'epsilon': 0.1,
           'gamma_regularizer': keras.regularizers.l2(0.01),
           'beta_regularizer': keras.regularizers.l2(0.01)
       },
       input_shape=(3, 4, 2))
   testing_utils.layer_test(
       keras.layers.BatchNormalization,
       kwargs={
           'gamma_initializer': 'ones',
           'beta_initializer': 'ones',
           'moving_mean_initializer': 'zeros',
           'moving_variance_initializer': 'ones'
       },
       input_shape=(3, 4, 2))
   testing_utils.layer_test(
       keras.layers.BatchNormalization,
       kwargs={'scale': False,
               'center': False},
       input_shape=(3, 3))
   testing_utils.layer_test(
       normalization.BatchNormalizationV2,
       kwargs={'fused': True},
       input_shape=(3, 3, 3, 3))
   testing_utils.layer_test(
       normalization.BatchNormalizationV2,
       kwargs={'fused': None},
       input_shape=(3, 3, 3))
开发者ID:aeverall,项目名称:tensorflow,代码行数:32,代码来源:normalization_test.py


示例8: test_upsampling_2d_bilinear

  def test_upsampling_2d_bilinear(self):
    num_samples = 2
    stack_size = 2
    input_num_row = 11
    input_num_col = 12
    for data_format in ['channels_first', 'channels_last']:
      if data_format == 'channels_first':
        inputs = np.random.rand(num_samples, stack_size, input_num_row,
                                input_num_col)
      else:
        inputs = np.random.rand(num_samples, input_num_row, input_num_col,
                                stack_size)

      testing_utils.layer_test(keras.layers.UpSampling2D,
                               kwargs={'size': (2, 2),
                                       'data_format': data_format,
                                       'interpolation': 'bilinear'},
                               input_shape=inputs.shape)

      if not context.executing_eagerly():
        for length_row in [2]:
          for length_col in [2, 3]:
            layer = keras.layers.UpSampling2D(
                size=(length_row, length_col),
                data_format=data_format)
            layer.build(inputs.shape)
            outputs = layer(keras.backend.variable(inputs))
            np_output = keras.backend.eval(outputs)
            if data_format == 'channels_first':
              self.assertEqual(np_output.shape[2], length_row * input_num_row)
              self.assertEqual(np_output.shape[3], length_col * input_num_col)
            else:
              self.assertEqual(np_output.shape[1], length_row * input_num_row)
              self.assertEqual(np_output.shape[2], length_col * input_num_col)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:34,代码来源:convolutional_test.py


示例9: test_relu_with_invalid_arg

 def test_relu_with_invalid_arg(self):
   with self.assertRaisesRegexp(
       ValueError, 'max_value of Relu layer cannot be negative value: -10'):
     with self.test_session():
       testing_utils.layer_test(keras.layers.ReLU,
                                kwargs={'max_value': -10},
                                input_shape=(2, 3, 4))
开发者ID:Huoxubeiyin,项目名称:tensorflow,代码行数:7,代码来源:advanced_activations_test.py


示例10: test_locallyconnected_2d_channels_first

  def test_locallyconnected_2d_channels_first(self):
    with self.cached_session():
      num_samples = 8
      filters = 3
      stack_size = 4
      num_row = 6
      num_col = 10

      for implementation in [1, 2]:
        for padding in ['valid', 'same']:
          kwargs = {
              'filters': filters,
              'kernel_size': 3,
              'data_format': 'channels_first',
              'implementation': implementation,
              'padding': padding
          }

          if padding == 'same' and implementation == 1:
            self.assertRaises(ValueError,
                              keras.layers.LocallyConnected2D,
                              **kwargs)
          else:
            testing_utils.layer_test(
                keras.layers.LocallyConnected2D,
                kwargs=kwargs,
                input_shape=(num_samples, num_row, num_col, stack_size))
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:27,代码来源:local_test.py


示例11: test_averagepooling_1d

 def test_averagepooling_1d(self):
   for padding in ['valid', 'same']:
     for stride in [1, 2]:
       testing_utils.layer_test(
           keras.layers.AveragePooling1D,
           kwargs={'strides': stride,
                   'padding': padding},
           input_shape=(3, 5, 4))
开发者ID:LiuCKind,项目名称:tensorflow,代码行数:8,代码来源:pooling_test.py


示例12: test_basic_batchnorm_v2

 def test_basic_batchnorm_v2(self):
   testing_utils.layer_test(
       normalization.BatchNormalizationV2,
       kwargs={'fused': True},
       input_shape=(3, 3, 3, 3))
   testing_utils.layer_test(
       normalization.BatchNormalizationV2,
       kwargs={'fused': None},
       input_shape=(3, 3, 3))
开发者ID:rmlarsen,项目名称:tensorflow,代码行数:9,代码来源:normalization_test.py


示例13: test_dropout

  def test_dropout(self):
    testing_utils.layer_test(
        keras.layers.Dropout, kwargs={'rate': 0.5}, input_shape=(3, 2))

    testing_utils.layer_test(
        keras.layers.Dropout,
        kwargs={'rate': 0.5,
                'noise_shape': [3, 1]},
        input_shape=(3, 2))
开发者ID:terrytangyuan,项目名称:tensorflow,代码行数:9,代码来源:core_test.py


示例14: test_cudnn_rnn_return_sequence

 def test_cudnn_rnn_return_sequence(self, layer_class, return_sequences):
   input_size = 10
   timesteps = 6
   units = 2
   num_samples = 32
   testing_utils.layer_test(
       layer_class,
       kwargs={'units': units,
               'return_sequences': return_sequences},
       input_shape=(num_samples, timesteps, input_size))
开发者ID:aritratony,项目名称:tensorflow,代码行数:10,代码来源:cudnn_recurrent_test.py


示例15: _run_test

  def _run_test(self, kwargs):
    num_samples = 2
    stack_size = 3
    length = 7

    with self.cached_session(use_gpu=True):
      testing_utils.layer_test(
          keras.layers.Conv1D,
          kwargs=kwargs,
          input_shape=(num_samples, length, stack_size))
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:10,代码来源:convolutional_test.py


示例16: test_implementation_mode_GRU

 def test_implementation_mode_GRU(self, implementation_mode):
   num_samples = 2
   timesteps = 3
   embedding_dim = 4
   units = 2
   testing_utils.layer_test(
       keras.layers.UnifiedGRU,
       kwargs={'units': units,
               'implementation': implementation_mode},
       input_shape=(num_samples, timesteps, embedding_dim))
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:10,代码来源:unified_gru_test.py


示例17: test_cudnn_rnn_go_backward

 def test_cudnn_rnn_go_backward(self, layer_class, go_backwards):
   input_size = 10
   timesteps = 6
   units = 2
   num_samples = 32
   testing_utils.layer_test(
       layer_class,
       kwargs={'units': units,
               'go_backwards': go_backwards},
       input_shape=(num_samples, timesteps, input_size))
开发者ID:aritratony,项目名称:tensorflow,代码行数:10,代码来源:cudnn_recurrent_test.py


示例18: test_return_sequences_GRU

 def test_return_sequences_GRU(self):
   num_samples = 2
   timesteps = 3
   embedding_dim = 4
   units = 2
   testing_utils.layer_test(
       keras.layers.GRU,
       kwargs={'units': units,
               'return_sequences': True},
       input_shape=(num_samples, timesteps, embedding_dim))
开发者ID:ZhangXinNan,项目名称:tensorflow,代码行数:10,代码来源:gru_test.py


示例19: test_upsampling_3d

  def test_upsampling_3d(self):
    num_samples = 2
    stack_size = 2
    input_len_dim1 = 10
    input_len_dim2 = 11
    input_len_dim3 = 12

    for data_format in ['channels_first', 'channels_last']:
      if data_format == 'channels_first':
        inputs = np.random.rand(num_samples, stack_size, input_len_dim1,
                                input_len_dim2, input_len_dim3)
      else:
        inputs = np.random.rand(num_samples, input_len_dim1, input_len_dim2,
                                input_len_dim3, stack_size)

      # basic test
      with self.test_session(use_gpu=True):
        testing_utils.layer_test(
            keras.layers.UpSampling3D,
            kwargs={'size': (2, 2, 2),
                    'data_format': data_format},
            input_shape=inputs.shape)

        for length_dim1 in [2, 3]:
          for length_dim2 in [2]:
            for length_dim3 in [3]:
              layer = keras.layers.UpSampling3D(
                  size=(length_dim1, length_dim2, length_dim3),
                  data_format=data_format)
              layer.build(inputs.shape)
              output = layer(keras.backend.variable(inputs))
              if context.executing_eagerly():
                np_output = output.numpy()
              else:
                np_output = keras.backend.eval(output)
              if data_format == 'channels_first':
                assert np_output.shape[2] == length_dim1 * input_len_dim1
                assert np_output.shape[3] == length_dim2 * input_len_dim2
                assert np_output.shape[4] == length_dim3 * input_len_dim3
              else:  # tf
                assert np_output.shape[1] == length_dim1 * input_len_dim1
                assert np_output.shape[2] == length_dim2 * input_len_dim2
                assert np_output.shape[3] == length_dim3 * input_len_dim3

              # compare with numpy
              if data_format == 'channels_first':
                expected_out = np.repeat(inputs, length_dim1, axis=2)
                expected_out = np.repeat(expected_out, length_dim2, axis=3)
                expected_out = np.repeat(expected_out, length_dim3, axis=4)
              else:  # tf
                expected_out = np.repeat(inputs, length_dim1, axis=1)
                expected_out = np.repeat(expected_out, length_dim2, axis=2)
                expected_out = np.repeat(expected_out, length_dim3, axis=3)

              np.testing.assert_allclose(np_output, expected_out)
开发者ID:didukhle,项目名称:tensorflow,代码行数:55,代码来源:convolutional_test.py


示例20: test_maxpooling_2d

 def test_maxpooling_2d(self):
   pool_size = (3, 3)
   for strides in [(1, 1), (2, 2)]:
     testing_utils.layer_test(
         keras.layers.MaxPooling2D,
         kwargs={
             'strides': strides,
             'padding': 'valid',
             'pool_size': pool_size
         },
         input_shape=(3, 5, 6, 4))
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:11,代码来源:pooling_test.py



注:本文中的tensorflow.python.keras.testing_utils.layer_test函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python testing_utils.should_run_eagerly函数代码示例发布时间:2022-05-27
下一篇:
Python testing_utils.get_test_data函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap