def testConstructAllWithConflictingValues(self):
    labels = numpy.array([[0., 1.], [1., 0.]], dtype=numpy.float32)
    template = self.Template('input').flatten().softmax_classifier(2, labels)

    softmax = template.softmax.bind(input=self.input)
    loss = template.loss.bind(input=labels)
    with self.assertRaises(ValueError):
      prettytensor.construct_all([softmax, loss])
예제 #2
0
def main(argv=None):
    input.init_dataset_constants()
    num_images = GRID[0] * GRID[1]
    FLAGS.batch_size = num_images
    with tf.Graph().as_default():
        g_template = model.generator_template()
        z = tf.placeholder(tf.float32, shape=[FLAGS.batch_size, FLAGS.z_size])
        #np.random.seed(1337) # generate same random numbers each time
        noise = np.random.normal(size=(FLAGS.batch_size, FLAGS.z_size))
        with pt.defaults_scope(phase=pt.Phase.test):
            gen_images_op, _ = pt.construct_all(g_template, input=z)

        sess = tf.Session()
        init_variables(sess)
        gen_images, = sess.run([gen_images_op], feed_dict={z: noise})
        gen_images = (gen_images + 1) / 2

        sess.close()

        fig = plt.figure(1)
        grid = ImageGrid(fig, 111, nrows_ncols=GRID, axes_pad=0.1)
        for i in xrange(num_images):
            im = gen_images[i]
            axis = grid[i]
            axis.axis('off')
            axis.imshow(im)

        plt.show()
        fig.savefig('montage.png', dpi=100, bbox_inches='tight')
예제 #3
0
def losses(real_images):
    # get z
    z = tf.truncated_normal([FLAGS.batch_size, FLAGS.z_size], stddev=1)
    #z = tf.random_uniform([FLAGS.batch_size, FLAGS.z_size], minval=-1, maxval=1)

    d_template = discriminator_template()
    g_template = generator_template()

    gen_images, z_prediction = pt.construct_all(g_template, input=z)
    tf.image_summary('generated_images',
                     gen_images,
                     max_images=FLAGS.batch_size,
                     name='generated_images_summary')

    real_logits = d_template.construct(input=real_images)
    fake_logits = d_template.construct(input=gen_images)

    real_loss = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(real_logits,
                                                tf.ones_like(real_logits)))
    fake_loss = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(fake_logits,
                                                tf.zeros_like(fake_logits)))
    discriminator_loss = tf.add(real_loss,
                                fake_loss,
                                name='discriminator_loss')

    generator_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
        fake_logits, tf.ones_like(fake_logits)),
                                    name='generator_loss')

    z_prediction_loss = tf.reduce_mean(tf.square(z - z_prediction),
                                       name='z_prediction_loss')

    tf.add_to_collection('losses', generator_loss)
    tf.add_to_collection('losses', discriminator_loss)
    tf.add_to_collection('losses', z_prediction_loss)

    return generator_loss, discriminator_loss, z_prediction_loss
예제 #4
0
    def _build_graph(self):
        # build up the hidden Z
        z = tf.truncated_normal([self.batch_size, self.hidden_size], stddev=1)

        # the training step
        global_step = tf.Variable(0, trainable=False)

        # build template
        discriminator_template = model.build_discriminator_template(
            self.version)
        generator_template = model.build_generator_template(
            self.version, self.hidden_size)

        # instance the template
        self.g_out, z_prediction = pt.construct_all(generator_template,
                                                    input=z)
        real_disc_inst = discriminator_template.construct(
            input=customDataGeter.input(self.data_directory, self.img_size,
                                        self.batch_size))
        fake_disc_inst = discriminator_template.construct(input=self.g_out)

        if self.version == 2:
            self.discriminator_loss = tf.reduce_mean(fake_disc_inst -
                                                     real_disc_inst,
                                                     name='discriminator_loss')
            self.generator_loss = tf.reduce_mean(-fake_disc_inst,
                                                 name='generator_loss')
        if self.version == 1:
            self.discriminator_loss = tf.add(
                tf.reduce_mean(
                    tf.nn.sigmoid_cross_entropy_with_logits(
                        real_disc_inst, tf.ones_like(real_disc_inst))),
                tf.reduce_mean(
                    tf.nn.sigmoid_cross_entropy_with_logits(
                        fake_disc_inst, tf.zeros_like(fake_disc_inst))),
                name='discriminator_loss')
            self.generator_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(
                    fake_disc_inst, tf.ones_like(fake_disc_inst)),
                name='generator_loss')

        self.z_prediction_loss = tf.reduce_mean(tf.square(z - z_prediction),
                                                name='z_prediction_loss')

        # build the optimization operator (RMS no better than adam.)
        if self.version == 1:
            self.opt_d = tf.train.AdamOptimizer(
                self.learning_rate,
                beta1=0.5).minimize(self.discriminator_loss,
                                    global_step,
                                    var_list=tf.get_collection(
                                        tf.GraphKeys.TRAINABLE_VARIABLES,
                                        'discriminator'))
            self.opt_g = tf.train.AdamOptimizer(
                self.learning_rate,
                beta1=0.5).minimize(self.generator_loss,
                                    global_step,
                                    var_list=tf.get_collection(
                                        tf.GraphKeys.TRAINABLE_VARIABLES,
                                        'generator'))
        if self.version == 2:
            self.opt_d = tf.train.RMSPropOptimizer(
                self.learning_rate).minimize(
                    self.discriminator_loss,
                    global_step,
                    var_list=tf.get_collection(
                        tf.GraphKeys.TRAINABLE_VARIABLES, 'discriminator'))
            self.opt_g = tf.train.RMSPropOptimizer(
                self.learning_rate).minimize(
                    self.generator_loss,
                    global_step,
                    var_list=tf.get_collection(
                        tf.GraphKeys.TRAINABLE_VARIABLES, 'generator'))

        self.opt_z = tf.train.AdamOptimizer(
            self.learning_rate, beta1=0.5).minimize(
                self.z_prediction_loss,
                global_step,
                var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                           'generator'))

        if self.version == 2:
            # define the clip op
            clipped_var_c = [
                tf.assign(var,
                          tf.clip_by_value(var, -self.clip_abs, self.clip_abs))
                for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                             'discriminator')
            ]
            # merge the clip operations on critic variables
            with tf.control_dependencies([self.opt_d]):
                self.opt_d = tf.tuple(clipped_var_c)