def create_model():
    input_layer = Input(team_size * 2)
    embedding = Embedding(100, 1)(input_layer)
    embedding = Flatten()(embedding)
    team_a_embedding = embedding[:, :team_size]
    team_b_embedding = embedding[:, team_size:]

    dense = Dense(5, kernel_constraint=non_neg())
    dense2 = Dense(5, kernel_constraint=non_neg())
    densefinal = Dense(1, kernel_constraint=non_neg())

    def multidense(x):
        return densefinal(dense2(dense(x)))

    # multidense =  Lambda(lambda x: tf.math.reduce_sum(x,axis = 1, keepdims = True)/3)

    team_a_elo = multidense(team_a_embedding)
    team_b_elo = multidense(team_b_embedding)
    elos = Concatenate(axis=1)([team_a_elo, team_b_elo])

    def loglike(win, elos):
        win = tf.cast(win, tf.float32)
        teamAelo = elos[:, 0]
        teamBelo = elos[:, 1]
        win = tf.reshape(win, [-1])
        teamAlikelihood = 1 - 1 / (1 + math.e ** ((teamAelo - teamBelo) / scale))
        teamBlikelihood = 1 / (1 + math.e ** ((teamAelo - teamBelo) / scale))
        loglike = win * tf.math.log(teamAlikelihood) + (1 - win) * tf.math.log(teamBlikelihood)
        # import pdb; pdb.set_trace()
        return - tf.math.reduce_sum(loglike)

    opt = Adam(0.1)
    model = Model(input_layer, elos)
    model.compile(optimizer=opt, loss=loglike)
    return model
예제 #2
0
    def build(self, input_shape):
        """
        This method must be defined for any custom layer, here you define the training parameters.
        
        input_shape: a tensor that automatically captures the dimensions of the input by tensorflow. 
        """
        # retreive the number of waveguides
        self.num_wg = input_shape[-1]

        # define a list of trainable tensorflow parameters representing the coupling coefficients
        coupling_coeff = []
        for idx_wg in range(self.num_wg):
            coupling_coeff.append(
                self.add_weight(name="e%s" % idx_wg,
                                shape=tf.TensorShape(()),
                                initializer=initializers.Constant(1.0),
                                trainable=True,
                                constraint=constraints.non_neg()))

        # convert the list of tensors to one tensor
        coupling_coeff = tf.convert_to_tensor(coupling_coeff)

        # add an extra dimension to represent time
        coupling_coeff = tf.expand_dims(coupling_coeff, 0)

        # add another dimension to represent batch
        coupling_coeff = tf.expand_dims(coupling_coeff, 0)

        # store this tensor as a class member so we can access it from othe methods in the class
        self.coupling_coeff = coupling_coeff

        # this has to be called for any tensorflow custom layer
        super(Coupling_Layer, self).build(input_shape)
예제 #3
0
def create_output_layers(x, x_row_splits, n_ccoords=2,
                           add_beta=None, add_beta_weight=0.2, use_e_proxy=False,
                           scale_exp_e=True, n_classes=0):
    beta = None
    if add_beta is not None:

        # the exact weighting can be learnt, but there has to be a positive correlation
        from tensorflow.keras.constraints import non_neg

        assert add_beta_weight < 1
        n_adds = float(len(add_beta))
        if isinstance(add_beta, list):
            add_beta = Concatenate()(add_beta)
            add_beta = ScalarMultiply(1. / n_adds)(add_beta)
        add_beta = Dense(1, activation='sigmoid', name="predicted_add_beta",
                         kernel_constraint=non_neg(),  # maybe it figures it out...?
                         kernel_initializer='ones'
                         )(add_beta)

        # tf.print(add_beta)

        add_beta = ScalarMultiply(add_beta_weight)(add_beta)

        beta = Dense(1, activation='sigmoid', name="pre_predicted_beta")(x)
        beta = ScalarMultiply(1 - add_beta_weight)(beta)
        beta = Add(name="predicted_beta")([beta, add_beta])

    else:
        beta = Dense(1, activation='sigmoid', name="predicted_beta")(x)

    # x_raw = BatchNormalization(momentum=0.6,name="pre_ccoords_bn")(raw_inputs)
    # pre_ccoords = Dense(64, activation='elu',name="pre_ccords")(Concatenate()([x,x_raw]))
    ccoords = Dense(2, activation=None, name="predicted_ccoords")(x)
    if n_ccoords > 2:
        ccoords = Concatenate()([ccoords, Dense(n_ccoords - 2, activation=None, name="predicted_ccoords_add")(x)])

    xy = Dense(2, activation=None, name="predicted_positions", kernel_initializer='zeros')(x)
    t = Dense(1, activation=None, name="predicted_time", kernel_initializer='zeros')(x)
    t = ScalarMultiply(1e-9)(t)
    xyt = Concatenate()([xy, t])

    energy = Dense(1, activation=None)(x)
    if scale_exp_e:
        energy = ExpMinusOne(name='predicted_energy')(energy)
    else:
        energy = ScalarMultiply(100.)(energy)

    if n_classes > 0:
        classes_scores = Dense(n_classes, activation=None, name="predicted_classification_scores")(x)
       
        return Concatenate(name="predicted_final")([beta, energy, xyt, ccoords, classes_scores])
    else:
        return Concatenate(name="predicted_final")([beta, energy, xyt, ccoords])
