Related
Can you please tell me how to build an autoencoder using CNN and pooling layers, with a single matrix(4,4) with integer numbers?
e.g,
input data = array([[ 4, 3, 8, 6], [ 1, 1, 2, 2], [24, 18, 32, 24], [ 6, 6, 8, 8]])
autoencoder(data)
output data= array([[ 4, 3, 8, 6], [ 1, 1, 2, 2], [24, 18, 32, 24], [ 6, 6, 8, 8]])
Explanation:
https://medium.com/machine-learning-researcher/auto-encoder-d942a29c9807
The article you quoted is already well detailed, all you need to do is replaced linear units by convolution and max pool in the encoding part and deconvolution and upsampling in the decoding part.
Here is in example using Keras of a convolutional autoencoder.
But if you're new to Deep Learning I would not advise you to start here. Autoencoder are tricky, the expected output you described above could easily be obtained by setting:
def autoencoder(x):
return x
Which is not something you want.
People commonly design "undercomplete" autoencoders, with internal layer of smaller dimension to induce some compression. But "overcomplete" autoencoders with larger internal dimension are also valid architecture combined with regularization.
I am trying to reduce the resolution of an image to speed up training. So I used tf.nn.max_pool method to operate on my raw image. I am expecting the resultant image is a blurred one with smaller size, but actually it is not.
My raw image has shape [320, 240, 3], and it looks like:
And after max_pooling, with ksize=[1,2,2,1] and strides=[1,2,2,1] it becomes
produced by the following code:
# `img` is an numpy.array with shape [320, 240, 3]
# since tf.nn.max_pool only receives tensor with size
# [batch_size, height,width,channel], so I need to reshape
# the image to have a dummy dimension.
img_tensor = tf.placeholder(tf.float32, shape=[1,320,240,3])
pooled = tf.nn.max_pool(img_tensor, ksize=[1,2,2,1], strides=[1,2,2,1],padding='VALID')
pooled_img = pooled.eval(feed_dict={img_tensor: img.reshape([1,320,240,3])})
plt.imshow(np.squeeze(pooled_img, axis=0))
The pooled image has shape [160, 120, 3] which is expected. Its just the transformation behaviour is really confused me. It shouldnt have that "repeated shifting" behaviour, since there is no pixel overlapping computation.
Many thanks in advance.
I think the problem is how your image has been reshaped. This image actually has the shape of [240, 320, 3].
So try to use [1, 240, 320, 3]) instead of [1, 320, 240, 3]). It should work.
My question:
I have searched through available Ruby gems to find one that performs k-means clustering. I've found quite a few: kmeans, kmeans-clustering, reddavis-k_means and k_means_pp. My problem is that none of the gems deals with one-dimensional k-means clustering. They all expect input like this:
[[1, 2], [3, 4], [5, 6]]
My input looks like this:
[1, 2, 3, 4, 5, 6]
Hence my question: How do I perform a one-dimensional k-means clustering using Ruby?
The context (my task):
I have 100 input values:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 5, 8, 8, 10, 16, 18, 22, 22, 35, 50, 50
Each value represents a response time, i.e. the number of minutes it took for some customer service agent to respond to an email from a customer. So the first value 0 indicates that the customer only waited 0 minutes for a response.
I need to find out how many fast, medium-fast and slow response time instances there is. In other words, I want to cut my input values up in 3 pools, and then count how many there are in each pool.
The complicating factor is that I based on the overall slope steepness have to figure out where to make the cuts. There is no fixed definition of fast, medium-fast and slow. The first cut (between fast and medium-fast) should occur where the steepness of the slope starts to increase more drastically than before. The second cut (between medium-fast and slow) should occur when an even more dramatic steepness increase occur.
Here is a graphical representation of the input values.
In the above example, common sense would probably define fast as 0-3, because there are many instances of 0, 1, 2, and 3. 4-8 or 4-10 looks like common sense choices for medium-fast. But how to determine something like this mathematically? If the response times were generally faster, then the customers would be expecting this, so an even smaller increase towards the end should trigger the cut.
Finishing notes:
I did find the gem davidrichards-kmeans that deals with one-dimensional k-means clustering, but it don't seem to work properly (the example code raises a syntax error).
k-means is the wrong tool for this job anyway.
It's not designed for fitting an exponential curve.
Here is a much more sound proposal for you:
Look at the plot, mark the three points, and then you have your three groups.
Or look at quantiles... Report the median response time, the 90% quantile, and the 99% quantile...
Clustering is about structure discovery in multivariate data. It's probably not what you want it to be, sorry.
If you insist on trying k-means, try encoding the data as
[[1], [2], [3], [4], [5]]
and check if the results are at least a little bit what you want them to be (also remember that k-means is randomized. Running it multiple times may yield very different results).
What does <> (less than followed by greater than) mean in Mathematica? For example:
InterpolatingFunction[{-6,6},{0,6}],<>[x,y]
I am very much confused in such kind of expressions. As I have received such kind of output in NDSolve.
Mathematica expressions come with a head and then several arguments. For example, the output of some operation might give you the output List[1,2,3,4,5]. However, Mathematica knows this is a list and the output is formatted as {1,2,3,4,5} instead.
A function like Interpolation will give you a special type of object (an interpolating function) that has many components. Unlike list, most of its components are irrelevant, so you can ignore them. Mathematica hides them using <> so that you don't have to look at them.
f = Interpolation[RandomInteger[10, 10]]
output: InterpolatingFunction[{{1, 10}}, "<>"]
All it shows you is the Head, which is InterpolatingFunction, and then the first argument which is the domain(s) of the function. There is only one variable, so there is only one domain {1,10} so the list of domains is {{1,10}}.
All the other arguments are there, so you can find them. You can evaluate f by:
f[2.3]
output: 0.7385
(Your output will vary!) But you can also look at the pieces of f:
f[[2]]
output: {4, 3, 0, {10}, {4}, 0, 0, 0, 0, Automatic}
The second piece, normally hidden, is a list of different properties of the interpolating function that we normally don't care about.
You can change the head on many things using ## which changes the header of one thing into another. For example:
mylist = {2,3,4,5};
Plus##mylist
output: 14
You can do this with our function:
List##f
output: {{{1, 10}}, {4, 3, 0, {10}, {4}, 0, 0, 0, 0, Automatic},
{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, {{9}, {2}, {0}, {6},
{10}, {6}, {7}, {5}, {0}, {6}}, {Automatic}}
All of that is the "guts" of the interpolating function. That's what's missing in the <>, because this might describe the interpolating function but we don't really need to see it.
If you are looking for an explicit polynomial interpolation, you should be doing:
InterpolatingPolynomial[RandomInteger[10, 10], x]
which gives you a function of x (in a very non-simplified form) that is what you want.
I'm putting together a simple chess position evaluation function. This being the first time for me building a chess engine, I am feeling very tentative with putting in just any evaluation function. The one shown on this Chess Programming Wiki page looks like a good candidate. However this has an ellipsis at the end which makes me unsure of whether it will be a good one to use?
Once the whole engine is in place and functional, I intend to come back to the evaluation function and make a real attempt to sorting it out properly. But for now I need some sort of function which is good enough to play against an average amateur.
The most basic component of an evaluation function is material, obviously. This should be perfectly straightforward, but on its own does not lead to interesting play. The engine has no sense of position at all, and simply reacts to tactical lines. But we will start here:
value = white_material - black_material // calculate delta material
Next we introduce some positional awareness through piece-square tables. For example, this is a such a predefined table for pawns:
pawn_table = {
0, 0, 0, 0, 0, 0, 0, 0,
75, 75, 75, 75, 75, 75, 75, 75,
25, 25, 29, 29, 29, 29, 25, 25,
4, 8, 12, 21, 21, 12, 8, 4,
0, 4, 8, 17, 17, 8, 4, 0,
4, -4, -8, 4, 4, -8, -4, 4,
4, 8, 8,-17,-17, 8, 8, 4,
0, 0, 0, 0, 0, 0, 0, 0
}
Note that this assumes the common centipawn (value of pawn is ~100) value system. For each white pawn we encounter, we index into the table with the pawn's square and add the corresponding value.
for each p in white pawns
value += pawn_table[square(p)]
Note that we can use use a simple calculation to reflect the table when indexing for black pieces. Alternatively you can define separate tables.
For simple evaluation this will work very well and your engine will probably already be playing common openings. However, it's not too hard to make some simple improvements. For example, you can create tables for the opening and the endgame, and interpolate between them using some sort of phase calculation. This is especially effective for kings, where their place shifts from the corners to the middle of the board as the game progresses.
Thus our evaluation function may look something like:
evaluate(position, colour) {
phase = total_pieces / 32 // this is just an example
opening_value += ... // sum of evaluation terms
endgame_value += ...
final_value = phase * opening_value + (1 - phase) * endgame_value
return final_value * sign(colour) // adjust for caller's perspective
}
This type of evaluation, along with quiescence search, should be enough to annihilate most amateurs.