def __init__(self, name, n_x, n_y, N_h1, NF_h, N_h2, St1, St2, sz_f, sz_im): ''' Initialisation INPUTS: name - name to assign to the decoder n_x - channels of the input n_y - channels of output N_h - array of number of channels in the hidden units [Nhx,Nh1,Nh2,...,Nhn] St - array of strides to use every operation (must be one longer then the above) sz_f - filters sizes in the format [H,W] ''' self.n_x = n_x self.n_y = n_y self.N_h1 = N_h1 self.N_h2 = N_h2 self.NF_h = NF_h self.Sz1 = NN_utils.compute_size(sz_im, St1) self.Sz2 = NN_utils.compute_size(self.Sz1[-1], St2) self.St = np.concatenate((St1, np.ones(np.shape(NF_h)[0]), St2), 0) self.sz_f = sz_f self.sz_im = sz_im self.name = name self.bias_start = 0.0 network_weights = self._create_weights() self.weights = network_weights # Choice of non-linearity (tf.nn.relu/tf.nn.leaky_relu/tf.nn.elu) self.nonlinearity = tf.nn.leaky_relu
def _create_weights(self): ''' Initialise weights. each of the functions you can import from "networks" in the compute function above has a "make_weights" counterpart you need to call in here ''' # first, we initialise an empty ordered dictionary all_weights = collections.OrderedDict() # we can make the weights for the convolutional network taking x: all_weights = networks.conv_2D_make_weights(all_weights, self.n_ch_x, self.N_x, self.fs_x, ID=0) # now we make the weights for the fully connected network taking z: all_weights = networks.fc_make_weights(all_weights, self.n_z, self.N_z, ID=1) # to initialise the weights for the last fully connected networ, we need to know its input size (size of hidden_post_z + size of hidden_post_x). # there is a function in "NN_utils" to compute the size of the images at each conv layer we can use: im_sizes = NN_utils.compute_size( self.x_siz, self.st_x ) # this computes a length(st_x) x 2 array where each row is the dimensions of the images at each hhidden layer siz_hidden_post_x = im_sizes[-1, 0] * im_sizes[-1, 1] * self.N_x[ -1] # the size of hidden_post_x is the last layer's height*width*n_channels dim_input = siz_hidden_post_x + self.N_z[ -1] # the size of the input to the last layer is then the sum of the above and the dimensionality of the last fully connected layer that took z as input # now we can make the weights for the fully connected network taking the concatenated layers: all_weights = networks.fc_make_weights(all_weights, dim_input, self.N_comb, ID=2) # lastly, we initialise the weights for the two single matrices to get mu and log_sigma_square from the last layer all_weights = networks.fc_make_weights(all_weights, self.N_comb[-1], [self.n_y], add_b=False, ID=3) all_weights = networks.fc_make_weights(all_weights, self.N_comb[-1], [self.n_y], add_b=False, ID=4) return all_weights