I am trying to solve the supervsied learning problem (housing price) using pytorch and I encounter CUDA error?

M

Manish Kumar Singh

Guest
Manish Kumar Singh Asks: I am trying to solve the supervsied learning problem (housing price) using pytorch and I encounter CUDA error?
I am getting the following error:

Code:
    ---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_17/1714279870.py in <module>
      4 
      5 
----> 6 data_tensor = CustomData(data)
      7 model = NeuralNetwork().to(device)
      8 train_data_tensor = DataLoader(data_tensor,batch_size,shuffle=True)

/tmp/ipykernel_17/3667328859.py in __init__(self, data, transform, target_transform)
     33         #self.data = data
     34         self.label = data[:,-1]
---> 35         self.label = self.label.to(device)
     36 
     37         self.feature = data[:,:-1]

RuntimeError: CUDA error: device-side assert triggered
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

My code is

Code:
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
from pandas import DataFrame
from torch.utils.data import Dataset
import torch

import torch.nn as nn
from torch.utils.data import DataLoader
import torchvision
from torchvision import datasets, transforms
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

Here I try fill gaps in the data and also convert text data (like good, and bad) into numbers and finally convert the entire thing into a tensor. So far no error.

Code:
data = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')
data_cat = data[[str(i) for i in data.columns if data[str(i)].dtype == object]]
data_cat = data_cat.rank(axis=0, method='dense', na_option='top', ascending=False).astype(float)#converting text data to numbers
data_cat.fillna(0)
data_num = data[[str(i) for i in data.columns if data[str(i)].dtype != object]]
data_val = data_num.iloc[:,-1]
data_val = data_val.fillna(0)
data_num = data_num.iloc[:,:-1]
data_num = (data_num - data_num.mean())/(data_num.std())
data_num = data_num.fillna(0)
data = pd.concat([data_cat,data_num,data_val],axis=1)
data = torch.from_numpy(data.to_numpy())

Now I create a dataset to be used in NeuralNetwork via Dataloader. Again so far so good.

Code:
class CustomDataset(Dataset):
    def __init__(self,feature, label, transform=None, target_transform=None):
        self.feature = feature
        self.label = label
        self.transform = transform
        self.target_transform = target_transform
    def __len__(self):
        return len(self.label)
    def __getitem__(self, idx):
        feature = self.feature[idx]
        label = self.label[idx]
        if self.transform:
            feature = self.transform(feature)
        if self.target_transform:
            label = self.target_transform(label)
        return feature, label      
data_tensor = CustomDataset(data[:,:-1],data[:,-1])

Since I will be using GPU, I define the device.

Code:
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")

This is my nn model. Because there are categorical data, I am using an embedding layer first.

Code:
class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        
        self.linear_relu_stack = nn.Sequential(
            nn.Embedding(79,10),
            nn.Linear(70, 10),
            nn.ReLU(),
            nn.Linear(10, 10),
            nn.ReLU(),
            nn.Linear(10, 10),
            nn.ReLU(),            
            nn.Linear(10, 1),
        )
    def forward(self, x):
        logits = self.linear_relu_stack(x)
        return logits

These are my hyperprameters.

Code:
learning_rate = 0.005
batch_size = 20
num_of_epoch = 50

Now I am setting up the training process. I suspect this is where the bug lies.

Code:
model = NeuralNetwork().to(device)
train_data_tensor = DataLoader(data_tensor,batch_size,shuffle=True)
loss_fn = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
def train_loop(data_loader, model, loss_fn, optimizer):
    size = len(data_loader.dataset)
    for batch, (X, y) in enumerate(data_loader):
        # Compute prediction and loss
        pred = model(X)
        loss = loss_fn(pred, y)
        # Backpropagation
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()        
        if batch % 100 == 0:
            loss, current = loss.item(), batch * len(X)
            print(f"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]")

#Actual training
for t in range(num_of_epoch):
    print(f"Epoch {t+1}\n-------------------------------")
    train_loop(train_data_tensor, model, loss_fn, optimizer)
    
print("Done")

Can someone help me rectify errors in code?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.
 

Unreplied Threads

[Solved] Extract Data from Corrupted TerraSync file, Pathfinder can't correct & won't open

  • Kevin Nolan
  • Geography
  • Replies: 0
Kevin Nolan Asks: Extract Data from Corrupted TerraSync file, Pathfinder can't correct & won't open
I had an employee recording points for a project with a Trimble Geo XT 7 series receiver attached to a Zephyr 2 external antenna. The receiver went to sleep between collecting episodes. Upon attempting to re-open, the file came back as corrupted and TerraSync was unable to fix and open the file. I have never had an unrecoverable corruption and didn't expect this to be a permanent problem. We stared a new file and finished collecting data.

