def _build_decoder(self, weight_init=tf.truncated_normal): """Construct decoder network: placeholders, operations, optimizer, extract gradient back-prop for encoding layer""" self._clamped = tf.placeholder(tf.float32, (FLAGS.batch_size, self.layer_narrow)) self._reconstruction = tf.placeholder(tf.float32, self._batch_shape) clamped_init = np.zeros((FLAGS.batch_size, self.layer_narrow), dtype=np.float32) self._clamped_variable = tf.Variable(clamped_init, name='clamped') self._assign_clamped = tf.assign(self._clamped_variable, self._clamped) self._decode = pt.wrap(self._clamped_variable) # self._decode = self._decode.reshape([FLAGS.batch_size, 1, 1, self.layer_narrow]) print(self._decode.get_shape()) self._decode = self._decode.fully_connected(7200) self._decode = self._decode.reshape([FLAGS.batch_size, 1, 1, 7200]) self._decode = self._decode.deconv2d((10, 20), 128, edges='VALID') print(self._decode.get_shape()) self._decode = self._decode.deconv2d(5, 64, stride=2) print(self._decode.get_shape()) self._decode = self._decode.deconv2d(5, 32, stride=2) print(self._decode.get_shape()) self._decode = self._decode.deconv2d(5, 3, stride=2, activation_fn=tf.nn.sigmoid) print(self._decode.get_shape()) # variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.decoder_scope) self._decoder_loss = self._decode.l2_regression(pt.wrap(self._reconstruction)) self._opt_decoder = self._optimizer(learning_rate=FLAGS.learning_rate/FLAGS.batch_size) self._train_decoder = self._opt_decoder.minimize(self._decoder_loss) self._clamped_grad, = tf.gradients(self._decoder_loss, [self._clamped_variable])
def __init__(self,params,name): with tf.variable_scope(name): self.network_type = 'nips' self.params = params self.network_name = name self.x = tf.placeholder('float32',[None,84,84,4]) self.q_t = tf.placeholder('float32',[None]) self.actions = tf.placeholder("float32", [None, params['num_act']]) self.rewards = tf.placeholder("float32", [None]) self.terminals = tf.placeholder("float32", [None]) x_pt = pt.wrap(self.x).sequential() x_pt.conv2d(8, 16, stride=4, edges='VALID', activation_fn=tf.nn.relu) x_pt.conv2d(4, 32, stride=3, edges='VALID', activation_fn=tf.nn.relu) x_pt.flatten() x_pt.fully_connected(256, activation_fn=tf.nn.relu) x_pt.fully_connected(params['num_act']) self.y = x_pt.as_layer() #Q,Cost,Optimizer self.discount = tf.constant(self.params['discount']) self.yj = tf.add(self.rewards, tf.mul(1.0-self.terminals, tf.mul(self.discount, self.q_t))) self.Qxa = tf.mul(self.y, self.actions) self.Q_pred = tf.reduce_max(self.Qxa, reduction_indices=1) self.cost = pt.wrap(self.Q_pred).l2_regression(self.yj) self.global_step = tf.Variable(0, name='global_step', trainable=False) self.rmsprop = tf.train.RMSPropOptimizer(self.params['lr'],self.params['rms_decay'],0.0,self.params['rms_eps']).minimize(self.cost, global_step=self.global_step)
def _build_encoder(self): """Construct encoder network: placeholders, operations, optimizer""" self._input = tf.placeholder(tf.float32, self._batch_shape, name='input') self._encoding = tf.placeholder(tf.float32, (FLAGS.batch_size, self.layer_narrow), name='encoding') self._encode = (pt.wrap(self._input) .flatten() .fully_connected(self.layer_encoder, name='enc_hidden') .fully_connected(self.layer_narrow, name='narrow')) self._encode = pt.wrap(self._input) self._encode = self._encode.conv2d(5, 32, stride=2) print(self._encode.get_shape()) self._encode = self._encode.conv2d(5, 64, stride=2) print(self._encode.get_shape()) self._encode = self._encode.conv2d(5, 128, stride=2) print(self._encode.get_shape()) self._encode = (self._encode.dropout(0.9). flatten(). fully_connected(self.layer_narrow, activation_fn=None)) # variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.encoder_scope) self._encoder_loss = self._encode.l1_regression(pt.wrap(self._encoding)) ut.print_info('new learning rate: %.8f (%f)' % (FLAGS.learning_rate/FLAGS.batch_size, FLAGS.learning_rate)) self._opt_encoder = self._optimizer(learning_rate=FLAGS.learning_rate/FLAGS.batch_size) self._train_encoder = self._opt_encoder.minimize(self._encoder_loss)
def testBinaryCrossEntropy(self): n1 = numpy.array([[2., 3., 4., 5., -6., -7.]], dtype=numpy.float32) n2 = numpy.array([[1., 1., 0., 0., 0., 1.]], dtype=numpy.float32) ftensor1 = prettytensor.wrap(n1) ftensor2 = prettytensor.wrap(n2) out = self.RunTensor( ftensor1.binary_cross_entropy_with_logits(ftensor2)) testing.assert_allclose( out, numpy.sum(n1 * (1-n2) + numpy.log(1 + numpy.exp(-n1)), axis=1), rtol=0.00001)
def testSparseCrossEntropyWithPerOutputWeights(self): n1 = numpy.array([[2., 3., 4.], [5., -6., -7.]], dtype=numpy.float32) n2 = numpy.array([0, 2], dtype=numpy.int32) weights = numpy.array([0.5, 0.75]) ftensor1 = prettytensor.wrap(n1) ftensor2 = prettytensor.wrap(n2) out = self.RunTensor(ftensor1.sparse_cross_entropy( n2, per_example_weights=weights)) testing.assert_allclose(out, 5.10191011428833, rtol=0.00001)
def testBinaryCrossEntropyWithPerOutputWeights(self): n1 = numpy.array([[2., 3., 4.], [5., -6., -7.]], dtype=numpy.float32) n2 = numpy.array([[1., 1., 0.], [0., 0., 1.]], dtype=numpy.float32) ftensor1 = prettytensor.wrap(n1) ftensor2 = prettytensor.wrap(n2) weights = numpy.random.rand(*n1.shape).astype(numpy.float32) out = self.RunTensor(ftensor1.binary_cross_entropy_with_logits( ftensor2, per_output_weights=weights)) expected = (numpy.sum((n1 * (1 - n2) + numpy.log(1 + numpy.exp(-n1))) * weights) / len(n1)) testing.assert_allclose(out, expected, rtol=0.00001)
def testGraphMatchesImmediate(self): """Ensures that the vars line up between the two modes.""" with tf.Graph().as_default(): input_pt = prettytensor.wrap(self.input) self.BuildLargishGraph(input_pt) normal_names = sorted([v.name for v in tf.all_variables()]) with tf.Graph().as_default(): template = prettytensor.template('input') self.BuildLargishGraph(template).construct( input=prettytensor.wrap(self.input)) template_names = sorted([v.name for v in tf.all_variables()]) self.assertSequenceEqual(normal_names, template_names)
def fc(): with tf.variable_scope('fc'): with pt.defaults_scope(activation_fn=tf.nn.relu): fc_seq = pt.wrap(x).sequential() fc_seq.fully_connected(10, activation_fn=None) fc_seq.softmax() return tf.nn.softmax(fc_seq)
def mapping(self, x): """ lambda = phi(x) """ with pt.defaults_scope( activation_fn=tf.nn.elu, batch_normalize=True, learned_moments_update_rate=0.0003, variance_epsilon=0.001, scale_after_normalization=True, ): params = ( pt.wrap(x) .reshape([FLAGS.n_data, 28, 28, 1]) .conv2d(5, 32, stride=2) .conv2d(5, 64, stride=2) .conv2d(5, 128, edges="VALID") .dropout(0.9) .flatten() .fully_connected(self.num_vars * 2, activation_fn=None) ).tensor mean = params[:, : self.num_vars] stddev = tf.sqrt(tf.exp(params[:, self.num_vars :])) return [mean, stddev]
def reshape_cleavable(tensor, per_example_length=1): """Reshapes input so that it is appropriate for sequence_lstm.. The expected format for sequence lstms is [timesteps * batch, per_example_length] and the data produced by the readers in samyro.read is [batch, timestep, *optional* expected_length]. The result can be cleaved so that there is a Tensor per timestep. Args: tensor: The tensor to reshape. per_example_length: The number of examples at each timestep. Returns: A Pretty Tensor that is compatible with cleave and then sequence_lstm. Largely borrowed from prettytensor.tutorial.data_utils. """ # We can put the data into a format that can be easily cleaved by # transposing it (so that it varies fastest in batch) and then making each # component have a single value. # This will make it compatible with the Pretty Tensor function # cleave_sequence. dims = [1, 0] for i in xrange(2, tensor.get_shape().ndims): dims.append(i) transposed = prettytensor.wrap( tensorflow.transpose(tensor, dims)) return transposed.reshape([-1, per_example_length])
def mapping(self, x): """ Inference network to parameterize variational family. It takes data x as input and outputs the variational parameters lambda. lambda = phi(x) """ with pt.defaults_scope(activation_fn=tf.nn.elu, batch_normalize=True, learned_moments_update_rate=0.0003, variance_epsilon=0.001, scale_after_normalization=True): params = (pt.wrap(x). reshape([FLAGS.n_data, 28, 28, 1]). conv2d(5, 32, stride=2). conv2d(5, 64, stride=2). conv2d(5, 128, edges='VALID'). dropout(0.9). flatten(). fully_connected(self.num_local_vars * 2, activation_fn=None)).tensor # Return list of vectors where mean[i], stddev[i] are the # parameters of the local variational factor for data point i. mean = tf.reshape(params[:, :self.num_local_vars], [-1]) stddev = tf.reshape(tf.sqrt(tf.exp(params[:, self.num_local_vars:])), [-1]) return [mean, stddev]
def multilayer_fully_connected(images, labels): images = pt.wrap(images) with pt.defaults_scope(activation_fn=tf.nn.relu, l2loss=0.00001): return (images .fully_connected(100) .fully_connected(100) .softmax_classifier(10, labels))
def encoder(self, input_tensor): template = (pt.wrap(input_tensor) .flatten() .fully_connected(self.layer_encoder) .fully_connected(self.layer_encoder) .fully_connected(self.layer_narrow)) return template
def decoder(input_tensor=None): '''Create decoder network. If input tensor is provided then decodes it, otherwise samples from a sampled vector. Args: input_tensor: a batch of vectors to decode Returns: A tensor that expresses the decoder network ''' epsilon = tf.random_normal([FLAGS.batch_size, FLAGS.hidden_size]) if input_tensor is None: mean = None stddev = None input_sample = epsilon else: mean = input_tensor[:, :FLAGS.hidden_size] stddev = tf.sqrt(tf.exp(input_tensor[:, FLAGS.hidden_size:])) input_sample = mean + epsilon * stddev return (pt.wrap(input_sample). reshape([FLAGS.batch_size, 1, 1, FLAGS.hidden_size]). deconv2d(3, 128, edges='VALID'). deconv2d(5, 64, edges='VALID'). deconv2d(5, 32, stride=2). deconv2d(5, 1, stride=2, activation_fn=tf.nn.sigmoid). flatten()).tensor, mean, stddev
def neural_network(x): """ Inference network to parameterize variational family. It takes data as input and outputs the variational parameters. loc, scale = neural_network(x) """ num_vars = 10 with pt.defaults_scope(activation_fn=tf.nn.elu, batch_normalize=True, learned_moments_update_rate=0.0003, variance_epsilon=0.001, scale_after_normalization=True): params = (pt.wrap(x). reshape([N_DATA, 28, 28, 1]). conv2d(5, 32, stride=2). conv2d(5, 64, stride=2). conv2d(5, 128, edges='VALID'). dropout(0.9). flatten(). fully_connected(num_vars * 2, activation_fn=None)).tensor # Return list of vectors where mean[i], stddev[i] are the # parameters of the local variational factor for data point i. loc = tf.reshape(params[:, :num_vars], [-1]) scale = tf.reshape(tf.sqrt(tf.exp(params[:, num_vars:])), [-1]) return [loc, scale]
def generator(self, z_var): node1_0 =\ (pt.wrap(z_var). flatten(). custom_fully_connected(self.s16 * self.s16 * self.gf_dim * 8). fc_batch_norm(). reshape([-1, self.s16, self.s16, self.gf_dim * 8])) node1_1 = \ (node1_0. custom_conv2d(self.gf_dim * 2, k_h=1, k_w=1, d_h=1, d_w=1). conv_batch_norm(). apply(tf.nn.relu). custom_conv2d(self.gf_dim * 2, k_h=3, k_w=3, d_h=1, d_w=1). conv_batch_norm(). apply(tf.nn.relu). custom_conv2d(self.gf_dim * 8, k_h=3, k_w=3, d_h=1, d_w=1). conv_batch_norm()) node1 = \ (node1_0. apply(tf.add, node1_1). apply(tf.nn.relu)) node2_0 = \ (node1. # custom_deconv2d([0, self.s8, self.s8, self.gf_dim * 4], k_h=4, k_w=4). apply(tf.image.resize_nearest_neighbor, [self.s8, self.s8]). custom_conv2d(self.gf_dim * 4, k_h=3, k_w=3, d_h=1, d_w=1). conv_batch_norm()) node2_1 = \ (node2_0. custom_conv2d(self.gf_dim * 1, k_h=1, k_w=1, d_h=1, d_w=1). conv_batch_norm(). apply(tf.nn.relu). custom_conv2d(self.gf_dim * 1, k_h=3, k_w=3, d_h=1, d_w=1). conv_batch_norm(). apply(tf.nn.relu). custom_conv2d(self.gf_dim * 4, k_h=3, k_w=3, d_h=1, d_w=1). conv_batch_norm()) node2 = \ (node2_0. apply(tf.add, node2_1). apply(tf.nn.relu)) output_tensor = \ (node2. # custom_deconv2d([0, self.s4, self.s4, self.gf_dim * 2], k_h=4, k_w=4). apply(tf.image.resize_nearest_neighbor, [self.s4, self.s4]). custom_conv2d(self.gf_dim * 2, k_h=3, k_w=3, d_h=1, d_w=1). conv_batch_norm(). apply(tf.nn.relu). # custom_deconv2d([0, self.s2, self.s2, self.gf_dim], k_h=4, k_w=4). apply(tf.image.resize_nearest_neighbor, [self.s2, self.s2]). custom_conv2d(self.gf_dim, k_h=3, k_w=3, d_h=1, d_w=1). conv_batch_norm(). apply(tf.nn.relu). # custom_deconv2d([0] + list(self.image_shape), k_h=4, k_w=4). apply(tf.image.resize_nearest_neighbor, [self.s, self.s]). custom_conv2d(3, k_h=3, k_w=3, d_h=1, d_w=1). apply(tf.nn.tanh)) return output_tensor
def testSparseCrossEntropy(self): n1 = numpy.array([[2., 3., 4., 5., -6., -7.]], dtype=numpy.float32) n2 = numpy.array([0], dtype=numpy.int32) ftensor1 = prettytensor.wrap(n1) out = self.RunTensor(ftensor1.sparse_cross_entropy(n2)) testing.assert_allclose(out, 3.440204381942749, rtol=0.00001)
def output( self, inputs, phase = pt.Phase.train ): inputs = pt.wrap( inputs ) with pt.defaults_scope( phase = phase, activation_fn = self.nonlinearity, l2loss = self.l2loss ): for layer in self.hidden_layers: inputs = inputs.fully_connected( layer ) return inputs.fully_connected( self.dim_output, activation_fn = None )
def testFullWithVariableStart(self): input_layer = prettytensor.wrap(tf.Variable(self.input_data)) st = input_layer.sequential() st.flatten() st.fully_connected(20) result = self.RunTensor(st) self.assertEqual(20, result.shape[1])
def testSparseCrossEntropyBadInputs(self): n1 = numpy.array([[2., 3., 4., 5., -6., -7.]], dtype=numpy.float32) n2 = numpy.array([0], dtype=numpy.int32) ftensor1 = prettytensor.wrap(n1) with self.assertRaises(TypeError): ftensor1.sparse_cross_entropy(numpy.array([0.0])) with self.assertRaises(ValueError): ftensor1.sparse_cross_entropy(numpy.array([[0]]))
def __init__(self, env): self.env = env if not isinstance(env.observation_space, Box) or \ not isinstance(env.action_space, Discrete): print("Incompatible spaces.") exit(-1) print("Observation Space", env.observation_space) print("Action Space", env.action_space) self.session = tf.Session() self.end_count = 0 self.train = True self.obs = obs = tf.placeholder( dtype, shape=[ None, 2 * env.observation_space.shape[0] + env.action_space.n], name="obs") self.prev_obs = np.zeros((1, env.observation_space.shape[0])) self.prev_action = np.zeros((1, env.action_space.n)) self.action = action = tf.placeholder(tf.int64, shape=[None], name="action") self.advant = advant = tf.placeholder(dtype, shape=[None], name="advant") self.oldaction_dist = oldaction_dist = tf.placeholder(dtype, shape=[None, env.action_space.n], name="oldaction_dist") # Create neural network. action_dist_n, _ = (pt.wrap(self.obs). fully_connected(64, activation_fn=tf.nn.tanh). softmax_classifier(env.action_space.n)) eps = 1e-6 self.action_dist_n = action_dist_n N = tf.shape(obs)[0] p_n = slice_2d(action_dist_n, tf.range(0, N), action) oldp_n = slice_2d(oldaction_dist, tf.range(0, N), action) ratio_n = p_n / oldp_n Nf = tf.cast(N, dtype) surr = -tf.reduce_mean(ratio_n * advant) # Surrogate loss var_list = tf.trainable_variables() kl = tf.reduce_sum(oldaction_dist * tf.log((oldaction_dist + eps) / (action_dist_n + eps))) / Nf ent = tf.reduce_sum(-action_dist_n * tf.log(action_dist_n + eps)) / Nf self.losses = [surr, kl, ent] self.pg = flatgrad(surr, var_list) # KL divergence where first arg is fixed # replace old->tf.stop_gradient from previous kl kl_firstfixed = tf.reduce_sum(tf.stop_gradient( action_dist_n) * tf.log(tf.stop_gradient(action_dist_n + eps) / (action_dist_n + eps))) / Nf grads = tf.gradients(kl_firstfixed, var_list) self.flat_tangent = tf.placeholder(dtype, shape=[None]) shapes = map(var_shape, var_list) start = 0 tangents = [] for shape in shapes: size = np.prod(shape) param = tf.reshape(self.flat_tangent[start:(start + size)], shape) tangents.append(param) start += size gvp = [tf.reduce_sum(g * t) for (g, t) in zip(grads, tangents)] self.fvp = flatgrad(gvp, var_list) self.gf = GetFlat(self.session, var_list) self.sff = SetFromFlat(self.session, var_list) self.vf = VF(self.session) self.session.run(tf.initialize_all_variables())
def encoder(self, input_tensor): print('Convolutional encoder') template = (pt.wrap(input_tensor). conv2d(5, 32, stride=2). conv2d(5, 64, stride=2). conv2d(5, 128, edges='VALID') .dropout(FLAGS.dropout).flatten() .fully_connected(self.layer_narrow)) return template
def construct_policy_net(obs, action_dim): mean = ( pt.wrap(obs).fully_connected(64, activation_fn=tf.nn.relu, stddev=0.01). fully_connected(64, activation_fn=tf.nn.relu, stddev=0.01). fully_connected(action_dim, activation_fn=None, stddev=0.01)) action_dist_logstd_param = tf.Variable((.01*np.random.randn(1, action_dim)).astype(np.float32)) action_dist_logstd = tf.tile(action_dist_logstd_param, tf.pack((tf.shape(mean)[0], 1))) std = tf.exp(action_dist_logstd) return tf.concat(1, [mean, std])
def generate_condition(self, c_var): conditions =\ (pt.wrap(c_var). flatten(). custom_fully_connected(self.ef_dim * 2). apply(leaky_rectify, leakiness=0.2)) mean = conditions[:, :self.ef_dim] log_sigma = conditions[:, self.ef_dim:] return [mean, log_sigma]
def __init__(self, env, H, timesteps_per_batch, learning_rate, gamma, epochs, dropout): if not isinstance(env.observation_space, Box) or \ not isinstance(env.action_space, Discrete): logger.error("Incompatible spaces.") exit(-1) self.H = H self.timesteps_per_batch = timesteps_per_batch self.learning_rate = learning_rate self.gamma = gamma self.epochs = epochs self.dropout = dropout self.env = env self.session = tf.Session() # Full state used for next action prediction. Contains current # observation, previous observation and previous action. self.obs = tf.placeholder( DTYPE, shape=[None, 2 * env.observation_space.shape[0] + env.action_space.n], name="obs") self.prev_obs = np.zeros(env.observation_space.shape[0]) self.prev_action = np.zeros(env.action_space.n) # One hot encoding of the actual action taken. self.action = tf.placeholder(DTYPE, shape=[None, env.action_space.n], name="action") # Advantage, obviously. self.advant = tf.placeholder(DTYPE, shape=[None], name="advant") # Old policy prediction. self.prev_policy = tf.placeholder(DTYPE, shape=[None, env.action_space.n], name="prev_policy") self.policy_network, _ = ( pt.wrap(self.obs) .fully_connected(H, activation_fn=tf.nn.tanh) .dropout(self.dropout) .softmax_classifier(env.action_space.n)) self.returns = tf.placeholder(DTYPE, shape=[None, env.action_space.n], name="returns") loss = - tf.reduce_sum(tf.mul(self.action, tf.div(self.policy_network, self.prev_policy)), 1) * self.advant self.train = tf.train.AdamOptimizer().minimize(loss) features_count = 2 * env.observation_space.shape[0] + env.action_space.n + 2 self.value_function = ValueFunction(self.session, features_count, learning_rate=1e-3, epochs=50, dropout=0.5) self.session.run(tf.initialize_all_variables())
def create_net(self, shape): with tf.variable_scope("vf") as scope: self.x = tf.placeholder(tf.float32, shape=[None, shape], name="x") self.y = tf.placeholder(tf.float32, shape=[None], name="y") self.net = (pt.wrap(self.x). fully_connected(64, activation_fn=tf.nn.relu). fully_connected(64, activation_fn=tf.nn.relu). fully_connected(1)) self.net = tf.reshape(self.net, (-1, )) l2 = (self.net - self.y) * (self.net - self.y) self.train = tf.train.AdamOptimizer().minimize(l2) self.session.run(tf.initialize_all_variables())
def lenet5(images, labels): images = pt.wrap(images) with pt.defaults_scope(activation_fn=tf.nn.relu, l2loss=0.00001): return (images .reshape([-1, 28, 28, 1]) .conv2d(5, 20) .max_pool(2, 2) .conv2d(5, 50) .max_pool(2, 2) .flatten() .fully_connected(500) .softmax_classifier(10, labels))
def _recognition_network_conv(self, input_tensor): hidden_tensor = (pt.wrap(input_tensor). reshape([self.batch_size, 28, 28, 1]). conv2d(5, 32, stride=2). conv2d(5, 64, stride=2). conv2d(5, 128, edges='VALID'). dropout(0.9). flatten(). fully_connected(self.params['n_z'] * 2, activation_fn=None)).tensor z_mean = hidden_tensor[:, :self.params['n_z']] z_log_sigma_sq = tf.sqrt(tf.exp(hidden_tensor[:, self.params['n_z']:])) return (hidden_tensor, z_mean, z_log_sigma_sq)
def generator(): '''Create a network that generates images TODO: Add fixed initialization, so we can draw interpolated images Returns: A deconvolutional (not true deconv, transposed conv2d) network that generated images. ''' input_tensor = tf.random_uniform([FLAGS.batch_size, 1, 1, 100], -1.0, 1.0) return (pt.wrap(input_tensor). deconv2d(3, 128, edges='VALID'). deconv2d(5, 64, edges='VALID'). deconv2d(5, 32, stride=2). deconv2d(5, 1, stride=2, activation_fn=tf.nn.sigmoid)).tensor
def construct_policy_net(obs, action_dim): with tf.variable_scope('policy_net') as scope: mean = ( pt.wrap(obs).fully_connected(action_dim, activation_fn=None, stddev=0.01)) # fully_connected(64, activation_fn=tf.nn.relu, stddev=0.01). # fully_connected(action_dim, activation_fn=None, stddev=0.01)) action_dist_logstd_param = tf.get_variable( 'logstd', [1, action_dim], initializer=tf.random_normal_initializer(0.0, 0.1)) action_dist_logstd = tf.tile( action_dist_logstd_param, tf.pack([obs.get_shape()[0], 1])) std = tf.exp(action_dist_logstd) return tf.concat(1, [mean, std])
def guts(self): conv_layers = (pt.wrap(self.input_data).conv2d( 4, 32, stride=2, name="enc_conv1").conv2d(4, 64, stride=2, name="enc_conv2").conv2d( 4, 128, stride=2, name="enc_conv3").conv2d(4, 256, stride=2, name="enc_conv4").flatten()) mu = conv_layers.fully_connected(self.representation_size, activation_fn=None, name="mu") stddev_log_sq = conv_layers.fully_connected(self.representation_size, activation_fn=None, name="stddev_log_sq") return mu, stddev_log_sq
def setUp(self): self.prng = numpy.random.RandomState(42) tf.ops.reset_default_graph() self.input = tf.placeholder(tf.float32, [4, 2]) self.target = tf.placeholder(tf.float32) xor_inputs = numpy.array([[0., 0.], [1., 0.], [1., 1.], [0., 1.]]) xor_outputs = numpy.array([[0., 1.], [1., 0.], [0., 1.], [1., 0.]]) self.xor_data = itertools.cycle( input_helpers.feed_numpy(4, xor_inputs, xor_outputs)) self.softmax_result = (pt.wrap(self.input).fully_connected( 2, activation_fn=tf.sigmoid, init=self.random_numpy).fully_connected( 2, activation_fn=None, init=self.random_numpy).softmax(self.target)) self.tmp_file = tempfile.mkdtemp()
def predictor(input_tensor=None): '''Create prediction network ''' epsilon = tf.random_normal( [FLAGS.sample_size, FLAGS.batch_size, FLAGS.z_size]) if input_tensor is None: mean = None stddev = None input_sample = epsilon else: mean = input_tensor[:, :FLAGS.z_size] stddev = tf.sqrt(tf.exp(input_tensor[:, FLAGS.z_size:])) input_sample = mean + epsilon * stddev input_sample = tf.reduce_mean(input_sample, axis=0) return (pt.wrap(input_sample).fully_connected( FLAGS.output_size, activation_fn=None).softmax_activation()).tensor, mean, stddev, epsilon
def encoder(input_tensor): '''Create encoder network. Args: input_tensor: a batch of flattened images [batch_size, 28*28] Returns: A tensor that expresses the encoder network ''' return (pt.wrap(input_tensor).reshape([ FLAGS.batch_size, 28, 28, 1 ]).flatten().fully_connected(FLAGS.hidden_size * 2, activation_fn=tf.nn.relu).fully_connected( FLAGS.hidden_size * 2, activation_fn=tf.nn.relu).fully_connected( FLAGS.z_size * 2, activation_fn=None)).tensor
def encoder(input_tensor): '''Create encoder network. Args: input_tensor: a batch of flattened images [batch_size, 28*28] Returns: A tensor that expresses the encoder network ''' return (pt.wrap(input_tensor). reshape([FLAGS.batch_size, 28, 28, 1]). conv2d(5, 32, stride=2). conv2d(5, 64, stride=2). conv2d(5, 128, edges='VALID'). dropout(0.9). flatten(). fully_connected(FLAGS.hidden_size * 2, activation_fn=None)).tensor
def mapping(self, x): """ lambda = phi(x) """ with pt.defaults_scope(activation_fn=tf.nn.elu, batch_normalize=True, learned_moments_update_rate=0.0003, variance_epsilon=0.001, scale_after_normalization=True): params = (pt.wrap(x).reshape([FLAGS.n_data, 28, 28, 1]).conv2d( 5, 32, stride=2).conv2d(5, 64, stride=2).conv2d( 5, 128, edges='VALID').dropout(0.9).flatten().fully_connected( self.num_vars * 2, activation_fn=None)).tensor mean = params[:, :self.num_vars] stddev = tf.sqrt(tf.exp(params[:, self.num_vars:])) return [mean, stddev]
def gener_model_dyn_full(hidden_activations): '''The input to this network (hidden_activations) is a set of sampled activations that represents hidden activations across a batch. They are correlated by means of the structure imposed on the noise that they experience when they are sampled together, but here they should be shaped in a batch-friendly setup, that is once again a 2d tensor: [batch,everything]. We will first connect to a fully connected layer in order to generate input that is correctly shaped for deconvolution that mirrors the recognition model. ''' return (pt.wrap(hidden_activations).fully_connected( 1 * 1 * 512, activation_fn=None, weights=tf.random_uniform_initializer(0.1)).reshape( [None, 1, 1, 512]).deconv2d(5, 256, stride=2).deconv2d( 5, 128, stride=2).deconv2d(5, 64, stride=2).deconv2d( 5, 32, stride=2).deconv2d(5, 16, stride=2).deconv2d( 5, 3, stride=2, activation_fn=tf.nn.sigmoid)).tensor # 32x32x4
def inference_network(x): """Inference network to parameterize variational model. It takes data as input and outputs the variational parameters. loc, scale = neural_network(x) """ with pt.defaults_scope(activation_fn=tf.nn.elu, batch_normalize=True, scale_after_normalization=True): params = (pt.wrap(x).reshape([M, 28, 28, 1]).conv2d( 5, 32, stride=2).conv2d(5, 64, stride=2).conv2d( 5, 128, edges='VALID').dropout(0.9).flatten().fully_connected( d * 2, activation_fn=None)).tensor loc = params[:, :d] scale = tf.nn.softplus(params[:, d:]) return loc, scale
def __init__(self, scope): with tf.variable_scope("%s_shared" % scope): self.obs = obs = tf.placeholder(dtype, shape=[None, pms.obs_shape], name="%s_obs" % scope) self.action_n = tf.placeholder(dtype, shape=[None, pms.action_shape], name="%s_action" % scope) self.advant = tf.placeholder(dtype, shape=[None], name="%s_advant" % scope) self.old_dist_means_n = tf.placeholder( dtype, shape=[None, pms.action_shape], name="%s_oldaction_dist_means" % scope) self.old_dist_logstds_n = tf.placeholder( dtype, shape=[None, pms.action_shape], name="%s_oldaction_dist_logstds" % scope) self.action_dist_means_n = (pt.wrap(self.obs).fully_connected( 64, activation_fn=tf.nn.relu, init=tf.random_normal_initializer(-0.05, 0.05), name="%s_fc1" % scope).fully_connected( 64, activation_fn=tf.nn.relu, init=tf.random_normal_initializer(-0.05, 0.05), name="%s_fc2" % scope).fully_connected( pms.action_shape, init=tf.random_normal_initializer(-0.05, 0.05), name="%s_fc3" % scope)) self.N = tf.shape(obs)[0] Nf = tf.cast(self.N, dtype) self.action_dist_logstd_param = tf.Variable( (.01 * np.random.randn(1, pms.action_shape)).astype( np.float32), trainable=False, name="%spolicy_logstd" % scope) self.action_dist_logstds_n = tf.tile( self.action_dist_logstd_param, tf.pack((tf.shape(self.action_dist_means_n)[0], 1))) self.var_list = [ v for v in tf.trainable_variables() if v.name.startswith(scope) ]
def multilayer_fully_connected(images, labels): """Creates a multi layer network of fully_connected layers. Each layer is 100 neurons. Please change this to experiment with architectures. Args: images: The input images. labels: The labels as dense one-hot vectors. Returns: A softmax result. """ # Pretty Tensor is a thin wrapper on Tensors. # Change this method to experiment with other architectures images = pt.wrap(images) with pt.defaults_scope(activation_fn=tf.nn.relu, l2loss=0.00001): return (images.flatten().fully_connected(100).fully_connected( 100).softmax_classifier(10, labels))
def discriminator(input_tensor): '''Create discriminator network. Args: input_tensor: a batch of flattened images [batch_size, 28*28] Returns: A tensor that expresses the logit of being a true sample ''' return (pt.wrap(input_tensor). reshape([FLAGS.batch_size, 28, 28, 1]). conv2d(5, 32, stride=2). conv2d(5, 64, stride=2). conv2d(5, 128, edges='VALID'). dropout(0.9). flatten(). fully_connected(1, activation_fn=None)).tensor
def inference_network(x): """Inference network to parameterize variational family. It takes data as input and outputs the variational parameters. mu, sigma = neural_network(x) """ n_vars = 10 with pt.defaults_scope(activation_fn=tf.nn.elu, batch_normalize=True, scale_after_normalization=True): params = (pt.wrap(x).reshape([N_MINIBATCH, 28, 28, 1]).conv2d( 5, 32, stride=2).conv2d(5, 64, stride=2).conv2d( 5, 128, edges='VALID').dropout(0.9).flatten().fully_connected( n_vars * 2, activation_fn=None)).tensor mu = tf.reshape(params[:, :n_vars], [-1]) sigma = tf.reshape(tf.nn.softplus(params[:, n_vars:]), [-1]) return mu, sigma
def generator(input_tensor): '''Create a network that generates images TODO: Add fixed initialization, so we can draw interpolated images Returns: A deconvolutional (not true deconv, transposed conv2d) network that generated images. ''' epsilon = tf.random_normal([FLAGS.batch_size, FLAGS.hidden_size]) mean = input_tensor[:, :FLAGS.hidden_size] stddev = tf.sqrt(tf.exp(input_tensor[:, FLAGS.hidden_size:])) input_sample = mean + epsilon * stddev input_sample = tf.reshape(input_sample, [FLAGS.batch_size, 1, 1, -1]) return (pt.wrap(input_sample). deconv2d(3, 128, edges='VALID'). deconv2d(5, 64, edges='VALID'). deconv2d(5, 32, stride=2). deconv2d(5, 1, stride=2, activation_fn=tf.nn.sigmoid)).tensor
def main_network(images, training): # Wrap the input images as a Pretty Tensor object. x_pretty = pt.wrap(images) # Pretty Tensor uses special numbers to distinguish between # the training and testing phases. if training: phase = pt.Phase.train else: phase = pt.Phase.infer # Create the convolutional neural network using Pretty Tensor. # It is very similar to the previous tutorials, except # the use of so-called batch-normalization in the first layer. with pt.defaults_scope(activation_fn=tf.nn.relu, phase=phase): y_pred, loss = x_pretty. conv2d(kernel=3, depth=64, name='layer_conv1', batch_normalize=True). max_pool(kernel=2, stride=2). conv2d(kernel=3, depth=128, name='layer_conv2', batch_normalize=True). max_pool(kernel=2, stride=2). conv2d(kernel=3, depth=256, name='layer_conv3', batch_normalize=True). max_pool(kernel=2, stride=2). flatten(). fully_connected(size=256, name='layer_fc1'). fully_connected(size=128, name='layer_fc2'). softmax_classifier(num_classes=num_classes, labels=y_true) return y_pred, loss
def encoder(self, inputs, latent_size, activ=tf.nn.elu, phase=pt.Phase.train): with pt.defaults_scope(activation_fn=activ, batch_normalize=True, learned_moments_update_rate=0.0003, variance_epsilon=0.001, scale_after_normalization=True, phase=phase): params = (pt.wrap(inputs). reshape([-1, self.input_shape[0], self.input_shape[1], 1]). conv3d(5, 8, stride=2). conv3d(5, 8, stride=2). conv3d(5, 8, edges='VALID'). flatten(). fully_connected(self.latent_size * 2, activation_fn=None)).tensor mean = params[:, :self.latent_size] stddev = params[:, self.latent_size:] return [mean, stddev]
def main_network(images, training): x_pretty = pt.wrap(images) if training: phase = pt.Phase.train else: phase = pt.Phase.infer with pt.defaults_scope(activation_fn=tf.nn.relu, phase=phase): y_pred,loss=x_pretty.\ conv2d(kernel=5,depth=16,name='layer_conv1').\ max_pool(kernel=2,stride=2).\ conv2d(kernel=5,depth=36,name='layer_conv2').\ max_pool(kernel=2,stride=2).\ flatten().\ fully_connected(size=128,name='layer_fc1').\ softmax_classifier(num_classes=10,labels=y_true) return y_pred, loss
def test_predictor(input_tensor): '''Create prediction network for both public and private labels ''' epsilon = tf.random_normal([FLAGS.sample_size, FLAGS.batch_size, FLAGS.z_size]) if input_tensor is None: mean = None stddev = None input_sample = epsilon else: mean = input_tensor[:, :FLAGS.z_size] stddev = tf.clip_by_value(tf.sqrt(tf.exp(input_tensor[:, FLAGS.z_size:])), -FLAGS.max_stddev, FLAGS.max_stddev) input_sample = mean + epsilon * stddev input_sample = tf.reduce_mean(input_sample, axis=0) return (pt.wrap(input_tensor). fully_connected(FLAGS.hidden_size * 2, activation_fn=tf.nn.relu). fully_connected(FLAGS.hidden_size, activation_fn=tf.nn.relu). fully_connected(FLAGS.output_size + FLAGS.private_size, activation_fn=None)).tensor
def decoder(input_tensor=None): '''Create decoder network. If input tensor is provided then decodes it, otherwise samples from a sampled vector. Args: input_tensor: a batch of vectors to decode Returns: A tensor that expresses the decoder network ''' epsilon = tf.random_normal([FLAGS.batch_size, FLAGS.hidden_size]) if input_tensor is None: print('Empty tensor') mean = None stddev = None input_sample = epsilon else: mean = input_tensor[:, :FLAGS.hidden_size] stddev = tf.sqrt(tf.exp(input_tensor[:, FLAGS.hidden_size:])) input_sample = mean + epsilon * stddev #a = pt.wrap(input_sample).reshape([FLAGS.batch_size, 1, 1, FLAGS.hidden_size]) #b = a.deconv2d(4, 256, edges='VALID') #c = b.deconv2d(5, 128, edges='VALID') #d = c.deconv2d(5, 64, stride=2) #e = d.deconv2d(5, 32, stride=2) #f = e.deconv2d(5, 1, stride=2, activation_fn=tf.nn.sigmoid).flatten() #print (a.get_shape()) #print (b.get_shape()) #print (c.get_shape()) #print (d.get_shape()) #print (e.get_shape()) #print (f.get_shape()) return (pt.wrap(input_sample).reshape([ FLAGS.batch_size, 1, 1, FLAGS.hidden_size ]).deconv2d(4, 256, edges='VALID').deconv2d( 5, 128, edges='VALID').deconv2d(5, 64, stride=2).deconv2d( 5, 32, stride=2).deconv2d( 5, 1, stride=2, activation_fn=tf.nn.sigmoid).flatten()).tensor, mean, stddev
def encoder(X): '''Create encoder network. Args: x: a batch of flattened images [batch_size, 28*28] Returns: A tensor that expresses the encoder network # The transformation is parametrized and can be learned. # returns network output, mean, setd ''' lay_end = ( pt.wrap(X).reshape([batch_size, dim1, dim2, dim3]).conv2d(4, 64, stride=2).conv2d(4, 128, stride=2). # conv2d(5, 256, stride=2). flatten()) z_mean = lay_end.fully_connected(hidden_size, activation_fn=None) z_log_sigma_sq = lay_end.fully_connected(hidden_size, activation_fn=None) return z_mean, z_log_sigma_sq
def get_softmax_layer(transfer_values, dataset): tf.reset_default_graph() # Placeholder Variables transfer_len = model.transfer_len x = tf.placeholder(tf.float32, shape=[None, transfer_len], name='x') y_true = tf.placeholder(tf.float32, shape=[None, dataset.num_classes], name='y_true') y_true_cls = tf.argmax(y_true, dimension=1) # Neural Network x_pretty = pt.wrap( x) # Wrap the transfer-values as a Pretty Tensor object. with pt.defaults_scope(activation_fn=tf.nn.relu): y_pred, loss = x_pretty.fully_connected( size=1024, name='layer_fc1').softmax_classifier( num_classes=dataset.num_classes, labels=y_true) # Optimization Method global_step = tf.Variable(initial_value=0, name='global_step', trainable=False) optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize( loss, global_step) # Classification Accuracy y_pred_cls = tf.argmax(y_pred, dimension=1) # correct_prediction = tf.equal(y_pred_cls, y_true_cls) correct_prediction = tf.diag_part(tf.matmul(y_pred, tf.transpose(y_true))) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) prob_list = None with tf.Session() as session: saver = tf.train.Saver() saver.restore(sess=session, save_path=(dataset.output_path + dataset.data_name)) prob_list = y_pred.eval({x: transfer_values}, session) session.close() return prob_list
def __init__(self): self.batch_size = 1 input_data = tf.placeholder(tf.float32, [self.batch_size, 84, 84, 1]) targets = tf.placeholder(tf.float32, [self.batch_size, 1]) self.lr_start = 0.01 self.lr = self.lr_start self.lr_end = self.lr self.lr_endt = 1000000 seq = pt.wrap(input_data).sequential() #reshape here seq.conv2d([8, 8], 32, stride=8) seq.conv2d([4, 4], 64, stride=4) seq.conv2d([3, 3], 64, stride=1) seq.flatten() seq.fully_connected(512, activation_fn=tf.nn.relu) seq.softmax_classifier(1, targets)
def discriminator(D_I): ''' A encodes Create a network that discriminates between images from a dataset and generated ones. Args: input: a batch of real images [batch, height, width, channels] Returns: A tensor that represents the network ''' descrim_conv = ( pt.wrap(D_I). # This is what we're descriminating reshape([batch_size, dim1, dim2, dim3]).conv2d(5, 32, stride=1).conv2d( 5, 128, stride=2).conv2d(5, 256, stride=2).conv2d(5, 256, stride=2).flatten()) lth_layer = descrim_conv.fully_connected( 1024, activation_fn=tf.nn.elu) # this is the lth layer D = lth_layer.fully_connected( 1, activation_fn=tf.nn.sigmoid) # this is the actual discrimination return D, lth_layer
def generator_simple(self, z_var): output_tensor =\ (pt.wrap(z_var). flatten(). custom_fully_connected(self.s16 * self.s16 * self.gf_dim * 8). reshape([-1, self.s16, self.s16, self.gf_dim * 8]). conv_batch_norm(). apply(tf.nn.relu). custom_deconv2d([0, self.s8, self.s8, self.gf_dim * 4], k_h=4, k_w=4). conv_batch_norm(). apply(tf.nn.relu). custom_deconv2d([0, self.s4, self.s4, self.gf_dim * 2], k_h=4, k_w=4). conv_batch_norm(). apply(tf.nn.relu). custom_deconv2d([0, self.s2, self.s2, self.gf_dim], k_h=4, k_w=4). conv_batch_norm(). apply(tf.nn.relu). custom_deconv2d([0] + list(self.image_shape), k_h=4, k_w=4). apply(tf.nn.tanh)) return output_tensor
def construct_model(self): # All will be 4 layers, with elu, and a tanh at the end. with tf.variable_scope("Actor_model") as scope: print("SCOPE: {}".format(scope)) h1_size = int((2 * self.state_dim / 3.0) + self.action_dim / 3.0) h2_size = int((self.state_dim / 3.0) + 2 * self.action_dim / 3.0) print('sizes descending:\t\t{}\t{}\t{}\t{}'.format( self.state_dim, h1_size, h2_size, self.action_dim)) states_in = tf.placeholder(tf.float32, [None, self.state_dim]) out = (pt.wrap(states_in).fully_connected( h1_size, activation_fn=tf.nn.elu).fully_connected( h2_size, activation_fn=tf.nn.elu).fully_connected( self.action_dim, activation_fn=self.final_activation)) out = out * self.action_bound self.in_batch = states_in self.out = out self.lr_var = tf.placeholder(tf.float32, []) all_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) self.network_parameters = tf.get_collection( tf.GraphKeys.GLOBAL_VARIABLES, scope=r".*Actor_model.*")
def model2(x, y): x_img = tf.reshape(x, [-1, img_size, img_size, img_channel]) y_cls = tf.argmax(y, axis=1) x_pretty = pt.wrap(x_img) with pt.defaults_scope(activation_fn=tf.nn.relu): y_pred, loss = x_pretty.\ conv2d(kernel=5, depth=16, name='layer_conv1').\ max_pool(kernel=2, stride=2).\ conv2d(kernel=5, depth=36, name='layer_conv2').\ max_pool(kernel=2, stride=2).\ flatten().\ fully_connected(size=128, name='fc1').\ softmax_classifier(num_class=10, labels=y) optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(loss) y_pred_cls = tf.argmax(y_pred, axis=1) correct_pred = tf.equal(y_pred_cls, y_cls) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) return accuracy, optimizer
def output(self, inputs, phase=pt.Phase.train): # print("input_size:", inputs.shape) input_size = inputs.shape[0] input_dim = inputs.shape[1] # inputs = np.reshape(inputs, [input_size, input_dim, 1, 1]) inputs = tf.reshape(inputs, [input_size, input_dim, 1, 1]) inputs = pt.wrap(inputs) with pt.defaults_scope(phase=phase, activation_fn=tf.nn.relu, l2loss=0.00001): inputs = inputs.conv2d(5, 20).max_pool(2, 2).conv2d(5, 50).max_pool( 2, 2).flatten() # print(self.hidden_layers) for layer in self.hidden_layers: inputs = inputs.fully_connected(layer) # A Pretty Tensor handle to the layer. outputs = inputs.fully_connected(self.dim_output, activation_fn=None) # print("output: ", outputs.shape) return outputs
def generator(Z=None): '''Create a generator network ''' # tf.set_random_seed(round(time())) if Z == None: Z = tf.random_uniform([FLAGS.batch_size, FLAGS.hidden_size]) return (pt.wrap(Z). reshape([FLAGS.batch_size, 1, 1, FLAGS.hidden_size]). deconv2d(4, 128, stride=1,edges='VALID'). # 4*4 batch_normalize(). relu(). #tf.nn.deconv2d(4,64,stride=2 deconv2d(5, 64, stride=2,edges='VALID'). #11*11 batch_normalize(). relu(). deconv2d(4, 32, stride=2,edges='VALID'). #24*24 #deconv2d(4, 64, stride=3). deconv2d(5, 1, stride=1,edges='VALID',activation_fn=tf.nn.sigmoid). # 16*16 flatten()).tensor
def prettyNetwork(images, training): # Wrap the input images as a Pretty Tensor object x_pretty = pt.wrap(images) #special number by Pretty Tensor if training: phase = pt.Phase.train else: phase = pt.Phase.infer with pt.defaults_scope(activation_fn=tf.nn.relu, phase=phase): y_pred, loss = x_pretty.\ conv2d(kernel=5, depth=64, name='layer_conv1', batch_normalize=True).\ max_pool(kernel=2, stride=2).\ conv2d(kernel=5, depth=64, name='layer_conv2').\ max_pool(kernel=2, stride=2).\ flatten().\ fully_connected(size=256, name='layer_fc1').\ fully_connected(size=128, name='layer_fc2').\ softmax_classifier(num_classes=n_classes, labels=y) return y_pred, loss
def mapping(self, x): """ Inference network to parameterize variational family. It takes data x as input and outputs the variational parameters lambda. lambda = phi(x) """ with pt.defaults_scope(activation_fn=tf.nn.elu, batch_normalize=True, learned_moments_update_rate=0.0003, variance_epsilon=0.001, scale_after_normalization=True): params = (pt.wrap(x).reshape([FLAGS.n_data, 28, 28, 1]).conv2d( 5, 32, stride=2).conv2d(5, 64, stride=2).conv2d( 5, 128, edges='VALID').dropout(0.9).flatten().fully_connected( self.num_local_vars * 2, activation_fn=None)).tensor # Return list of vectors where mean[i], stddev[i] are the # parameters of the local variational factor for data point i. mean = tf.reshape(params[:, :self.num_local_vars], [-1]) stddev = tf.reshape(tf.sqrt(tf.exp(params[:, self.num_local_vars:])), [-1]) return [mean, stddev]
def neural_network(x): """ Inference network to parameterize variational family. It takes data as input and outputs the variational parameters. loc, scale = neural_network(x) """ n_vars = 10 with pt.defaults_scope(activation_fn=tf.nn.elu, batch_normalize=True, learned_moments_update_rate=0.0003, variance_epsilon=0.001, scale_after_normalization=True): params = (pt.wrap(x).reshape([N_MINIBATCH, 28, 28, 1]).conv2d( 5, 32, stride=2).conv2d(5, 64, stride=2).conv2d( 5, 128, edges='VALID').dropout(0.9).flatten().fully_connected( n_vars * 2, activation_fn=None)).tensor # Return list of vectors where mean[i], stddev[i] are the # parameters of the local variational factor for data point i. loc = tf.reshape(params[:, :n_vars], [-1]) scale = tf.reshape(tf.sqrt(tf.exp(params[:, n_vars:])), [-1]) return [loc, scale]