예제 #4
0
    def build(self, input_shape):
        """
        This method must be defined for any custom layer, here you define the training parameters.
        
        input_shape: a tensor that automatically captures the dimensions of the input by tensorflow. 
        """
        # get the the number of paramters
        num_params = input_shape[-1].value

        # calcualte the number of waveguides p = N*(N+1)/2
        self.num_wg = int(np.sqrt(2 * num_params + 0.25) - 0.5)

        self.beta0 = self.add_weight(name="beta0",
                                     shape=tf.TensorShape((num_params, )),
                                     initializer=initializers.Constant(100.0),
                                     trainable=True,
                                     constraint=constraints.non_neg())

        # this has to be called for any tensorflow custom layer
        super(Param_to_Ham_Layer, self).build(input_shape)
예제 #5
0
    def retain(ARGS):
        """Create the model"""

        # Define the constant for model saving
        reshape_size = ARGS.emb_size + ARGS.numeric_size
        if ARGS.allow_negative:
            embeddings_constraint = FreezePadding()
            beta_activation = 'tanh'
            output_constraint = None
        else:
            embeddings_constraint = FreezePadding_Non_Negative()
            beta_activation = 'sigmoid'
            output_constraint = non_neg()

        def reshape(data):
            """Reshape the context vectors to 3D vector"""
            return K.reshape(x=data, shape=(K.shape(data)[0], 1, reshape_size))

        # Code Input
        codes = L.Input((None, None), name='codes_input')
        inputs_list = [codes]
        # Calculate embedding for each code and sum them to a visit level
        codes_embs_total = L.Embedding(
            ARGS.num_codes + 1,
            ARGS.emb_size,
            name='embedding'
            # BUG: embeddings_constraint not supported
            # https://github.com/tensorflow/tensorflow/issues/33755
            # ,embeddings_constraint=embeddings_constraint
        )(codes)
        codes_embs = L.Lambda(lambda x: K.sum(x, axis=2))(codes_embs_total)
        # Numeric input if needed
        if ARGS.numeric_size > 0:
            numerics = L.Input((None, ARGS.numeric_size), name='numeric_input')
            inputs_list.append(numerics)
            full_embs = L.concatenate([codes_embs, numerics], name='catInp')
        else:
            full_embs = codes_embs

        # Apply dropout on inputs
        full_embs = L.Dropout(ARGS.dropout_input)(full_embs)

        # Time input if needed
        if ARGS.use_time:
            time = L.Input((None, 1), name='time_input')
            inputs_list.append(time)
            time_embs = L.concatenate([full_embs, time], name='catInp2')
        else:
            time_embs = full_embs

        # Setup Layers
        # This implementation uses Bidirectional LSTM instead of reverse order
        #    (see https://github.com/mp2893/retain/issues/3 for more details)

        # If training on GPU and Tensorflow use CuDNNLSTM for much faster training
        if glist:
            alpha = L.Bidirectional(L.CuDNNLSTM(ARGS.recurrent_size,
                                                return_sequences=True),
                                    name='alpha')
            beta = L.Bidirectional(L.CuDNNLSTM(ARGS.recurrent_size,
                                               return_sequences=True),
                                   name='beta')
        else:
            alpha = L.Bidirectional(L.LSTM(ARGS.recurrent_size,
                                           return_sequences=True,
                                           implementation=2),
                                    name='alpha')
            beta = L.Bidirectional(L.LSTM(ARGS.recurrent_size,
                                          return_sequences=True,
                                          implementation=2),
                                   name='beta')

        alpha_dense = L.Dense(1, kernel_regularizer=l2(ARGS.l2))
        beta_dense = L.Dense(ARGS.emb_size + ARGS.numeric_size,
                             activation=beta_activation,
                             kernel_regularizer=l2(ARGS.l2))

        # Compute alpha, visit attention
        alpha_out = alpha(time_embs)
        alpha_out = L.TimeDistributed(alpha_dense,
                                      name='alpha_dense_0')(alpha_out)
        alpha_out = L.Softmax(axis=1)(alpha_out)
        # Compute beta, codes attention
        beta_out = beta(time_embs)
        beta_out = L.TimeDistributed(beta_dense, name='beta_dense_0')(beta_out)
        # Compute context vector based on attentions and embeddings
        c_t = L.Multiply()([alpha_out, beta_out, full_embs])
        c_t = L.Lambda(lambda x: K.sum(x, axis=1))(c_t)
        # Reshape to 3d vector for consistency between Many to Many and Many to One implementations
        contexts = L.Lambda(reshape)(c_t)

        # Make a prediction
        contexts = L.Dropout(ARGS.dropout_context)(contexts)
        output_layer = L.Dense(1,
                               activation='sigmoid',
                               name='dOut',
                               kernel_regularizer=l2(ARGS.l2),
                               kernel_constraint=output_constraint)

        # TimeDistributed is used for consistency
        # between Many to Many and Many to One implementations
        output = L.TimeDistributed(output_layer,
                                   name='time_distributed_out')(contexts)
        # Define the model with appropriate inputs
        model = Model(inputs=inputs_list, outputs=[output])

        return model
