For example, I know that I can represent the value -1 in this way, where "n" is the bit size of the type, n-1 is the most significant bit, and the value -2, which gives the complement representation of 2, plus the sum of the sum described below, now, I would like to know how I can use the sum, or any other type of calculation, to represent any negative value, in the form of a mathematical demonstration.
Assuming n has 8 bits, 8 - 1, 7, that is -2^7 = -128 + 127 = -1,
I'm saying the summation will go through all the bits, such as 2^0 + 2^1, ..., 2^6, ie I ignore the most significant bit.
Iiuc, the formula you gave does exactly what you're asking to do. You have answered your own question.
Let's do examples with n=4. For a reference, we can count in the negative direction to get all possibilities:
-1 = 1111
-2 = 1110
-3 = 1101
-4 = 1100
-5 = 1011
-6 = 1010
-7 = 1001
-8 - 1000
So now let's see if the formula gives correct results. By convention, a 4-digit binary number representation is four bits numbered 3 down to 0: b_3 b_2 b_1 b_0.
Example: 1111
So we have b_0 = b_1 = b_2 = b_3 = 1.
Substituting, v = -2^3x1 + 1x1 + 2x1 + 4x1 = -8 + 7 = -1.
Example: 1010
So we have b_0 = 0, b_1 = 1, b_2 = 0, b_3 = 1.
Substituting, v = -2^3x1 + 1x0 + 2x1 + 4x0 = -8 + 2 = -6.
You can work all the other values. They'll be correct.
The formula works for positive numbers, too. In that case b_3 = 0, so the -8 term goes away.
Does it means the remainder of that number divided by 6 is 0? I'm confused.
It does mean that the remainder is 0 when you divide that number by 6, or equivalently that the number is a multiple of 6, but that doesn't tell you what it really means.
When we say that "x = y mod 6", it doesn't mean that x = (y mod 6). "mod" is not an operator applied to y and 6. For example, it is correct to say that 4 + 3 = 1 mod 6. The "mod 6" means that we are working only with the remainders of numbers after division by 6, and not really working with the integers at all.
Note that this word "mod" is different from the modulus operator used in programming languages. Even though "4+3 = 1 mod 6" in English is true, "4+3 == 1%6" in C is false. In programming languages, % is an operator.
In my CNN architecture for binary classification, I have 2 convolutional layers, 2 maxpooling layers, 2 batchnormalization operations, 1 RELu and 1 fullyconnected layer.
Case1: When the number of channels, d=1: In the first layer an input of size [28*28*d], d=1 channel is convolved with M_1=20 number of filters applied over all the input channels of size (f_h x f_w x d) = [3x 3x1] having the step size (stride) as 1 that creates a feature map of size {(h-f_h+1) x (w - f_w +1)x d x M_1} = (28-3 +1)x(28-3+1)x1x 20 = [25x 25x 20].
The second convolutional layer contains twice the number of filters = 40 of same size [3x 3 x 1]. So, the number of parameters becomes [23 * 23 * 1 * 40] as the output from the second convolutional layer. So total number of parameters = [25x 25x 20]+ [23 * 23 * 40]
Case 2: When d=2 and all other sizes are same. The filter size become [3 x 3 x 2]. The output of the first convolutional layer will contain: (28-3 +1)x(28-3+1)x2 x20 = [25x 25x 40].
For the second convolutional layer, the output will contain [23 x 23 x 2 x 40] parameters.
Question ) Is my calculation for each case above correct? What will be the input to the fully connected layer?
In a convolutional layer, the no. of channels in the filter is kept equal to the number of channels of the input. Also, the no. of channels in the filter does not affect the size of the output. The size of the output is given by :
height = (input_height - filter_height + 1) (Assuming no padding and stride = 1)
width = (input_width - filter_width + 1)
channels = no. of filters
So for your question :
Case 1 (d=1) :
Conv layer #1
Input : 28 x 28 x 1, Filter : 3 x 3 x 1, #filters : 20
So your output becomes (28-3+1) x (28-3+1) X #filters = 26 x 26 x 20
The above output becomes your input for the second convolutional layer, which leads to :
Conv layer #2
Input : 26 x 26 x 20, Filter : 3 x 3 x 20, #filters : 40 (You mentioned size of filter as 3x3x1, but it should be 3x3x20 since the no. of filter channels = no. of input channel)
Output for this layer : (26-3+1) x (26-3+1) x #filters = 24 x 24 x 40
Case 2 (d=2) :
For this case also, the output of the two conv layers will be the same as above, since the output does not depend on the number of channels
Conv layer #1
Input : 28 x 28 x 2, Filter : 3 x 3 x 2, #filters : 20
So your output becomes (28-3+1) x (28-3+1) X #filters = 26 x 26 x 20
Conv layer #2
Input : 26 x 26 x 20, Filter : 3 x 3 x 20, #filters : 40
Output for this layer : (26-3+1) x (26-3+1) x #filters = 24 x 24 x 40
Hope this helps!
There are a couple of errors. The first one is trivial and it's about the size of the output of the first layer, as it should be 26x26x20 not 25x25x20, because a kernel size of 3 without padding will make you lose just a pixel from each edge.
The more important issue in your reasoning happens in your calculation of the number of parameters of the second convolutional layer. Plain, usual convolution requires you to also consider the depth: let's assume you have a convolutional layer with m output featuremaps f_1,...,f_m and inputs of size h*w*d. Let's also assume that the convolution kernel is of spatial dimension k*k. Then a pixel x in any of the output maps, say, f_j, will be the result of a 3dĀ convolution between the input of size h*w*d and a kernel of size k*k*d. Therefore, the convolutional layer has k*k*d*m parameters. This means that in your case:
input of layer: 26x26x20
number of filters in layer: 40
number of parameters: 26x26x20x40
The same can be said about your reasoning with d=2: the second convolution will have 20x40 output maps,not 2x40.
I have a dataset of species and their rough locations in a 100 x 200 meter area. The location part of the data frame is not in a format that I find to be usable. In this 100 x 200 meter rectangle, there are two hundred 10 x 10 meter squares named A through CV. Within each 10 x 10 square there are four 5 x 5 meter squares named 1, 2, 3, and 4, respectively (1 is south of 2 and west of 3. 4 is east of 2 and north of 3). I want to let R know that A is the square with corners at (0 ,0), (10,0), (0,0), and (0,10), that B is just north of A and has corners (0,10), (0,20), (10,10), and (10,20), and K is just east of A and has corners at (10,0), (10,10), (20,0), and (20,10), and so on for all the 10 x 10 meter squares. Additionally, I want to let R know where each 5 x 5 meter square is in the 100 x 200 meter plot.
So, my data frame looks something like this
10x10 5x5 Tree Diameter
A 1 tree1 4
B 1 tree2 4
C 4 tree3 6
D 3 tree4 2
E 3 tree5 3
F 2 tree6 7
G 1 tree7 12
H 2 tree8 1
I 2 tree9 2
J 3 tree10 8
K 4 tree11 3
L 1 tree12 7
M 2 tree13 5
Eventually, I want to be able to plot the 100 x 200 meter area and have each 10 x 10 meter square show up with the number of trees, or number of species, or total biomass
What is the best way to turn the data I have into spatial data that R can use for graphing and perhaps analysis?
Here's a start.
## set up a vector of all 10x10 position tags
tags10 <- c(LETTERS,
paste0("A",LETTERS),
paste0("B",LETTERS),
paste0("C",LETTERS[1:22]))
A function to convert (e.g.) {"J",3} to the center of the corresponding sub-square.
convpos <- function(pos10,pos5) {
## convert letters to major (x,y) positions
p1 <- as.numeric(factor(pos10,levels=tags10)) ## or use match()
p1.x <- ((p1-1) %% 10) *10+5 ## %% is modulo operator
p1.y <- ((p1-1) %/% 10)*10+5 ## %/% is integer division
## sort out sub-positions
p2.x <- ifelse(pos5 <=2,2.5,7.5) ## {1,2} vs {3,4} values
p2.y <- ifelse(pos5 %%2 ==1 ,2.5,7.5) ## odd {1,3} vs even {2,4} values
c(p1.x+p2.x,p1.y+p2.y)
}
usage:
convpos("J",2)
convpos(mydata$tenbytenpos,mydata$fivebyfivepos)
Important notes:
this is a proof of concept, I can pretty much guarantee I haven't got the correspondence of x and y coordinates quite right. But you should be able to trace through this line-by-line and see what it's doing ...
it should work correctly on vectors (see second usage example above): I switched from switch to ifelse for that reason
your column names (10x10) are likely to get mangled into something like X10.10 when reading data into R: see ?data.frame and ?check.names
Similar to what #Ben Bolker has done, here's a lookup function (though you may need to transpose something to make the labels match what you describe).
tenbyten <- c(LETTERS[1:26],
paste0("A",LETTERS[1:26]),
paste0("B",LETTERS[1:26]),
paste0("C",LETTERS[1:22]))
tenbyten <- matrix(rep(tenbyten, each = 2), ncol = 10)
tenbyten <- t(apply(tenbyten, 1, function(x){rep(x, each = 2)}))
# the 1234 squares
squares <- matrix(c(rep(c(1,2),10),rep(c(4,3),10)), nrow = 20, ncol = 20)
# stick together into a reference grid
my.grid <- matrix(paste(tenbyten, squares, sep = "-"), nrow = 20, ncol = 20)
# a lookup function for the site grid
coordLookup <- function(tbt, fbf, .my.grid = my.grid){
x <- col(.my.grid) * 5 - 2.5
y <- row(.my.grid) * 5 - 2.5
marker <- .my.grid == paste(tbt, fbf, sep = "-")
list(x = x[marker], y = y[marker])
}
coordLookup("BB",2)
$x
[1] 52.5
$y
[1] 37.5
If this isn't what you're looking for, then maybe you'd prefer a SpatialPolygonsDataFrame, which has proper polygon IDs, and you attach data to, etc. In that case just Google around for how to make one from scratch, and manipulate the row() and col() functions to get your polygon corners, similar to what's given in this lookup function, which only returns centroids.
Edit: getting SPDF started:
This is modified from the function example and can hopefully be a good start:
library(sp)
# really you have a 20x20 grid, counting the small ones.
# c(2.5,2.5) specifies the distance in any direction from the cell center
grd <- GridTopology(c(1,1), c(2.5,2.5), c(20,20)))
grd <- as.SpatialPolygons.GridTopology(grd)
# get centroids
coords <- coordinates(polys)
# make SPDF, with an extra column for your grid codes, taken from the above.
# you can add further columns to this data.frame(), using polys#data
polys <- SpatialPolygonsDataFrame(grd,
data=data.frame(x=coords[,1], y=coords[,2], my.ID = as.vector(my.grid),
row.names=getSpPPolygonsIDSlots(grd)))
If I have 37 different characters and I need to create all possible words from that having max length of 15, what will be number pf total words?
Supoose, I have X=2 characters, and Y=2 max length, then possible outcomes are:
A
AA
B
BB
AB
BA
So Z=6 number of total outomces.
Now if value of X=37 and Y=15, what will be value of Z???
It's sum(37^i) for i=1..15. For your example: 2^1 + 2^2 = 6.
In Addition to Bartosz' answer, a simple formula for the sum is
n = ((1-X^(Y+1)) / (1-X)) - 1
Another simple formula for the summation is
x * (x^y - 1)
-------------
x - 1
http://www.wolframalpha.com/input/?i=sum+x%5Ei+for+i+1+to+y+