Encoding invariance for deep neural network - tensorflow

I have a set of data, 2D matrix (like Grey pictures).
And use CNN for classifier.
Would like to know if there is any study/experience on the accuracy impact
if we change the encoding from traditionnal encoding.
I suppose yes, question is rather which transformation of the encoding make the accuracy invariant, which one deteriorates....
To clarify, this concerns mainly the quantization process of the raw data into input data.
EDIT:
Quantize the raw data into input data is already a pre-processing of the data, adding or removing some features (even minor). It seems not very clear the impact in term of accuracy on this quantization process on real dnn computation.
Maybe, some research available.

I'm not aware of any research specifically dealing with quantization of input data, but you may want to check out some related work on quantization of CNN parameters: http://arxiv.org/pdf/1512.06473v2.pdf. Depending on what your end goal is, the "Q-CNN" approach may be useful for you.
My own experience with using various quantizations of the input data for CNNs has been that there's a heavy dependency between the degree of quantization and the model itself. For example, I've played around with using various interpolation methods to reduce image sizes and reducing the color palette size, and in the end, I discovered that each variant required a different tuning of hyper-parameters to achieve optimal results. Generally, I found that minor quantization of data had a negligible impact, but there was a knee in the curve where throwing away additional information dramatically impacted the achievable accuracy. Unfortunately, I'm not aware of any way to determine what degree of quantization will be optimal without experimentation, and even deciding what's optimal involves a trade-off between efficiency and accuracy which doesn't necessarily have a one-size-fits-all answer.
On a theoretical note, keep in mind that CNNs need to be able to find useful, spatially-local features, so it's probably reasonable to assume that any encoding that disrupts the basic "structure" of the input would have a significantly detrimental effect on the accuracy achievable.

In usual practice -- a discrete classification task in classic implementation -- it will have no effect. However, the critical point is in the initial computations for back-propagation. The classic definition depends only on strict equality of the predicted and "base truth" classes: a simple right/wrong evaluation. Changing the class coding has no effect on whether or not a prediction is equal to the training class.
However, this function can be altered. If you change the code to have something other than a right/wrong scoring, something that depends on the encoding choice, then encoding changes can most definitely have an effect. For instance, if you're rating movies on a 1-5 scale, you likely want 1 vs 5 to contribute a higher loss than 4 vs 5.
Does this reasonably deal with your concerns?
I see now. My answer above is useful ... but not for what you're asking. I had my eye on the classification encoding; you're wondering about the input.
Please note that asking for off-site resources is a classic off-topic question category. I am unaware of any such research -- for what little that is worth.
Obviously, there should be some effect, as you're altering the input data. The effect would be dependent on the particular quantization transformation, as well as the individual application.
I do have some limited-scope observations from general big-data analytics.
In our typical environment, where the data were scattered with some inherent organization within their natural space (F dimensions, where F is the number of features), we often use two simple quantization steps: (1) Scale all feature values to a convenient integer range, such as 0-100; (2) Identify natural micro-clusters, and represent all clustered values (typically no more than 1% of the input) by the cluster's centroid.
This speeds up analytic processing somewhat. Given the fine-grained clustering, it has little effect on the classification output. In fact, it sometimes improves the accuracy minutely, as the clustering provides wider gaps among the data points.
Take with a grain of salt, as this is not the main thrust of our efforts.

Related