예제 #6
0
 def build(self,input_shape):
     """
     This method must be defined for any custom layer, here you define the training parameters.
     
     input_shape: a tensor that automatically captures the dimensions of the input by tensorflow. 
     """    
     # get the the number of paramters
     num_params = input_shape.as_list()[-1]
     
     # calculate the number of waveguides p = N*(N+1)/2
     self.num_wg = int( np.sqrt(2*num_params + 0.25) - 0.5 )
     
     self.beta0 = self.add_weight(name="beta0", shape=tf.TensorShape((num_params,)), initializer=initializers.Constant(100.0),trainable=True, constraint = constraints.non_neg())
     
     # construct a matrix operator to rearrange the elements to be upper triangular
     r1 = 0
     r2 = 0
     A = np.zeros((self.num_wg**2,num_params))
     for k in range(0,self.num_wg):
         for i in range(0,self.num_wg-k):
             A[i+r2, i+r1] = 1
         r1 = r1 + (self.num_wg-k) 
         r2 = r2 + k+1 + (self.num_wg-k)
     
     self.tri_op = tf.constant(np.reshape(A, (1,1,self.num_wg**2,num_params)), dtype=tf.float32)
     # this has to be called for any tensorflow custom layer
     super(Param_to_Ham_Layer, self).build(input_shape)
예제 #7
0
 def build(self,input_shape):
     """
     This method must be defined for any custom layer, here you define the training parameters.
     
     input_shape: a tensor that automatically captures the dimensions of the input by tensorflow. 
     """        
     # retreive the number of waveguides
     self.num_wg = input_shape.as_list()[-1]
     
     # define the tensorflow parameters representing the coupling coefficients 
     self.coupling_coeff = self.add_weight(name="e", shape=(1,1, self.num_wg), initializer=initializers.Constant(1.0), trainable=True, constraint = constraints.non_neg()) 
     
     # this has to be called for any tensorflow custom layer
     super(Coupling_Layer, self).build(input_shape)
