Exemplo n.º 1
0
    def test_output_with_max_pooling(self, filters, in_channels, strides,
                                     pool_shape, pool_stride):
        dim_prod = self.input_width * self.input_height * 3
        input_ph = tf.compat.v1.placeholder(tf.float32,
                                            shape=(None, dim_prod),
                                            name='input_flattened')

        # Build a cnn with random filter weights
        with tf.compat.v1.variable_scope('CNN'):
            self.cnn2 = cnn_with_max_pooling(
                input_var=input_ph,
                input_dim=self.input_dim,
                filters=filters,
                strides=strides,
                name='cnn1',
                pool_shapes=(pool_shape, pool_shape),
                pool_strides=(pool_stride, pool_stride),
                padding='VALID',
                hidden_w_init=tf.constant_initializer(1),
                hidden_nonlinearity=self.hidden_nonlinearity)

        self.sess.run(tf.compat.v1.global_variables_initializer())

        obs_input = np.ones((self.batch_size, dim_prod))
        result = self.sess.run(self.cnn2, feed_dict={input_ph: obs_input})

        two_layer = len(filters) == 2
        # get weight values
        with tf.compat.v1.variable_scope('CNN', reuse=True):
            h0_w = tf.compat.v1.get_variable('cnn1/h0/weight').eval()
            h0_b = tf.compat.v1.get_variable('cnn1/h0/bias').eval()
            if two_layer:
                h1_w = tf.compat.v1.get_variable('cnn1/h1/weight').eval()
                h1_b = tf.compat.v1.get_variable('cnn1/h1/bias').eval()

        filter_weights = (h0_w, h1_w) if two_layer else (h0_w, )
        filter_bias = (h0_b, h1_b) if two_layer else (h0_b, )

        input_val = self.obs_input

        # convolution according to TensorFlow's approach
        # and perform max pooling on each layer
        for filter_iter, filter_weight, _filter_bias, in_channel in zip(
                filters, filter_weights, filter_bias, in_channels):
            input_val = convolve(_input=input_val,
                                 filter_weights=(filter_weight, ),
                                 filter_bias=(_filter_bias, ),
                                 strides=strides,
                                 filters=(filter_iter, ),
                                 in_channels=(in_channel, ),
                                 hidden_nonlinearity=self.hidden_nonlinearity)

            # max pooling
            input_val = max_pooling(_input=input_val,
                                    pool_shape=pool_shape,
                                    pool_stride=pool_stride)

        # flatten
        dense_out = input_val.reshape((self.batch_size, -1)).astype(np.float32)
        np.testing.assert_array_equal(dense_out, result)
Exemplo n.º 2
0
    def test_output_with_random_filter(self, filter_sizes, in_channels,
                                       out_channels, strides):
        # Build a cnn with random filter weights
        with tf.variable_scope('CNN'):
            self.cnn2 = cnn(input_var=self._input_ph,
                            filter_dims=filter_sizes,
                            num_filters=out_channels,
                            strides=strides,
                            name='cnn1',
                            padding='VALID',
                            hidden_nonlinearity=self.hidden_nonlinearity)

        self.sess.run(tf.global_variables_initializer())

        result = self.sess.run(self.cnn2,
                               feed_dict={self._input_ph: self.obs_input})

        two_layer = len(filter_sizes) == 2
        # get weight values
        with tf.variable_scope('CNN', reuse=True):
            h0_w = tf.get_variable('cnn1/h0/weight').eval()
            h0_b = tf.get_variable('cnn1/h0/bias').eval()
            if two_layer:
                h1_w = tf.get_variable('cnn1/h1/weight').eval()
                h1_b = tf.get_variable('cnn1/h1/bias').eval()

        filter_weights = (h0_w, h1_w) if two_layer else (h0_w, )
        filter_bias = (h0_b, h1_b) if two_layer else (h0_b, )

        # convolution according to TensorFlow's approach
        input_val = convolve(_input=self.obs_input,
                             filter_weights=filter_weights,
                             filter_bias=filter_bias,
                             strides=strides,
                             filter_sizes=filter_sizes,
                             in_channels=in_channels,
                             hidden_nonlinearity=self.hidden_nonlinearity)

        # flatten
        dense_out = input_val.reshape((self.batch_size, -1)).astype(np.float32)
        np.testing.assert_array_almost_equal(dense_out, result)