<commit_before>import numpy as np
import tensorflow as tf
from . import optimizee

from sklearn.datasets import load_digits, fetch_mldata
from sklearn.preprocessing import StandardScaler


class DIGITSClassifier(optimizee.Optimizee):
    name = 'digits_classifier'

    def __init__(self, num_units=20, num_layers=1, dataset_name='digits', activation='sigmoid'):
        if dataset_name == 'digits':
            dataset = load_digits(n_class=10)
        elif dataset_name == 'mnist':
            dataset = fetch_mldata('MNIST original', data_home='/srv/hd1/data/vyanush/')

        self.X, self.Y = dataset.data, dataset.target
        self.X = self.X.astype(np.float32)
        self.X = StandardScaler().fit_transform(self.X).astype(np.float32)

        self.num_units = num_units
        self.num_layers = num_layers
        self.activation = activation


    def get_x_dim(self):
        return self.dim


    def build(self):
        with tf.variable_scope('digits_classifier'):
            self.dim = tf.placeholder(tf.int32, [], name='dim')
            self.x = tf.placeholder(tf.float32, [None, None, None, None], name='X') # n_bptt_steps * batch_size * data_size * num_features
            self.y = tf.placeholder(tf.int32, [None, None, None], name='y')

 
    def loss(self, x, i):
        batch_size = tf.shape(x)[0]
        activation = getattr(tf.nn, self.activation)

        weights = []
        n_inputs = self.X.shape[1]

        fs = []
        s = 0

        dims = [n_inputs] + [self.num_units] * self.num_layers + [10]

        # self.x[i].shape == (batch_size, data_size, n_inputs)
        pred = tf.transpose(self.x[i], perm=[0, 2, 1])

        for i in range(1, len(dims)):
            n_inputs = dims[i - 1]
            n_outputs = dims[i]
            dim = n_inputs * n_outputs

            W = tf.slice(x, [0, s], [batch_size, dim])
            W = tf.reshape(W, [batch_size, n_outputs, n_inputs])
            b = tf.slice(x, [0, s + dim], [batch_size, n_outputs])
            b = tf.expand_dims(b, axis=-1)

            pred = tf.matmul(W, pred) + b

            if i + 1 < len(dims):
                #pred = tf.nn.sigmoid(pred)
                pred = activation(pred)

            s += (n_inputs + 1) * n_outputs

        pred = tf.transpose(pred, perm=[0, 2, 1]) # shape = (batch_size, data_size, n_classes)
        f = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.y[i], logits=pred), axis=-1)
    
        p = tf.argmax(tf.nn.softmax(pred), axis=-1)
        acc = tf.reduce_mean(tf.cast(tf.equal(tf.cast(p, tf.int32), self.y[i]), tf.float32), axis=1)

        g = self.grad(x, f)
        #return f, g
        return acc, g


    def get_initial_x(self, batch_size=1):
        self.batch_size = np.random.randint(low=1, high=self.X.shape[0] // 4 + 1)
        print("Digits classifier; batch_size: ", self.batch_size)

        self.x_len = 0
    
        dims = [self.X.shape[1]] + [self.num_units] * self.num_layers + [10]
        for i in range(1, len(dims)):<commit_msg>Fixed bug with shadowed batch index<commit_after>import numpy as np
import tensorflow as tf
from . import optimizee

from sklearn.datasets import load_digits, fetch_mldata
from sklearn.preprocessing import StandardScaler


class DIGITSClassifier(optimizee.Optimizee):
    name = 'digits_classifier'

    def __init__(self, num_units=20, num_layers=1, dataset_name='digits', activation='sigmoid', return_func=False):
        if dataset_name == 'digits':
            dataset = load_digits(n_class=10)
        elif dataset_name == 'mnist':
            dataset = fetch_mldata('MNIST original', data_home='/srv/hd1/data/vyanush/')

        self.X, self.Y = dataset.data, dataset.target
        self.X = self.X.astype(np.float32)
        self.X = StandardScaler().fit_transform(self.X).astype(np.float32)

        self.num_units = num_units
        self.num_layers = num_layers
        self.activation = activation
        self.return_func = return_func


    def get_x_dim(self):
        return self.dim


    def build(self):
        with tf.variable_scope('digits_classifier'):
            self.dim = tf.placeholder(tf.int32, [], name='dim')
            self.x = tf.placeholder(tf.float32, [None, None, None, None], name='X') # n_bptt_steps * batch_size * data_size * num_features
            self.y = tf.placeholder(tf.int32, [None, None, None], name='y')

 
    def loss(self, x, i):
        batch_size = tf.shape(x)[0]
        activation = getattr(tf.nn, self.activation)

        weights = []
        n_inputs = self.X.shape[1]

        fs = []
        s = 0

        dims = [n_inputs] + [self.num_units] * self.num_layers + [10]

        # self.x[i].shape == (batch_size, data_size, n_inputs)
        pred = tf.transpose(self.x[i], perm=[0, 2, 1])

        for j in range(1, len(dims)):
            n_inputs = dims[j - 1]
            n_outputs = dims[j]
            dim = n_inputs * n_outputs

            W = tf.slice(x, [0, s], [batch_size, dim])
            W = tf.reshape(W, [batch_size, n_outputs, n_inputs])
            b = tf.slice(x, [0, s + dim], [batch_size, n_outputs])
            b = tf.expand_dims(b, axis=-1)

            pred = tf.matmul(W, pred) + b

            if j + 1 < len(dims):
                #pred = tf.nn.sigmoid(pred)
                pred = activation(pred)

            s += (n_inputs + 1) * n_outputs

        pred = tf.transpose(pred, perm=[0, 2, 1]) # shape = (batch_size, data_size, n_classes)
        f = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.y[i], logits=pred), axis=-1)
    
        p = tf.argmax(tf.nn.softmax(pred), axis=-1)
        acc = tf.reduce_mean(tf.cast(tf.equal(tf.cast(p, tf.int32), self.y[i]), tf.float32), axis=1)

        g = self.grad(x, f)

        if self.return_func:
            return f, g
        else:
            return acc, g


    def get_initial_x(self, batch_size=1):
        self.batch_size = np.random.randint(low=1, high=self.X.shape[0] // 4 + 1)
        print("Digits classifier; batch_size: ", self.batch_size)

        self.x_len = 0
    
        dims = [self.X.shape[1]] + [self.num_units] * self.num_layers + [10]
        for i in range(1, len(dims)):
{'fraction_non_alphanumeric': 0.13826522371183977, 'fraction_numerical': 0.016475528993700535, 'mean_word_length': 2.3398058252427183, 'pattern_counts': {'":': 0, '<': 5, '<?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': '904224', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2147, 'n_words': 446}