예제 #8
0
    def retain(ARGS):
        """
        Helper function to create DAG of Keras Layers via functional API approach.
        The Keras Layer design is mimicking RETAIN architecture.
        :param ARGS: Arguments object containing user-specified parameters
        :type ARGS: :class:`argparse.Namespace`
        :return: Keras model
        :rtype: :class:`tensorflow.keras.Model`
        """

        #Define the constant for model saving
        reshape_size = ARGS.emb_size + ARGS.numeric_size
        if ARGS.allow_negative:
            embeddings_constraint = FreezePadding()
            beta_activation = 'tanh'
            output_constraint = None
        else:
            embeddings_constraint = FreezePadding_Non_Negative()
            beta_activation = 'sigmoid'
            output_constraint = non_neg()

        def reshape(data):
            """Reshape the context vectors to 3D vector"""
            return K.reshape(x=data, shape=(K.shape(data)[0], 1, reshape_size))

        #Code Input
        codes = L.Input((None, None), name='codes_input')
        inputs_list = [codes]
        #Calculate embedding for each code and sum them to a visit level
        codes_embs_total = L.Embedding(ARGS.num_codes + 1,
                                       ARGS.emb_size,
                                       name='embedding')(codes)
        codes_embs = L.Lambda(lambda x: K.sum(x, axis=2))(codes_embs_total)
        #Numeric input if needed
        if ARGS.numeric_size:
            numerics = L.Input((None, ARGS.numeric_size), name='numeric_input')
            inputs_list.append(numerics)
            full_embs = L.concatenate([codes_embs, numerics], name='catInp')
        else:
            full_embs = codes_embs

        #Apply dropout on inputs
        full_embs = L.Dropout(ARGS.dropout_input)(full_embs)

        #Time input if needed
        if ARGS.use_time:
            time = L.Input((None, 1), name='time_input')
            inputs_list.append(time)
            time_embs = L.concatenate([full_embs, time], name='catInp2')
        else:
            time_embs = full_embs

        #Setup Layers
        #This implementation uses Bidirectional LSTM instead of reverse order
        #    (see https://github.com/mp2893/retain/issues/3 for more details)

        alpha = L.Bidirectional(L.LSTM(ARGS.recurrent_size,
                                       return_sequences=True,
                                       implementation=2),
                                name='alpha')
        beta = L.Bidirectional(L.LSTM(ARGS.recurrent_size,
                                      return_sequences=True,
                                      implementation=2),
                               name='beta')

        alpha_dense = L.Dense(1, kernel_regularizer=l2(ARGS.l2))
        beta_dense = L.Dense(ARGS.emb_size + ARGS.numeric_size,
                             activation=beta_activation,
                             kernel_regularizer=l2(ARGS.l2))

        #Compute alpha, visit attention
        alpha_out = alpha(time_embs)
        alpha_out = L.TimeDistributed(alpha_dense,
                                      name='alpha_dense_0')(alpha_out)
        alpha_out = L.Softmax(name='softmax_1', axis=1)(alpha_out)
        #Compute beta, codes attention
        beta_out = beta(time_embs)
        beta_out = L.TimeDistributed(beta_dense, name='beta_dense_0')(beta_out)
        #Compute context vector based on attentions and embeddings
        c_t = L.Multiply()([alpha_out, beta_out, full_embs])
        c_t = L.Lambda(lambda x: K.sum(x, axis=1))(c_t)
        #Reshape to 3d vector for consistency between Many to Many and Many to One implementations
        contexts = L.Lambda(reshape)(c_t)

        #Make a prediction
        contexts = L.Dropout(ARGS.dropout_context)(contexts)
        output_layer = L.Dense(1,
                               activation='sigmoid',
                               name='dOut',
                               kernel_regularizer=l2(ARGS.l2),
                               kernel_constraint=output_constraint)

        #TimeDistributed is used for consistency
        # between Many to Many and Many to One implementations
        output = L.TimeDistributed(output_layer,
                                   name='time_distributed_out')(contexts)
        #Define the model with appropriate inputs
        model = Model(inputs=inputs_list, outputs=[output])

        return model
