<commit_before># -*- coding: utf-8 -*-

import tensorflow as tf
import tf_sparsenet
import numpy as np
import scipy.linalg

class TopoSparsenet(tf_sparsenet.Sparsenet):
    """Topographic Sparsenet with TensorFlow backend and a few methods for defining topologies."""
	
    def __init__(self, data, datatype="image", pca=None,
      dict_shape = (30,30), topo = None, lam_g=0.1,
      **kwargs):
        """
        Topographic Sparsenet inherits from Sparsenet. Its unique
        attributes give the dictionary a shape and define the relative 
        weight of the topographic term in the cost function.
        The topology matrix g is defined by the topology object topo.
        
        Args:
        lam_g : float, defines weight of topography term
        dict_shape : tuple (len, wid) of ints specifying shape of dictionary
        """
        self.lam_g = lam_g
        self.epsilon = 0.0001 # to regularize derivative of square root
        self.dict_shape = dict_shape
        nunits = int(np.prod(self.dict_shape))
        self.topo = topo or topology((nunits, nunits))
        self.nunits = topo.ncomponents * nunits
        try:
            kwargs['lam']
            super().__init__(data, datatype = datatype, pca = pca, **kwargs)
        except KeyError:
            super().__init__(data, datatype = datatype, pca = pca, lam=0, **kwargs)

    def build_graph(self):
        self.g = tf.constant(self.topo.get_matrix(), dtype=tf.float32)
        
        self.infrate = tf.Variable(self.infrate, trainable=False)
        self.learnrate = tf.Variable(self.learnrate, trainable=False)
        
        self.phi = tf.Variable(tf.random_normal([self.nunits,self.stims.datasize]))
        self.acts = tf.Variable(tf.zeros([self.nunits,self.batch_size]))
        self.reset_acts = self.acts.assign(tf.zeros([self.nunits,self.batch_size]))
        
        self.X = tf.Variable(tf.zeros([self.batch_size, self.stims.datasize]), trainable=False)
        self.Xhat = tf.matmul(tf.transpose(self.acts), self.phi, name='Xhat')
        self.resid = self.X - self.Xhat
        self.mse = tf.reduce_sum(tf.square(self.resid))/self.batch_size/self.stims.datasize
        self.meanL1 = tf.reduce_sum(tf.abs(self.acts))/self.batch_size
        self.layer2 = tf.reduce_sum(tf.sqrt(tf.matmul(self.g,tf.square(self.acts),
            name='g_times_acts') + self.epsilon))/self.batch_size
        self.loss = 0.5*self.mse + (self.lam*self.meanL1 + self.lam_g*self.layer2)/self.stims.datasize
        
        inferer = tf.train.GradientDescentOptimizer(self.infrate)
        inf_step = tf.Variable(0, name='inf_step', trainable=False)
        self.inf_op = inferer.minimize(self.loss, global_step=inf_step, var_list=[self.acts])
        
        learner = tf.train.GradientDescentOptimizer(self.learnrate)
        learn_step = tf.Variable(0,name='learn_step', trainable=False)<commit_msg>fixed bug in setting dict size<commit_after># -*- coding: utf-8 -*-

import tensorflow as tf
import tf_sparsenet
import numpy as np
import scipy.linalg

class TopoSparsenet(tf_sparsenet.Sparsenet):
    """Topographic Sparsenet with TensorFlow backend and a few methods for defining topologies."""
	
    def __init__(self, data, datatype="image", pca=None,
      dict_shape = (30,30), topo = None, lam_g=0.1,
      **kwargs):
        """
        Topographic Sparsenet inherits from Sparsenet. Its unique
        attributes give the dictionary a shape and define the relative 
        weight of the topographic term in the cost function.
        The topology matrix g is defined by the topology object topo.
        
        Args:
        lam_g : float, defines weight of topography term
        dict_shape : tuple (len, wid) of ints specifying shape of dictionary
        """
        self.lam_g = lam_g
        self.epsilon = 0.0001 # to regularize derivative of square root
        self.dict_shape = dict_shape
        nunits = int(np.prod(self.dict_shape))
        self.topo = topo or topology((nunits, nunits))
        nunits = topo.ncomponents * nunits
        try:
            kwargs['lam']
            super().__init__(data, nunits=nunits, datatype = datatype, pca = pca, **kwargs)
        except KeyError:
            super().__init__(data, nunits=nunits, datatype = datatype, pca = pca, lam=0, **kwargs)

    def build_graph(self):
        self.g = tf.constant(self.topo.get_matrix(), dtype=tf.float32)
        
        self.infrate = tf.Variable(self.infrate, trainable=False)
        self.learnrate = tf.Variable(self.learnrate, trainable=False)
        
        self.phi = tf.Variable(tf.random_normal([self.nunits,self.stims.datasize]))
        self.acts = tf.Variable(tf.zeros([self.nunits,self.batch_size]))
        self.reset_acts = self.acts.assign(tf.zeros([self.nunits,self.batch_size]))
        
        self.X = tf.Variable(tf.zeros([self.batch_size, self.stims.datasize]), trainable=False)
        self.Xhat = tf.matmul(tf.transpose(self.acts), self.phi, name='Xhat')
        self.resid = self.X - self.Xhat
        self.mse = tf.reduce_sum(tf.square(self.resid))/self.batch_size/self.stims.datasize
        self.meanL1 = tf.reduce_sum(tf.abs(self.acts))/self.batch_size
        self.layer2 = tf.reduce_sum(tf.sqrt(tf.matmul(self.g,tf.square(self.acts),
            name='g_times_acts') + self.epsilon))/self.batch_size
        self.loss = 0.5*self.mse + (self.lam*self.meanL1 + self.lam_g*self.layer2)/self.stims.datasize
        
        inferer = tf.train.GradientDescentOptimizer(self.infrate)
        inf_step = tf.Variable(0, name='inf_step', trainable=False)
        self.inf_op = inferer.minimize(self.loss, global_step=inf_step, var_list=[self.acts])
        
        learner = tf.train.GradientDescentOptimizer(self.learnrate)
        learn_step = tf.Variable(0,name='learn_step', trainable=False)
{'fraction_non_alphanumeric': 0.11820371644872678, 'fraction_numerical': 0.007914659325533379, 'mean_word_length': 2.831905075807515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6491854', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1880, 'n_words': 384}