RuntimeError: Too many open files when I use pytorch dataloader. (Not opening any files) - neural-network

I am trying to build and train a neural network using more low level pytorch.
I have the following simple toy code that defines and trains a 6-layer fully connected neural network. The dataset is from a simple sinusoidal function.
# initialise network weights
W1 = torch.randn((1, 30), requires_grad=True)
W2 = torch.randn((30, 30), requires_grad=True)
W3 = torch.randn((30, 30), requires_grad=True)
W4 = torch.randn((30, 30), requires_grad=True)
W5 = torch.randn((30, 30), requires_grad=True)
W6 = torch.randn((30, 1), requires_grad=True)
B1 = torch.randn((30), requires_grad=True)
B2 = torch.randn((30), requires_grad=True)
B3 = torch.randn((30), requires_grad=True)
B4 = torch.randn((30), requires_grad=True)
B5 = torch.randn((30), requires_grad=True)
B6 = torch.randn((1), requires_grad=True)
def Neural_net(x, W1, W2, W3, W4, W5 , W6, B1, B2, B3, B4, B5, B6):
# calculate hidden and output layers
h1 = torch.tanh((x # W1) + B1)
h2 = torch.tanh((h1 # W2) + B2)
h3 = torch.tanh((h2 # W3) + B3)
h4 = torch.tanh((h3 # W4) + B4)
h5 = torch.tanh((h4 # W5) + B5)
output = (h5 # W6) + B6
return output
# generating dataset
features = torch.linspace(1,20,50)
features = features.view(len(features),1)
labels = torch.sin(0.5*features)
# Creating Dataloader
data_size = np.shape(features)[0]
data_set = torch.FloatTensor(features)
labels = torch.FloatTensor(labels)
dataset = TensorDataset(data_set, labels)
num_batches = 10
dataloader = torch.utils.data.DataLoader(dataset, batch_size=data_size//num_batches,
shuffle=True, num_workers=2, drop_last=False)
# training
num_epochs = 10000
criterion = torch.nn.MSELoss()
h = 0.01/num_batches
for epoch in range(num_epochs):
for i, data in enumerate(dataloader):
x = data[0]
y = data[1]
dL1 = 0
dL2 = 0
dL3 = 0
dL4 = 0
dL5 = 0
dL6 = 0
dLb1 = 0
dLb2 = 0
dLb3 = 0
dLb4 = 0
dLb5 = 0
dLb6 = 0
# forward
outputs = Neural_net(x, W1, W2, W3, W4, W5,W6, B1, B2, B3, B4, B5, B6)
loss = criterion(outputs, y)
# backward
dL1 = torch.autograd.grad(loss,W1,create_graph=True)[0]
dL2 = torch.autograd.grad(loss,W2,create_graph=True)[0]
dL3 = torch.autograd.grad(loss,W3,create_graph=True)[0]
dL4 = torch.autograd.grad(loss,W4,create_graph=True)[0]
dL5 = torch.autograd.grad(loss,W5,create_graph=True)[0]
dL6 = torch.autograd.grad(loss,W6,create_graph=True)[0]
dLb1 = torch.autograd.grad(loss,B1,create_graph=True)[0]
dLb2 = torch.autograd.grad(loss,B2,create_graph=True)[0]
dLb3 = torch.autograd.grad(loss,B3,create_graph=True)[0]
dLb4 = torch.autograd.grad(loss,B4,create_graph=True)[0]
dLb5 = torch.autograd.grad(loss,B5,create_graph=True)[0]
dLb6 = torch.autograd.grad(loss,B6,create_graph=True)[0]
# optimise
W1 = W1 - h * dL1
W2 = W2 - h * dL2
W3 = W3 - h * dL3
W4 = W4 - h * dL4
W5 = W5 - h * dL5
W6 = W6 - h * dL6
#
B1 = B1 - h * dLb1
B2 = B2 - h * dLb2
B3 = B3 - h * dLb3
B4 = B4 - h * dLb4
B5 = B5 - h * dLb5
B6 = B6 - h * dLb6
if epoch%10 == 0:
print('epoch = ',epoch,'loss = ', loss)
The problem is, that while the code seems to work at first, training the neural network for a number of epochs, the code stops with the following Runtime error after some time:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [24], in <cell line: 5>()
3 h = 0.01/num_batches
5 for epoch in range(num_epochs): # loop over the dataset multiple times
----> 7 for i, data in enumerate(dataloader):#, 0):
8 x = data[0]
9 y = data[1]
File ~/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py:517, in _BaseDataLoaderIter.__next__(self)
515 if self._sampler_iter is None:
516 self._reset()
--> 517 data = self._next_data()
518 self._num_yielded += 1
519 if self._dataset_kind == _DatasetKind.Iterable and \
520 self._IterableDataset_len_called is not None and \
521 self._num_yielded > self._IterableDataset_len_called:
File ~/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py:1182, in _MultiProcessingDataLoaderIter._next_data(self)
1179 return self._process_data(data)
1181 assert not self._shutdown and self._tasks_outstanding > 0
-> 1182 idx, data = self._get_data()
1183 self._tasks_outstanding -= 1
1184 if self._dataset_kind == _DatasetKind.Iterable:
1185 # Check for _IterableDatasetStopIteration
File ~/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py:1148, in _MultiProcessingDataLoaderIter._get_data(self)
1144 # In this case, `self._data_queue` is a `queue.Queue`,. But we don't
1145 # need to call `.task_done()` because we don't use `.join()`.
1146 else:
1147 while True:
-> 1148 success, data = self._try_get_data()
1149 if success:
1150 return data
File ~/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py:1013, in _MultiProcessingDataLoaderIter._try_get_data(self, timeout)
1011 except OSError as e:
1012 if e.errno == errno.EMFILE:
-> 1013 raise RuntimeError(
1014 "Too many open files. Communication with the"
1015 " workers is no longer possible. Please increase the"
1016 " limit using `ulimit -n` in the shell or change the"
1017 " sharing strategy by calling"
1018 " `torch.multiprocessing.set_sharing_strategy('file_system')`"
1019 " at the beginning of your code") from None
1020 raise
RuntimeError: Too many open files. Communication with the workers is no longer possible. Please increase the limit using `ulimit -n` in the shell or change the sharing strategy by calling `torch.multiprocessing.set_sharing_strategy('file_system')` at the beginning of your code
Any help would be very appreciated.

Related

artificial neural network in octave

I'm having trouble on an easy exercise about an artificial neural network with 2 features, a hidden layer of 5 neurons and two possible outputs (0 or 1).
My X matrix is a 51x2 matrix, and y is a 51x1 vector.
I know I'm not supposed to do the while E>1 but I wanted to see if eventually my error would be lower than 1
I'd like to know what I am doing wrong. My error doesn't seem to lower (around 1.5 no matter how much iterations I'm doing). Do you see in the code where I am doing a mistake? I'm supposed to use gradient descent.
function [E, v,w] = costFunction(X, y,alpha1,alpha2)
[m n] = size(X);
E = 1;
v = 2*rand(5,3)-1;
w = 2*rand(2,6)-1;
grad_v=zeros(size(v));
grad_w=zeros(size(w));
K = 2;
E = 2;
while E> 1
a1 = [ones(m,1) X];
z2 = a1 * v';
a2 = sigmoid(z2);
a2 = [ones(size(a2,1),1),a2];
z3 = a2 * w';
h = sigmoid(z3);
cost = sum((-y.*log(h)) - ((1-y).*log(1-h)),2);
E = (1/m)*sum(cost);
Delta1=0;
Delta2=0;
for t = 1:m
a1 = [1;X(t,:)'];
z2 = v * a1;
a2 = sigmoid(z2);
a2 = [1;a2];
z3 = w * a2;
a3 = sigmoid(z3);
d3 = a3 - y(t,:)';
d2 = (w(:,2:end)'*d3).*sigmoidGradient(z2);
Delta2 += (d3*a2');
Delta1 += (d2*a1');
end
grad_v = (1/m) * Delta1;
grad_w = (1/m) * Delta2;
v -= alpha1 * grad_v;
w -= alpha2 * grad_w;
end
end

Forward Propagation with Dropout

I am working through Andrew Ng new deep learning Coursera course.
We are implementing the following code :
def forward_propagation_with_dropout(X, parameters, keep_prob = 0.5):
np.random.seed(1)
# retrieve parameters
W1 = parameters["W1"]
b1 = parameters["b1"]
W2 = parameters["W2"]
b2 = parameters["b2"]
W3 = parameters["W3"]
b3 = parameters["b3"]
# LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID
Z1 = np.dot(W1, X) + b1
A1 = relu(Z1)
### START CODE HERE ### (approx. 4 lines) # Steps 1-4 below correspond to the Steps 1-4 described above.
D1 = np.random.rand(*A1.shape) # Step 1: initialize matrix D1 = np.random.rand(..., ...)
D1 = (D1 < 0.5) # Step 2: convert entries of D1 to 0 or 1 (using keep_prob as the threshold)
A1 = A1*D1 # Step 3: shut down some neurons of A1
A1 = A1 / keep_prob # Step 4: scale the value of neurons that haven't been shut down
### END CODE HERE ###
Z2 = np.dot(W2, A1) + b2
A2 = relu(Z2)
### START CODE HERE ### (approx. 4 lines)
D2 =np.random.rand(*A2.shape) # Step 1: initialize matrix D2 = np.random.rand(..., ...)
D2 = (D2 < 0.5) # Step 2: convert entries of D2 to 0 or 1 (using keep_prob as the threshold)
A2 = A2 * D2 # Step 3: shut down some neurons of A2
A2 = A2 / keep_prob # Step 4: scale the value of neurons that haven't been shut down
### END CODE HERE ###
Z3 = np.dot(W3, A2) + b3
A3 = sigmoid(Z3)
cache = (Z1, D1, A1, W1, b1, Z2, D2, A2, W2, b2, Z3, A3, W3, b3)
return A3, cache
Calling:
X_assess, parameters = forward_propagation_with_dropout_test_case()
A3, cache = forward_propagation_with_dropout(X_assess, parameters, keep_prob = 0.7)
print ("A3 = " + str(A3))
My output was :
A3 = [[ 0.36974721 0.49683389 0.04565099 0.49683389 0.36974721]]
The expected output should be :
A3 = [[ 0.36974721 0.00305176 0.04565099 0.49683389 0.36974721]]
Only one number difference. Any ideas why ?
I think it is because of the way I shaped D1 and D2.
I think it is because you put D1 = (D1 < 0.5) and D2 = (D2 < 0.5)
You need to put "keep_prob" instead of 0.5

how to find intersection points between 2 curves

I have been trying to find intersection between 2 curves, my formulas are long, and I do not know what how to find the value at x axis
Here is my code:
Dr = (0:0.001:7);
a1 = (2*Dr + t)/t;
b1 = (h - 1.45*Dr)/t;
c1 = (h - k4*Dr)/t;
d1 = a1.*b1;
e1 = d1.*c1;
Fs1 = Dr.*((4000*E/(1 - v^2))*(t^3 / Do^2)*(k2/k1)*(1 + 0.153*e1));
Fs1 = double(Fs1);
a2 = ((Dr-dss)*ks - 0.1*t)/t;
b2 = (h - 2.2*(Dr-dss)*ks)/t;
c2 = (h - K4*(Dr-dss)*ks)/t;
d2 = a2.*b2;
e2 = d2.*c2;
f = (Dr -dss)*ks;
Fb2 = f.*((250*E/(1 - v^2))*(t^3 / (k1*Do^2))*(1 + 0.133*e2));
n = ks*Fb2;
n = double(n);
n(Dr<3.47)=0;
plot (Dr,n,Dr,Fs1)
ylim([0 600]);
grid on;

Interpolating data between two known functions matlab

I have two lines y1 = -a1*x1 + c1 for theta =30 and y1 = -a2*x1 + c2 for theta = 45
is it possible to interpolate a equation for y1 for theta between 30 and 45 in matlab ? The lines are almost parallel to each other. Anyone has an easy way of doing this?
You can interpolate the coeff a and c:
w = (theta - 30) / (45 - 30 ); % w = 0 for theta = 30 and w = 1 for theta = 45
aTheta = a2 * w + a1 * ( 1 - w );
cTheat = c2 * w + c1 * ( 1 - w );
yTheta = -aTheta * x + cTheta * y;
x = 1:10;
a30 = 1;
a45 = 1.1;
c30 = 0;
c45 = 3;
y30 = -a1*x + c1;
y45 = -a2*x + c2;
Now to find y40 we can just interpolate the curve parameters (i.e. slope (a) and offset (c))
a40 = interp1([30,45], [a30, a45], 40);
c40 = interp1([30,45], [c30, c45], 40);
And now our interpolated y40 is
y40 = -a40*x + c40;
plot(x,y30,x,y45,x,y40);

matlab: large system of coupled nonlinear equations

I'm trying to solve the a very large system of coupled nonlinear equations. Following this thread and the related help by Matalb (first example) I tried to wrote the following code:
%% FSOLVE TEST #2
clc; clear; close all
%%
global a0 a1 a2 a3 a4 h0 TM JA JB
a0 = 2.0377638272727268;
a1 = -7.105521894545453;
a2 = 9.234000147272726;
a3 = -5.302489919999999;
a4 = 1.1362478399999998;
h0 = 45.5;
TM = 0.00592256;
JA = 1.0253896074561006;
JB = 1.3079437258774012;
%%
global N
N = 5;
XA = 0;
XB = 15;
dX = (XB-XA)/(N-1);
XX = XA:dX:XB;
y0 = JA:(JB-JA)/(N-1):JB;
plot(XX,y0,'o')
[x,fval] = fsolve(#nlsys,y0);
where the function nlsys is as follows:
function S = nlsys(x)
global a1 a2 a3 a4 N TM h0 dX JA JB
H = h0^2/12;
e = cell(N,1);
for i = 2:N-1
D1 = (x(i+1) - x(i-1))./2./dX;
D2 = (x(i+1) + x(i-1) - 2.*x(i))./(dX^2);
f = a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3;
g = - H.* (a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3)./(x(i).^5);
b = (H/2) .* (5*a1 + 8*a2.*x(i) + 9*a3.*x(i).^2 + 8*a4.*x(i).^3)./(x(i).^6);
e{i} = #(x) f + b.*(D1.^2) + g.*D2 - TM;
end
e{1} = #(x) x(1) - JA;
e{N} = #(x) x(N) - JB;
S = #(x) cellfun(#(E) E(x), e);
When I run the program, Matlab gives the following errors:
Error using fsolve (line 280)
FSOLVE requires all values returned by user functions to be of data type double.
Error in fsolve_test2 (line 32)
[x,fval] = fsolve(#nlsys,y0);
Where are my mistakes?
Thanks in advance.
Petrus