Neural network hyperparameter tuning - is setting random seed a good idea? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am trying to tune a basic neural network as practice. (Based on an example from a coursera course: Neural Networks and Deep Learning - DeepLearning.AI)
I face the issue of the random weight initialization. Lets say I try to tune the number of layers in the network.
I have two options:
1.: set the random seed to a fixed value
2.: run my experiments more times without setting the seed
Both version has pros and cons.
My biggest concern is that if I use a random seed (e.g.: tf.random.set_seed(1)) then the determined values can be "over-fitted" to the seed and may not work well without the seed or if the value is changed (e.g.: tf.random.set_seed(1) -> tf.random.set_seed(2). On the other hand, if I run my experiments more times without random seed then I can inspect less option (due to limited computing capacity) and still only inspect a subset of possible random weight initialization.
In both cases I feel that luck is a strong factor in the process.
Is there a best practice how to handle this topic?
Has TensorFlow built in tools for this purpose? I appreciate any source of descriptions or tutorials. Thanks in advance!
Tuning hyperparameters in deep learning (generally in machine learning) is a common issue. Setting the random seed to a fixed number ensures reproducibility and fair comparison. Repeating the same experiment will lead to the same outcomes. As you probably know, best practice to avoid over-fitting is to do a train-test split of your data and then use k-fold cross-validation to select optimal hyperparameters. If you test multiple values for a hyperparameter, you want to make sure other circumstances that might influence the performance of your model (e.g. train-test-split or weight initialization) are the same for each hyperparameter in order to have a fair comparison of the performance. Therefore I would always recommend to fix the seed.
Now, the problem with this is, as you already pointed out, the performance for each model will still depend on the random seed, like the particular data split or weight initialization in your case. To avoid this, one can do repeated k-fold-cross validation. That means you repeat the k-fold cross-validation multiple times, each time with a different seed, select best parameters of that run, test on test data and average the final results to get a good estimate of performance + variance and therefore eliminate the influence the seed has in the validation process.
Alternatively you can perform k-fold cross validation a single time and train each split n-times with a different random seed (eliminating the effect of weight initialization, but still having the effect of the train-test-split).
Finally TensorFlow has no build-in tool for this purpose. You as practitioner have to take care of this.
There is no an absolute right or wrong answer to your question. You are almost answered your own question already. In what follows, however, I will try to expand more, via the following points:
The purpose of random initialization is to break the symmetry that makes neural networks fail to learn:
... the only property known with complete certainty is that the
initial parameters need to “break symmetry” between different units.
If two hidden units with the same activation function are connected to
the same inputs, then these units must have different initial
parameters. If they have the same initial parameters, then a
deterministic learning algorithm applied to a deterministic cost and
model will constantly update both of these units in the same way...
Deep Learning (Adaptive Computation and Machine Learning series)
Hence, we need the neural network components (especially weights) to be initialized by different values. There are some rules of thumb of how to choose those values, such as the Xavier initialization, which samples from normal distribution with mean of 0 and special variance based on the number of the network layer. This is a very interesting article to read.
Having said so, the initial values are important but not extremely critical "if" proper rules are followed, as per mentioned in point 2. They are important because large or improper ones may lead to vanishing or exploding gradient problems. On the other hand, different "proper" weights shall not hugely change the final results, unless they are making the aforementioned problems, or getting the neural network stuck at some local maxima. Please note, however, the the latter depends also on many other aspects, such as the learning rate, the activation functions used (some explode/vanish more than others: this is a great comparison), the architecture of the neural network (e.g. fully connected, convolutional ..etc: this is a cool paper) and the optimizer.
In addition to point 2, bringing a good learning optimizer into the bargain, other than the standard stochastic one, shall in theory not let a huge influence of the initial values to affect the final results quality, noticeably. A good example is Adam, which provides a very adaptive learning technique.
If you still get a noticeably-different results, with different "proper" initialized weights, there are some ways that "might help" to make neural network more stable, for example: use a Train-Test split, use a GridSearchCV for best parameters, and use k-fold cross validation...etc.
At the end, obviously the best scenario is to train the same network with different random initial weights many times then get the average results and variance, for more specific judgement on the overall performance. How many times? Well, if can do it hundreds of times, it will be better, yet that clearly is almost impractical (unless you have some Googlish hardware capability and capacity). As a result, we come to the same conclusion that you had in your question: There should be a tradeoff between time & space complexity and reliability on using a seed, taking into considerations some of the rules of thumb mentioned in previous points. Personally, I am okay to use the seed because I believe that, "It’s not who has the best algorithm that wins. It’s who has the most data". (Banko and Brill, 2001). Hence, using a seed with enough (define enough: it is subjective, but the more the better) data samples, shall not cause any concerns.

how 'negative sampling' improve word representation quality in word2vec?

negative sampling in 'word2vec' improves the training speed, that's obviously!
but why 'makes the word representations significantly more accurate.'?
I didn't find the relevant discussion or details. can u help me?
It's hard to describe what the author of that claim may have meant, without the full context of where it appeared. For example, word-vectors can be optimized for different tasks, and the same options that make word-vectors better for one task might make them worse for another.
One popular way to evaluate word-vectors since Google's original paper & code release is a set of word-analogy problems. These give a nice repeatable summary 'accuracy' percentage, so the author might have meant that for a particular training corpus, on that particular problem, holding other things constant, the negative-sampling mode had a higher 'accuracy' score.
But that wouldn't mean it's always better, with any corpus, or for any other downstream evaluation of quality or accuracy-on-some-task.
Projects with larger corpuses, and especially larger vocabularies (more unique words), tend to prefer the negative-sampling mode. The hierarchical-softmax alternative mode becomes slower as the vocabulary becomes larger, while the negative-sampling mode does not.
And, having a large, diverse corpus, with many subtly-different usage examples of all interesting words, is the most important contributor to really good word-vectors.
So, simply by making larger corpuses manageable, within a limited amount of training time, negative-sampling could be seen as indirectly enabling improved word-vectors - because corpus size is such an important factor.

Tensorflow: Fixed point quntization for a embedded DSP

Apologies for my newbie question.
From documents like these I understand the advantages of using 8bit numbers to save on memory and increase the performance for very small impact on accuracy:
https://www.tensorflow.org/performance/quantization
Other blogs mention these quantized models can be offloaded to DSPs, I have low-cost DSP which can do 168 multiplications with an addition with 9bit inputs in a single clock at very low power consumption and I would like to use it to do inference on some models I trained. I don't want to use any present frameworks as they will not fit/work on the target anyway. I would like to just train the model, save it and then read the weights myself while I will hardcode the flow/graph of the network by hand just as a proof of the concept.
When I look at it as a compression-only it makes sense to use min/max for layer + 8bit weights, which can cause the non-symmetrical ranges and the 0 is not in the middle of the 8bit range. Decompressing it into a 32bit real value to make the calculation can be done easily.
But still it's mentioned in multiple blogs that this approach can be used directly on DSPs and do calculations with 8bit numbers. I still can't grasp how this could be implemented. In past I used to have fixed point math where I pretended somewhere is a decimal point and then shifted the result after multiplication. But I think this can't be done when the min/max/non-symmetrical approach is used to train/store the model. Am I missing something because I can't understand how this could be implemented in low-level with simple integer multipliers in DSPs?

Classification Precision Optimization towards one class?

My dataset has two classes. The one of non-interested takes 90%, and the class of interest is about 10%.
I have already done the resampling, not only once, but a butch of balanced sets (e.g 10 sets). And do the majority vote to get the final prediction results. After compared many models, tree gives the best result. And I have already picked out the most important features based on importance scores.
The overall accuracy is not bad, 75%, but the precision towards the class I am interested is only 30%, which is not good. How to do the optimization towards the precision of the target class? I think the algorithms behind the ctree package in R is to do optimization towards the overall accuracy. I also tried the one-class classification, like svm, but not good. BTW, I used R and python both. But I do not find any relevant packages about my problem. Do I need to write my own tree algorithm which will optimize the precision of the class interested? Thanks.
There are plenty of models which give you ability to weight classes. This in general is better than just oversampling as it directly alternates the objective, not artificially tricks the model to overweight. If you use python, and like tree-based approach, Random Forest in scikit-learn has class-weight capabilities, simply overweight your minority class as long as the desired precision is not obtained.

What is the advantage of the paperboat format in performance optimization of ML?

The paperBoat format claims to provide a better dataset representation for machine learning routines. I'd like to understand the nature of its optimization. I understand that using an integer representation for model attributes means a faster processing of the data set, what are the other improvements.
Also, how to tune an ML algorithm to work with this file format.
I don't know if this format really provides better representation, but I can speculate why it can be more efficient.
First, as they state at format description, "Having data of the same precision consecutive enables hardware vectorization."; consider also wikipedia: "Vector processing techniques have since been added to almost all modern CPU designs".
Second, their format allows you to mix sparse and non-sparse features, but since all sparse features are placed consequently, it is possible to easily take them as a sparse matrix and optimize methods for learning like conjugate gradient.
how to tune an ML algorithm to work with this file format?
What do you mean by ML algorithm tuning? The learning algorithm doesn't know and doesn't need to know anything about file format of the dataset; and you can't increase or decrease accuracy if you know file format. In theory, you can speed up the concrete optimization algorithm (like Gradient descent) if you can rely on some properties of data (and, I guess, Ismion PaperBoat does it), but I don't think that you can tune it by yourself.