예제 #9
0
 def __init__(self, units):
     super(Motor_thal, self).__init__()
     unit1 = units[0]
     unit2 = units[1]
     unit3 = units[2]
     self.units = unit2 * 3
     self.input_dim = (unit2 * 3) + unit3
     self.training = True
     w_init = tf.random_uniform_initializer(minval=0., maxval=0.01)
     z_init = tf.zeros_initializer()
     mthal_bias = [0, 0]
     b_init = tf.random_uniform_initializer(minval=mthal_bias[0],
                                            maxval=mthal_bias[1])
     #split mthal into thirds. The middle layer is a transition layer
     limit_1 = unit2
     limit_2 = unit2 * 2
     limit_3 = unit2 * 3
     #GPi input
     self.gpi_1_len = unit2
     self.gpi_2_len = unit2 * 2
     self.gpi_3_len = unit2 * 3
     #premotor_input
     self.premotor_1_len = (unit2 * 3) + unit3
     self.unit_diags = []
     for unit in range(self.units):
         unit_diag = []
         for input in range(self.input_dim):
             #mthal_1
             if unit in range(limit_1):
                 if input in range(self.gpi_1_len):
                     #100% of self.gpi_1 -> mthal_1
                     unit_diag.append(1)
                 elif input in range(self.gpi_1_len, self.gpi_2_len):
                     #50% self.gpi_2 -> mthal_1
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(self.gpi_2_len, self.gpi_3_len):
                     #0% of self.gpi_3 -> mthal_1
                     unit_diag.append(0)
                 elif input in range(self.gpi_3_len, self.premotor_1_len):
                     #100% self.premotor1 -> mthal_1
                     unit_diag.append(1)
             #mthal_2
             elif unit in range(limit_1, limit_2):
                 if input in range(self.gpi_1_len):
                     #50% self.gpi_2 -> mthal_2
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(self.gpi_1_len, self.gpi_2_len):
                     #75% self.gpi_2 -> mthal_2
                     if random.randint(4) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(self.gpi_2_len, self.gpi_3_len):
                     #50% self.gpi_3 -> mthal_2
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(self.gpi_3_len, self.premotor_1_len):
                     #90% self.premotor1 -> mthal_2
                     if random.randint(10) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
             #mthal_3
             elif unit in range(limit_2, limit_3):
                 if input in range(self.gpi_1_len):
                     #0% self.gpi_1 -> mthal_3
                     unit_diag.append(0)
                 elif input in range(self.gpi_1_len, self.gpi_2_len):
                     #50% self.gpi_2 -> mthal_3
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(self.gpi_2_len, self.gpi_3_len):
                     #100% self.gpi_3 -> mthal_3
                     unit_diag.append(1)
                 elif input in range(self.gpi_3_len, self.premotor_1_len):
                     #80% self.premotor1 -> mthal_3
                     if random.randint(10) > 1:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
         self.unit_diags.append(np.array(unit_diag))
     self.unit_diags = np.array(self.unit_diags)
     if use_constraints:
         self.w = self.add_weight(shape=(self.input_dim, self.units),
                                  initializer=w_init,
                                  trainable=True,
                                  constraint=non_neg())
     else:
         self.w = self.add_weight(shape=(self.input_dim, self.units),
                                  initializer=w_init,
                                  trainable=True)
     #biases
     self.b = self.add_weight(shape=(self.units, ),
                              initializer=b_init,
                              trainable=True)
     self.s = tf.convert_to_tensor(np.array([
         sparse.diags(self.unit_diags[i], 0).toarray()
         for i in range(len(self.unit_diags))
     ]),
                                   dtype=tf.float32)
