Beispiel #1
0
    def test_variable_creation(self):
        weight = np.ones((3, 3))
        var1 = tf_utils.create_variable(weight, name='var1', shape=(3, 3))
        self.assertShapesEqual(var1.shape, (3, 3))

        var2 = tf_utils.create_variable(5, name='var2', shape=(4, 3))
        self.assertShapesEqual(var2.shape, (4, 3))
        np.testing.assert_array_almost_equal(self.eval(var2), 5 * np.ones(
            (4, 3)))

        initializer = init.Normal()
        var3 = tf_utils.create_variable(initializer, name='var3', shape=(4, 7))
        self.assertShapesEqual(var3.shape, (4, 7))

        weight = tf.Variable(np.ones((3, 3)), dtype=tf.float32)
        var4 = tf_utils.create_variable(weight, name='var4', shape=(3, 3))
        self.assertShapesEqual(var4.shape, (3, 3))
        self.assertIs(var4, weight)

        weight = np.ones((3, 4))
        with self.assertRaisesRegexp(ValueError, "Cannot create variable"):
            tf_utils.create_variable(weight, name='var5', shape=(3, 3))

        weight = tf.Variable(np.ones((4, 3)), dtype=tf.float32)
        with self.assertRaisesRegexp(ValueError, "Cannot create variable"):
            tf_utils.create_variable(weight, name='var6', shape=(3, 3))
Beispiel #2
0
    def variable(self, value, name, shape=None, trainable=True):
        layer_name = 'layer/{layer_name}/{parameter_name}'.format(
            layer_name=self.name, parameter_name=name.replace('_', '-'))

        self.variables[name] = tf_utils.create_variable(
            value, layer_name, shape, trainable)

        return self.variables[name]
Beispiel #3
0
    def init_functions(self):
        with tf.variable_scope('rbm'):
            self.weight = tf_utils.create_variable(
                value=self.weight,
                name='weight',
                shape=(self.n_visible, self.n_hidden),
                trainable=False,
            )
            self.hidden_bias = tf_utils.create_variable(
                value=self.hidden_bias,
                name='hidden-bias',
                shape=(self.n_hidden, ),
                trainable=False,
            )
            self.visible_bias = tf_utils.create_variable(
                value=self.visible_bias,
                name='visible-bias',
                shape=(self.n_visible, ),
                trainable=False,
            )
            self.h_samples = tf.Variable(
                tf.zeros([self.batch_size, self.n_hidden]),
                name="hidden-samples",
                dtype=tf.float32,
                trainable=False,
            )

            self.network_input = tf.placeholder(
                tf.float32,
                (None, self.n_visible),
                name="network-input",
            )
            self.network_hidden_input = tf.placeholder(
                tf.float32,
                (None, self.n_hidden),
                name="network-hidden-input",
            )