However, now Pathfinder cannot fix and refuses to open the corrupted file. I copied the corrupted file parts to a separate folder manually (Pathfinder won't transfer file). I can open the individual files that show up in Windows Explorer (*.dd, *.gic, *.gip, *.gis, *giw, *.gix, *.obs, *.obx) in various programs (Notepad, Word, Excel), but only user-entered data (comments, notes, text fields from data dictionary form) show up as readable text. The rest is various combinations of numbers, letters, and symbols. I need to extract the coordinate data from this file, even if I can't postprocess and correct it.

Is there any way to translate the data displayed as symbols into text and numbers so that I can see the coordinates?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, solveforum.

[Solved] How to snap lines from the same layer together?

Geowhat Asks: How to snap lines from the same layer together?
I did a Network Analysis and this is the outcome. How can I snap lines from the same layer together?

enter image description here

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, solveforum.

[Solved] Publish a Query Layer as a Feature Access Service - ArcGIS Server

a1234 Asks: Publish a Query Layer as a Feature Access Service - ArcGIS Server
I am connecting to a SQL Server 2016 database, and adding data into ArcMap 10.3.1 as a Query Layer.

I want to publish this data as a Feature Access service to ArcGIS Server 10.3.1. I have no problems publishing this data as a mapping service. However, when I want to enable Feature Access, I get 00223: Query layer has virtual column error.

enter image description here

I have read the documentation on this error, and the only solution is to "Change the query definition of your layer so it no longer contains a virtual column or columns".

I have tried several different ways to alter the sql query (very simple queries), and all still produce this same error.

What is a virtual column? How can I publish a Query Layer as a Feature Access service?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, solveforum.

Doubt about Electromagnetic Attraction b/w Electromagnet-Iron vs Electromagnet-Electromagnet

  • J.Doe
  • Physics
  • Replies: 0
J.Doe Asks: Doubt about Electromagnetic Attraction b/w Electromagnet-Iron vs Electromagnet-Electromagnet
https://www.supermagnete.de/eng/faq...the-combination-magnet-magnet-and-magnet-iron

At full contact, the attractive force between a raw magnet and an iron plate is the same as the attractive force between two raw magnets. However, with increasing distance, the attraction diminishes faster than the attraction between two raw magnets (see graph)

I am confused by this:


  1. Consider two identical Electromagnets touching each other in one case and a single electromagnet touching an iron plate in second case. Won't the attraction be more in the first case as both magnets are exerting a pull on each other where as in second case the magnet is pulling on a passive iron plate (which has temporarily induced magnetism)?


  2. For two cylindrical electromagnets with poles at distance $x$ repelling each other (they are relatively close by i.e. $10*R > x$ where $R$=radius of pole) how does the force exerted on each electromagnet change with increase in current ($I$)? Can someone help with this formula?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Closing a Graph

sam wolfe Asks: Closing a Graph
Consider the following graph g

enter image description here

I want to "close" it by adding the following edges

enter image description here

Alternatively, I would also be happy with something like

enter image description here

where I've added four "corner" vertices.

Now, this question is, in a way, a follow up to this answer, where I initially have a picture on which I draw a graph, as follows

enter image description here

Is it possible to turn the picture into a rectangle-shaped graph and then join them in somehow? This would lead to something like the second example.

Alternatively, I also thought about simply uniting 1-degree vertices that are close to each other, creating the first example graph.

In the end, I want a graph that defines a mesh, thus the need of "closing" the graph in this fashion. Ideally, I want KVertexConnectedGraphQ[g] = True for the closed graph.

Any ideas?

Edit 1: Note that my goal is to be able to do this for general graphs. For example, considering the graph

enter image description here

I want to get something like

enter image description here

I guess using something like ConvexHull (which doesn't correspond to what is drawn) in some matter could help my goal, but at this point I'm entirely sure how.

Edit 2: In order to be more practical consider the graph given by

Code:
g = Graph[{1 \[UndirectedEdge] 10, 2 \[UndirectedEdge] 9, 
   3 \[UndirectedEdge] 9, 4 \[UndirectedEdge] 12, 
   5 \[UndirectedEdge] 8, 6 \[UndirectedEdge] 13, 
   7 \[UndirectedEdge] 14, 8 \[UndirectedEdge] 11, 
   8 \[UndirectedEdge] 17, 9 \[UndirectedEdge] 20, 
   10 \[UndirectedEdge] 11, 10 \[UndirectedEdge] 21, 
   11 \[UndirectedEdge] 25, 12 \[UndirectedEdge] 18, 
   12 \[UndirectedEdge] 19, 13 \[UndirectedEdge] 18, 
   13 \[UndirectedEdge] 27, 14 \[UndirectedEdge] 15, 
   14 \[UndirectedEdge] 19, 16 \[UndirectedEdge] 17, 
   17 \[UndirectedEdge] 23, 18 \[UndirectedEdge] 24, 
   19 \[UndirectedEdge] 22, 20 \[UndirectedEdge] 21, 
   20 \[UndirectedEdge] 27, 21 \[UndirectedEdge] 34, 
   22 \[UndirectedEdge] 26, 22 \[UndirectedEdge] 29, 
   23 \[UndirectedEdge] 31, 23 \[UndirectedEdge] 33, 
   24 \[UndirectedEdge] 29, 24 \[UndirectedEdge] 30, 
   25 \[UndirectedEdge] 31, 25 \[UndirectedEdge] 34, 
   27 \[UndirectedEdge] 28, 28 \[UndirectedEdge] 32, 
   28 \[UndirectedEdge] 38, 29 \[UndirectedEdge] 37, 
   30 \[UndirectedEdge] 32, 30 \[UndirectedEdge] 35, 
   31 \[UndirectedEdge] 39, 32 \[UndirectedEdge] 41, 
   34 \[UndirectedEdge] 36, 35 \[UndirectedEdge] 42, 
   35 \[UndirectedEdge] 44, 36 \[UndirectedEdge] 45, 
   36 \[UndirectedEdge] 54, 37 \[UndirectedEdge] 40, 
   37 \[UndirectedEdge] 44, 38 \[UndirectedEdge] 45, 
   38 \[UndirectedEdge] 48, 39 \[UndirectedEdge] 47, 
   39 \[UndirectedEdge] 53, 41 \[UndirectedEdge] 42, 
   41 \[UndirectedEdge] 48, 42 \[UndirectedEdge] 56, 
   43 \[UndirectedEdge] 46, 44 \[UndirectedEdge] 51, 
   45 \[UndirectedEdge] 55, 46 \[UndirectedEdge] 47, 
   46 \[UndirectedEdge] 52, 47 \[UndirectedEdge] 49, 
   48 \[UndirectedEdge] 50},
  VertexCoordinates -> {{102.5`, 175.5`}, {84.5`, 152.5`}, {108.5`, 
     175.5`}, {133.5`, 153.5`}, {152.5`, 175.5`}, {244.5`, 
     175.5`}, {254.5`, 148.5`}, {43.5`, 174.5`}, {43.5`, 
     170.5`}, {196.5`, 174.5`}, {202.5`, 147.5`}, {297.5`, 
     174.5`}, {309.5`, 147.5`}, {63.5`, 148.5`}, {10.5`, 
     141.5`}, {143.5`, 117.5`}, {119.5`, 109.5`}, {67.5`, 
     94.5`}, {236.5`, 131.5`}, {293.5`, 127.5`}, {180.5`, 
     89.5`}, {312.5`, 146.5`}, {4.5`, 143.5`}, {18.5`, 
     97.5`}, {253.5`, 95.5`}, {301.5`, 98.5`}, {110.5`, 
     75.5`}, {313.5`, 93.5`}, {286.5`, 83.5`}, {52.5`, 80.5`}, {4.5`, 
     76.5`}, {236.5`, 82.5`}, {181.5`, 86.5`}, {187.5`, 
     80.5`}, {168.5`, 31.5`}, {297.5`, 37.5`}, {244.5`, 
     49.5`}, {59.5`, 29.5`}, {216.5`, 27.5`}, {125.5`, 
     38.5`}, {225.5`, 26.5`}, {280.5`, 23.5`}, {152.5`, 
     20.5`}, {110.5`, 3.5`}, {313.5`, 29.5`}, {199.5`, 7.5`}, {32.5`, 
     7.5`}, {85.5`, 3.5`}, {236.5`, 3.5`}, {4.5`, 25.5`}, {10.5`, 
     16.5`}, {281.5`, 4.5`}, {155.5`, 3.5`}, {4.5`, 3.5`}, {34.5`, 
     4.5`}, {199.5`, 4.5`}},
  VertexSize -> 3 {1, 1}, VertexStyle -> Red, 
  EdgeStyle -> Directive[Black]]

which yields the first graph g. Then, the code

Code:
hm = ConvexHullMesh[
      Transpose[
Select[{GraphEmbedding[g], VertexDegree[g]} // 
      Transpose, #[[2]] == 1 &]][[1]]]
gb = Graph[hm["Edges"], VertexCoordinates -> MeshCoordinates[hm], 
  VertexSize -> 3 {1, 1}, VertexStyle -> Red, 
  EdgeStyle -> Directive[Black]]

yields

enter image description here

Now, how do I merge both graphs? I tried GraphUnion, but I would need the correct VertexCoordinates. Could it be simply an ordering problem? Any suggestion?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Showing an inequality in vector space.

Hamdiken Asks: Showing an inequality in vector space.
Let $E$ be a normed vector space. We define a linear form $T:E\rightarrow \mathbb R$. Denote $H=Ker(T)$.

  • Show that if $T$ is continuous, then $H$ is a closed set.

$H$ is the continuous inverse of a closed set ($\{0\}$), hence closed.

  • Suppose that $H$ is closed. Let $a\in E$ such that $T(a)=1$. Show that the set $a+H$ is closed and does not contain $0$.

My guess is that since the set $\{a\}$ is bounded, and since $H$ is closed in a vector space, which means it's also bounded, gives the result that $a+H$ is closed. As for the $0$, we have for every $y\in a+H$, $T(y)=T(a+x)=T(a)+T(x)=1$. However $T(0)=0\neq1$, so $0\notin a+H$.

  • Deduce the existence of $r>0$ such that $B(0,r)\cap(a+H)=\emptyset$.

Since $0\notin a+H$, there exists a neighbourhood $V_0$ of $0$ such that $V_0\cap(a+H)=\emptyset$, hence the existence of an open ball $B(0,r)$ such that $B(0,r)\cap(a+H)=\emptyset$.

  • Show that for every $x\in B(0,r)$, we have $$|Tx|\leq 1$$

This is where I stopped. I thought of the orthogonal projection but didn't understand how to proceed.

Any help and correction of the answers above will be highly appreciated.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Monotone likelihood ratio for logistic distribution $f(x;\theta) = e^{-x - \theta} (1 + e^{-x - \theta})^{-2}$

Balkys Asks: Monotone likelihood ratio for logistic distribution $f(x;\theta) = e^{-x - \theta} (1 + e^{-x - \theta})^{-2}$
This is a question from Problems 9.4 of An Introduction to Probability and Statistics by Rohatgi.

Let $X$ have logistic distribution with the PDF $$f(x;\theta) = e^{-x - \theta} (1 + e^{-x - \theta})^{-2}, x \in \mathbb{R}.$$ Does ${f}$ have a monotone likelihood ratio?

I have tried to look at this related question, but the theory was beyond what I have learned.

My attempt: Let $\theta_1, \theta_0 \in \mathbb{R}$. Let $\theta_1 > \theta_0.$

Consider the likelihood ratio, $T_{NP}(\mathbf{x}) = \dfrac{f_n(\mathbf{x};\theta_1)}{f_n(\mathbf{x};\theta_0)} = \dfrac{ e^{-n \bar{x} - n \theta_1} \prod (1+e^{-x_i - \theta_1})^{-2} }{ e^{ -n \bar{x} - n \theta_0} \prod (1+e^{-x_i - \theta_0})^{-2} }.$

After simplifying more, I get $T_{NP}(\mathbf{x}) = e^{n(\theta_0 - \theta_1)} \prod \left(\dfrac{1+e^{-x_i - \theta_0}}{1+e^{-x_i - \theta_1}}\right)^2.$

I found that $\dfrac{d}{dx} \dfrac{1+e^{-x - \theta_0}}{1+e^{-x - \theta_1}}$ was negative for all $x$.

I tried to relate this to what I had done so far. I used that $\log$ is monotone increasing. Taking the log of the likelihood ratio, I get that the log-likelihood ratio is decreasing in $\sum \log \left(\dfrac{1+e^{-x_i - \theta_0}}{1+e^{-x_i - \theta_1}}\right).$ However, I am not sure how to get the required test statistic that is independent of the parameter $\theta$.

I think that a relevant result is that for a distribution from the exponential family, $f(x;\theta) = c(\theta)h(x)\exp(\pi(\theta)T(x))$, the likelihood ratio is monotone in $T(x)$.

I tried to use this by writing $f(x;\theta) = \exp{(-x-\theta)} \exp{(-2\log(1+e^{-x-\theta}))}.$ But I cannot identify $T(x)$ from this form.

Could someone please help me? Thank you very much.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.