예제 #10
0
    def __init__(self, units, str_dval=[]):
        super(GPi, self).__init__()
        unit1 = units[0]
        unit2 = units[1]
        unit3 = units[2]
        self.units = unit2 * 3
        self.input_dim = (unit1 + unit2) * 3
        self.training = True
        w_init = tf.random_uniform_initializer(minval=0., maxval=0.01)
        z_init = tf.zeros_initializer()
        gpi_bias = [0, 0]
        b_init = tf.random_uniform_initializer(
            minval=gpi_bias[0], maxval=gpi_bias[1]
        )  #give them a positive bias; they will 'output' at baseline #will be a tough hyperparam to tune
        #split GPi into thirds.
        limit_1 = unit2
        limit_2 = unit2 * 2
        limit_3 = unit2 * 3
        #dorsal striatum input
        self.ds1_len = unit1
        self.ds2_len = unit1 * 2
        self.ds3_len = unit1 * 3
        self.stn1_len = unit1 * 3 + unit2
        self.stn2_len = unit1 * 3 + unit2 * 2
        self.stn3_len = unit1 * 3 + unit2 * 3
        self.str_dval = str_dval
        self.unit_diags = []
        for unit in range(self.units):
            unit_diag = []
            for input in range(self.input_dim):
                if input < len(self.str_dval):
                    if self.str_dval[input] == 2:  #if D2, don't pass through
                        unit_diag.append(0)
                        continue
                #GPi_1
                if unit in range(limit_1):
                    if input in range(self.ds1_len):
                        #100% dorsal striatum_1 -> GPi_1
                        unit_diag.append(1)
                    elif input in range(self.ds1_len, self.ds2_len):
                        #50% self.ds_2 -> GPi_1
                        if random.randint(2) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.ds2_len, self.ds3_len):
                        #0% self.ds_3 -> GPi_1
                        unit_diag.append(0)
                    elif input in range(self.ds3_len, self.stn1_len):
                        #100% self.stn_1 -> GPi_1
                        unit_diag.append(1)
                    elif input in range(self.stn1_len, self.stn2_len):
                        #50% self.stn_2 -> GPi_1
                        if random.randint(2) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.stn2_len, self.stn3_len):
                        #0% of self.stn_3 -> GPi_1
                        unit_diag.append(0)
                #GPi_2
                elif unit in range(limit_1, limit_2):
                    if input in range(self.ds1_len):
                        #50% self.ds_1 -> GPi_2
                        if random.randint(2) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.ds1_len, self.ds2_len):
                        #75% self.ds_2 -> GPi_2
                        if random.randint(4) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.ds2_len, self.ds3_len):
                        #50% self.ds_3 -> GPi_2
                        if random.randint(2) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.stn1_len):
                        #50% self.stn_1 -> GPi_2
                        if random.randint(2) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.stn1_len, self.stn2_len):
                        #75% self.stn_2 -> GPi_2
                        if random.randint(4) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.stn2_len, self.stn3_len):
                        #50% self.stn_3 -> GPi_2
                        if random.randint(2) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                #GPi_3
                elif unit in range(limit_2, limit_3):
                    if input in range(self.ds1_len):
                        #0% self.ds_1 -> GPi_3
                        unit_diag.append(0)
                    elif input in range(self.ds1_len, self.ds2_len):
                        #50% self.ds_2 -> GPi_3
                        if random.randint(2) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.ds2_len, self.ds3_len):
                        #100% self.ds_3 -> GPi_3
                        unit_diag.append(1)
                    elif input in range(self.stn1_len):
                        #0% self.stn_1 -> GPi_3
                        unit_diag.append(0)
                    elif input in range(self.stn1_len, self.stn2_len):
                        #50% self.stn_2 -> GPi_3
                        if random.randint(2) > 0:
                            unit_diag.append(1)
                        else:
                            unit_diag.append(0)
                    elif input in range(self.stn2_len, self.stn3_len):
                        #100% self.stn_3 -> GPi_3
                        unit_diag.append(1)

            self.unit_diags.append(np.array(unit_diag))
        self.unit_diags = np.array(self.unit_diags)
        if use_constraints:
            self.w = self.add_weight(shape=(self.input_dim, self.units),
                                     initializer=w_init,
                                     trainable=True,
                                     constraint=non_neg())
        else:
            self.w = self.add_weight(shape=(self.input_dim, self.units),
                                     initializer=w_init,
                                     trainable=True)
        #positive biases
        self.b = self.add_weight(shape=(self.units, ),
                                 initializer=b_init,
                                 trainable=True)
        self.s = tf.convert_to_tensor(np.array([
            sparse.diags(self.unit_diags[i], 0).toarray()
            for i in range(len(self.unit_diags))
        ]),
                                      dtype=tf.float32)
