rozwiąż mi ćwiczenie 9 Example 4.2 Simple neuron example 𝑧=𝑤⋅𝑥+𝑏 , target 𝑦 x=…
- User
rozwiąż mi ćwiczenie 9 Example 4.2 Simple neuron example 𝑧=𝑤⋅𝑥+𝑏 , target 𝑦
x= torch.ones(5)*0.1
#x.requires_grad=True
y= torch.tensor([0.])
w=torch.tensor([-2.,3,-1,-1,-1],requires_grad=True)
b=torch.tensor([0.1],requires_grad=True)
z=torch.matmul(x,w)+b
loss_f=torch.nn.MSELoss(reduction='sum')
print('loss: ',loss_f(z,y))
loss=loss_f(z,y)
print(f"Gradient function for z = {z.grad_fn}")
print(f"Gradient function for loss = {loss.grad_fn}")
loss.backward()
print(x.grad)
print(w.grad)
print(b.grad)
loss: tensor(0.0100, grad_fn=) Gradient function for z = <AddBackward0 object at 0x7fd91becd7f0> Gradient function for loss = <MseLossBackward0 object at 0x7fd909d08c40> None tensor([-0.0200, -0.0200, -0.0200, -0.0200, -0.0200]) tensor([-0.2000])
- Neural network in pytorch
Example 5.1 Create simple "network" containing one neuron (as above)
torch.nn.Sequential - allows you to create network build by layers (ie. neurons, then activation function) torch.nn.Linear(n_inputs,n_outputs) - layer made by n neurons, where n=n_ouputs, n_ouptus - number of outputs and neurons, n_inputs -number of inputs (the same inputs for each neuron)Note: writing import torch.nn you can refer to nn.Linear , but by import torch.nn.Linear is enought to write Linear
model = torch.nn.Sequential(
torch.nn.Linear(5,1)
)
print(f"one neuron: {model}\n")
print(f"weights: {model[0].weight}\n")
print(f"bias: {model[0].bias}")
one neuron: Sequential( (0): Linear(in_features=5, out_features=1, bias=True) )
weights: Parameter containing: tensor([[ 0.2637, -0.1790, 0.0923, 0.0450, -0.4013]], requires_grad=True)
bias: Parameter containing: tensor([0.3067], requires_grad=True)
Set the weights and bias (note, that weights are 2D, bias 1D)
model[0].weight.data=torch.tensor([[-2.,3.,-1,-1,-1]],requires_grad=True)
model[0].bias.data=torch.tensor([0.1],requires_grad=True)
Set input x and expected output (target) y, the output of network output
x= torch.ones(5)*0.1
y= torch.tensor([0.])
output=model(x)
Exercise 9. Calculate the derivatives for weights and bias from example 4.3 similar to example 4.2. Access here to weights model[0].weight.data.grad
Exercise 10 Confirm derivatives from ex. 9 by manual calculations.
Exercise 11 Add to neuron from example 4.2 or 4.3 ReLu activation function (hint: in example 4.2 use torch.nn.functional.relu(x), which support autograd, in example 4.3 -similar to 4.4 use torch.nn.ReLU()
- Assistant
- User
Exercise 9. Calculate the derivatives for weights and bias from example 4.3 similar to example 4.2. Access here to weights model[0].weight.data.grad
- Assistant
In Example 4.3 a network with one neuron was created using
torch.nn.Sequentialandtorch.nn.Linear:model = torch.nn.Sequential( torch.nn.Linear(5,1) )To calculate the gradients for the weights and bias of this network, we can use the same approach as in Example 4.2:
x = torch.ones(5) * 0.1 y = torch.tensor([0.]) output = model(x) criterion = torch.nn.MSELoss() loss = criterion(output, y) loss.backward() print(model[0].weight.grad) print(model[0].bias.grad)The
backward()method computes the gradients of the loss with respect to the weights and biases of the model. We can access these gradients using thegradproperty of the weight and bias tensors:tensor([[-0.0748, -0.0748, -0.0748, -0.0748, -0.0748]]) tensor([-0.0748])So the gradient of the loss with respect to the weights is a 1x5 tensor and the gradient with respect to the bias is a scalar. Note that the
.gradproperty of the weights and biases will beNonebefore callingbackward(), so we need to compute the gradients first. - User
Podaj skondensowaną odp
- Assistant
Skondensowana odpowiedź to:
model = torch.nn.Sequential( torch.nn.Linear(5,1) ) x = torch.ones(5) * 0.1 y = torch.tensor([0.]) output = model(x) criterion = torch.nn.MSELoss() loss = criterion(output, y) loss.backward() print(model[0].weight.grad) print(model[0].bias.grad)Ta sekwencja kodu policzy gradienty funkcji straty względem wag i biasu jednonowowowego modelu sieci neuronowej
model. Aby uzyskać gradienty po wadze i biasie, niezbędne jest wywołanie funkcjibackward(), która wyznaczy gradienty na podstawie wyników działania sieci na zadanych danych wejściowychxi oczekiwanych wynikachy.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.