Torch: partition tensor - lua

I want to partition my dataset (10,000 50x50 RGB images) into two datasets. Something like:
X = torch.rand(10000, 3, 50, 50)
inds = torch.randperm(X:size(1))[{ { 1, nTrain } }]:long()
X_selected = X:index(1, inds)
X_remaining = X:delete(1, inds)
No matter what I google search, I just get Torch's GitHub documentation. How can I do this?

You can try this way
X = torch.rand(10000, 3, 50, 50)
inds = torch.randperm(X:size(1)):long()
train_inds = inds:narrow(1, 1, nTrain)
valid_inds = inds:narrow(1, nTrain + 1, X:size(1) - nTrain)
X_train = X:index(1, train_inds)
X_valid = X:index(1, valid_inds)

Related

Applying layer normalizations for all CNN layers

So I have to implement a model from a paper, and after explaining each layer it says:
Layer normalization was used for both the input samples and for all
convolutional layers.
My implementation is as follows:
self.sinc_convolution = sinc_conv(80, 251, 16000)
self.convolution2 = nn.Conv1d(80, 60, 5)
self.convolution3 = nn.Conv1d(60, 60, 5)
self.lr = nn.LeakyReLU(0.1)
self.fc1 = nn.Linear(60, constants.embedding_size * 2)
self.fc2 = nn.Linear(constants.embedding_size * 2, constants.embedding_size)
self.input_norm = nn.LayerNorm((1, 3200))
self.layer_norm1 = nn.LayerNorm((80, 2950))
self.layer_norm2 = nn.LayerNorm((60, 2946))
self.layer_norm3 = nn.LayerNorm((60, 2942))
But I am wondering if that's alright. How can I implement it for all layers?

Manim 'coords_to_point' new version

I am looking for something like the defunct 'coords_to_point' in the new manim version (2021).
What do you replace coords_to_point with?
from manimlib import *
class DiagramPlot(Scene):
def construct(self):
data = [20, 0, 0, -5]
x = [0, 8, 38, 39]
dot_collection = VGroup()
for time, val in enumerate(data):
dot = Dot().move_to(self.coords_to_point(x[time], val))
self.add(dot)
dot_collection.add(dot)
l1 = Line(dot_collection[0].get_center(), dot_collection[1].get_center())
l2 = Line(dot_collection[1].get_center(), dot_collection[2].get_center())
l3 = Line(dot_collection[2].get_center(), dot_collection[3].get_center())
self.add(l1, l2, l3)
I think what you are looking for is axes.c2p(*your_point) with axes being the Axis() object. the c2p method returns the point for your coordinates as numpy array.

Correct response and predictor parameters when using caret and roc()

I'm practicing logistic regression models and cross validation. I would like to output ROC curve to estimate performance, but I'm not sure which response and predictor I should use in the roc() function.
Here is an example I tried. Why are the plots different? What is my error? Thanks.
library(caret)
library(ggplot2)
library(lattice)
library(pROC)
data(mtcars)
mtcars$am = factor(ifelse(mtcars$am == 1, 'one', 'zero'))
ctrl = trainControl(method="cv",number = 5,
summaryFunction=twoClassSummary, classProbs=T,
savePredictions = T)
m=train(am ~ qsec, data = mtcars, method = "glm",
family = binomial,metric="ROC",
trControl=ctrl)
curve1 = roc(response = mtcars$am,
predictor = as.numeric(predict(m)), plot = T, legacy.axes = T, percent = T,
main = 'Test Curve1',
xlab = 'False Positive Percentage (1 - Specificity)',
ylab = 'True Positive Percentage (Sensitivity)',print.auc = T,
print.auc.x = 100, print.auc.y = 100, col = '#20B2AA', lwd = 4)
curve2 = roc(response = m$pred$obs,
predictor = as.numeric(m$pred$pred), plot = T, legacy.axes = T, percent = T,
main = 'Test Curve2',
xlab = 'False Positive Percentage (1 - Specificity)',
ylab = 'True Positive Percentage (Sensitivity)',print.auc = T,
print.auc.x = 100, print.auc.y = 100, col = '#20B2AA', lwd = 4)

What happens to this example of a resample

If i do this
X_train1 = resample(X_train, n_samples = 40, random_state = 1)
Y_train1 = resample(Y_train, n_samples = 40, random_state = 1)
will it be taking the exact same n_samples because they are both set to random_state = 1?
No, this cannot be guaranteed.
In your case, you can use this:
X_train1, y_train1 = resample(X_train, y_train, n_samples = 40, random_seed=1)
random_seed fixed to reproduce code on another machine or after a kernel restart.

All input arrays and target arrays must have the same number of samples."- Training on single image to check if the model works in keras

def obcandidate(inputvgg,outputmodel):
graph = Graph()
graph.add_input(name = 'input1', input_shape = (512, 14, 14))
graph.add_node(Convolution2D(512, 1, 1), name = 'conv11', input = 'input1')
graph.add_node(Convolution2D(512, 14, 14), name = 'conv112', input = 'conv11')
graph.add_node(Flatten(), name = 'flatten11', input = 'conv112')
graph.add_node(Dense(3136), name = 'dense1', input = 'flatten11')
graph.add_node((Activation('relu')), name = 'relu', input = 'dense1')
graph.add_node(Reshape((56,56)), name = 'reshape', input = 'relu')
sgd = SGD(lr = 0.001, decay = .00005, momentum = 0.9, nesterov = True)
graph.add_output(name = 'output1', input = 'reshape')
graph.compile(optimizer = sgd, loss = {
'output1': 'binary_crossentropy'})
print 'compile success'
history = graph.fit({'input1':inputvgg, 'output1':outputmodel}, nb_epoch=1)
predictions = graph.predict({'input1':inputvgg})
return graph
""
"main function"
""
if __name__ == "__main__":
model = VGG_16('vgg16_weights.h5')
sgdvgg = SGD(lr = 0.1, decay = 1e-6, momentum = 0.9, nesterov = True)
model.compile(optimizer = sgdvgg, loss = 'categorical_crossentropy')
finaloutputmodel = outputofconvlayer(model)
finaloutputmodel.compile(optimizer = sgdvgg, loss = 'categorical_crossentropy')
img = cv2.resize(cv2.imread('000032.jpg'), (224, 224))
mean_pixel = [103.939, 116.779, 123.68]
img = img.astype(np.float32, copy = False)
for c in range(3):
img[: , : , c] = img[: , : , c] - mean_pixel[c]
img = img.transpose((2, 0, 1))
img = np.expand_dims(img, axis = 0)
imgout = np.asarray(cv2.resize(cv2.imread('000032seg.png',0), (56, 56)))
imgout[imgout!=0]=1
out=imgout
inputvgg = np.asarray(finaloutputmodel.predict(img))
obcandidate(inputvgg,out)
Hi ,above is my code where i am trying to segment object candidate through graph model,
i want to check for one input if the code works or not so i am giving it one input image and the output image,
But keras gives me an error - "All input arrays and target arrays must have the same number of samples."
Can anyone tell me what do i do to see if my model runs .i am training on one input so that i can verify that my model is correct and start training ,is there any other way to do it?
In the part where you do this - history = graph.fit({'input1':inputvgg, 'output1':outputmodel}, nb_epoch=1) inputvgg and outputmodel should have same number of dimensions.

Resources