예제 #11
0
 def __init__(self, units):
     super(STN, self).__init__()
     unit1 = units[0]
     unit2 = units[1]
     unit3 = units[2]
     self.units = unit2 * 3
     self.input_dim = unit2 * 3
     self.training = True
     w_init = tf.random_uniform_initializer(minval=0., maxval=0.01)
     z_init = tf.zeros_initializer()
     stn_bias = [0, 0]
     b_init = tf.random_uniform_initializer(
         minval=stn_bias[0], maxval=stn_bias[1]
     )  #give them a positive bias; they will 'output' at baseline #will be a tough hyperparam to tune
     #split STN into thirds. The middle layer is a transition layer
     limit_1 = unit2
     limit_2 = unit2 * 2
     limit_3 = unit2 * 3
     #GPe input
     gpe_1_len = unit2
     gpe_2_len = unit2 * 2
     gpe_3_len = unit2 * 3
     self.unit_diags = []
     for unit in range(self.units):
         unit_diag = []
         for input in range(self.input_dim):
             #STN_1
             if unit in range(limit_1):
                 if input in range(gpe_1_len):
                     #100% of gpe_1 -> STN_1
                     unit_diag.append(1)
                 elif input in range(gpe_1_len, gpe_2_len):
                     #50% gpe_2 -> STN_1
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(gpe_2_len, gpe_3_len):
                     #0% of gpe_3 -> STN_1
                     unit_diag.append(0)
             #STN_2
             elif unit in range(limit_1, limit_2):
                 if input in range(gpe_1_len):
                     #50% gpe_2 -> STN_2
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(gpe_1_len, gpe_2_len):
                     #75% gpe_2 -> STN_2
                     if random.randint(4) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(gpe_2_len, gpe_3_len):
                     #50% gpe_3 -> STN_2
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
             #STN_3
             elif unit in range(limit_2, limit_3):
                 if input in range(gpe_1_len):
                     #0% gpe_1 -> STN_3
                     unit_diag.append(0)
                 elif input in range(gpe_1_len, gpe_2_len):
                     #50% gpe_2 -> STN_3
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(gpe_2_len, gpe_3_len):
                     #100% gpe_3 -> STN_3
                     unit_diag.append(1)
         self.unit_diags.append(np.array(unit_diag))
     self.unit_diags = np.array(self.unit_diags)
     if use_constraints:
         self.w = self.add_weight(shape=(self.input_dim, self.units),
                                  initializer=w_init,
                                  trainable=True,
                                  constraint=non_neg())
     else:
         self.w = self.add_weight(shape=(self.input_dim, self.units),
                                  initializer=w_init,
                                  trainable=True)
     #biases
     self.b = self.add_weight(shape=(self.units, ),
                              initializer=b_init,
                              trainable=True)
     self.s = tf.convert_to_tensor(np.array([
         sparse.diags(self.unit_diags[i], 0).toarray()
         for i in range(len(self.unit_diags))
     ]),
                                   dtype=tf.float32)
예제 #12
0
 def __init__(self, units, input_dim=15):
     super(Striatum, self).__init__()
     unit1 = units[0]
     unit2 = units[1]
     unit3 = units[2]
     self.units = unit1 * 3
     self.input_dim = input_dim
     self.training = True
     w_init = tf.random_uniform_initializer(minval=0., maxval=0.01)
     z_init = tf.zeros_initializer()
     str_bias = [0, 0]
     b_init = tf.random_uniform_initializer(minval=str_bias[0],
                                            maxval=str_bias[1])
     #self.w = []
     limit_1 = unit1
     limit_2 = unit1 * 2
     limit_3 = unit1 * 3
     goal_len = 3
     color_len = 6
     pos_len = 9
     act_len = 15
     self.dval = []
     self.unit_diags = []
     for unit in range(self.units):
         #for each unit, rand int, 50%, make it D1 or D2
         if random.randint(2) > 0:
             self.dval.append(1)
         else:
             self.dval.append(2)
         unit_diag = []
         for input in range(self.input_dim):
             #D1
             if unit in range(limit_1):
                 if input in range(goal_len):
                     #100% goal in D1
                     unit_diag.append(1)
                 elif input in range(goal_len, color_len):
                     #40% color in D1
                     if random.randint(5) > 2:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(color_len, pos_len):
                     #10% pos in D1
                     if random.randint(10) > 8:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(pos_len, act_len):
                     #0% last action in D1
                     unit_diag.append(0)
             #D2
             elif unit in range(limit_1, limit_2):
                 if input in range(goal_len):
                     #%50 goal in D2
                     if random.randint(10) > 4:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(goal_len, color_len):
                     #80% color in D2
                     if random.randint(5) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(color_len, pos_len):
                     #60% pos in D2
                     if random.randint(5) > 1:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(pos_len, act_len):
                     #20% last action to D1
                     if random.randint(10) > 7:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
             #D3
             elif unit in range(limit_2, limit_3):
                 if input in range(goal_len):
                     #%0 goal in D2
                     unit_diag.append(0)
                 elif input in range(goal_len, color_len):
                     #20% color in D2
                     if random.randint(5) > 3:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(color_len, pos_len):
                     #100% pos in D2
                     unit_diag.append(1)
                 elif input in range(pos_len, act_len):
                     #100% last action to D1
                     unit_diag.append(1)
         self.unit_diags.append(np.array(unit_diag))
     self.unit_diags = np.array(self.unit_diags)
     #biases -> Consider adding a negative bias
     if use_constraints:
         self.w = self.add_weight(shape=(self.input_dim, self.units),
                                  initializer=w_init,
                                  trainable=True,
                                  constraint=non_neg())
     else:
         self.w = self.add_weight(shape=(self.input_dim, self.units),
                                  initializer=w_init,
                                  trainable=True)
     self.b = self.add_weight(shape=(self.units, ),
                              initializer=b_init,
                              trainable=True)
     self.s = tf.convert_to_tensor(np.array([
         sparse.diags(self.unit_diags[i], 0).toarray()
         for i in range(len(self.unit_diags))
     ]),
                                   dtype=tf.float32)
예제 #13
0
 def __init__(self, units, str_dval=[]):
     super(GPe, self).__init__()
     unit1 = units[0]
     unit2 = units[1]
     unit3 = units[2]
     self.units = unit2 * 3
     self.input_dim = unit1 * 3
     self.training = True
     w_init = tf.random_uniform_initializer(minval=0., maxval=0.01)
     z_init = tf.zeros_initializer()
     gpe_bias = [0, 0]
     b_init = tf.random_uniform_initializer(
         minval=gpe_bias[0], maxval=gpe_bias[1]
     )  #give them a positive bias; they will 'output' at baseline #will be a tough hyperparam to tune
     #split GPe into thirds. The middle layer is really a transition layer is all
     limit_1 = unit2
     limit_2 = unit2 * 2
     limit_3 = unit2 * 3
     #dorsal striatum input
     ds1_len = unit1
     ds2_len = unit1 * 2
     ds3_len = unit1 * 3
     lateral_len = self.units  #will use in future; will have to sum input_dim too, input_dim += units
     self.str_dval = str_dval
     self.unit_diags = []
     for unit in range(self.units):
         unit_diag = []
         for input in range(self.input_dim):
             if self.str_dval[input] == 1 and random.randint(
                     2) > 0:  #if D1, only 50% pass through
                 unit_diag.append(0)
                 continue
             #GPe_1
             if unit in range(limit_1):
                 if input in range(ds1_len):
                     #100% of dorsal striatum_1 -> GPe_1
                     unit_diag.append(1)
                 elif input in range(ds1_len, ds2_len):
                     #50% ds_2 -> GPe_1
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(ds2_len, ds3_len):
                     #0% of ds_3 -> GPe_1
                     unit_diag.append(0)
             #GPe_2
             elif unit in range(limit_1, limit_2):
                 if input in range(ds1_len):
                     #50% ds_1 -> GPe_2
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(ds1_len, ds2_len):
                     #75% ds_2 -> GPe_2
                     if random.randint(4) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(ds2_len, ds3_len):
                     #50% ds_3 -> GPe_2
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
             #GPe_3
             elif unit in range(limit_2, limit_3):
                 if input in range(ds1_len):
                     #0% ds_1 -> GPe_3
                     unit_diag.append(0)
                 elif input in range(ds1_len, ds2_len):
                     #50% ds_2 -> GPe_3
                     if random.randint(2) > 0:
                         unit_diag.append(1)
                     else:
                         unit_diag.append(0)
                 elif input in range(ds2_len, ds3_len):
                     #100% ds_3 -> GPe_3
                     unit_diag.append(1)
         self.unit_diags.append(np.array(unit_diag))
     self.unit_diags = np.array(self.unit_diags)
     if use_constraints:
         self.w = self.add_weight(shape=(self.input_dim, self.units),
                                  initializer=w_init,
                                  trainable=True,
                                  constraint=non_neg())
     else:
         self.w = self.add_weight(shape=(self.input_dim, self.units),
                                  initializer=w_init,
                                  trainable=True)
     #positive biases
     self.b = self.add_weight(shape=(self.units, ),
                              initializer=b_init,
                              trainable=True)
     self.s = tf.convert_to_tensor(np.array([
         sparse.diags(self.unit_diags[i], 0).toarray()
         for i in range(len(self.unit_diags))
     ]),
                                   dtype=tf.float32)