[英]Octave Treats a 5000*10 Matrix As a 16*4 Matrix
I'm trying to use Octave to submit an assignment written in MATLAB.我正在尝试使用 Octave 提交用 MATLAB 编写的作业。 h_theta2
matrix is a 5000*10 matrix in MATLAB (please see the attached screenshot) and the code works fine in MATLAB. h_theta2
矩阵是 MATLAB 中的 5000*10 矩阵(请参阅随附的屏幕截图),代码在 MATLAB 中运行良好。 But when I try to submit the assignment in Octave it returns the following error:但是当我尝试在 Octave 中提交作业时,它会返回以下错误:
Submission failed: operator -: nonconformant arguments (op1 is 16x4, op2 is 5000x10)提交失败:operator -: nonconformant arguments (op1 is 16x4, op2 is 5000x10)
LineNumber: 98 (Which refers to delta3=h_theta2-y_2
in the attached screenshot.) LineNumber:98(指的是附加屏幕截图中的delta3=h_theta2-y_2
。)
This (I'm guessing) means that Octave is treating h_theta2
as a 16*4 matrix.这(我猜)意味着 Octave 将h_theta2
视为 16*4 矩阵。
The code is supposed to estimate the cost function and gradient of a neural network.该代码应该估计神经网络的成本 function 和梯度。 X, y, Theta1 and Theta2 are given in the assignment. X、y、Theta1 和 Theta2 在作业中给出。
function [J grad] = nnCostFunction(nn_params, ...
input_layer_size, ...
hidden_layer_size, ...
num_labels, ...
X, y, lambda)
NNCOSTFUNCTION Implements the neural network cost function for a two-layer neural network which performs classification. NNCOSTFUNCTION 为执行分类的两层神经网络实现神经网络成本 function。 [J grad] = NNCOSTFUNCTON(nn_params, hidden_layer_size, num_labels, ..., X, y, lambda)
computes the cost and gradient of the neural network. [J grad] = NNCOSTFUNCTON(nn_params, hidden_layer_size, num_labels, ..., X, y, lambda)
计算神经网络的成本和梯度。 The parameters for the neural network are "unrolled" into the vector nn_params and need to be converted back into the weight matrices.神经网络的参数被“展开”到向量 nn_params 中,需要转换回权重矩阵。
The returned parameter grad should be an "unrolled" vector of the partial derivatives of the neural network.返回的参数 grad 应该是神经网络的偏导数的“展开”向量。
Reshape nn_params
back into the parameters Theta1
and Theta2
, the weight matrices. nn_params
重新整形为参数Theta1
和Theta2
,即权重矩阵。 For 2-layer neural network:对于 2 层神经网络:
Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
hidden_layer_size, (input_layer_size + 1));
Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
num_labels, (hidden_layer_size + 1));
m = size(X, 1);
I need to return the following variables correctly:我需要正确返回以下变量:
J = 0;
Theta1_grad = zeros(size(Theta1));
Theta2_grad = zeros(size(Theta2));
Sigmoid function is defined in another file and is recalled here to calculate h_theta1
and h_theta2
. Sigmoid function 在另一个文件中定义,在这里调用以计算h_theta1
和h_theta2
。
%Sigmoid function:
function g = sigmoid(z)
%SIGMOID Compute sigmoid function
% J = SIGMOID(z) computes the sigmoid of z.
g = 1.0 ./ (1.0 + exp(-z));
end
Feedforward the neural network and return the cost in the variable J:前馈神经网络并返回变量 J 中的成本:
X = [ones(m, 1) X];
h_theta1=sigmoid(X*Theta1');
h_theta1=[ones(m,1) h_theta1];
h_theta2=sigmoid(h_theta1*Theta2');
y_2=zeros(5000,10);
for k=1:10
condition=y(:,1)==k;
y_2(condition,k)=1;
end
for i=1:m
for k=1:num_labels
e(i,k)=-y_2(i,k)'*log(h_theta2(i,k))-(1-y_2(i,k)')*log(1-h_theta2(i,k));
end
end
J=(1/m)*sum(e);
J=sum(J);
Theta_1=Theta1;
Theta_2=Theta2;
Theta_1(:,1)=[];
Theta_2(:,1)=[];
%Regularized cost function:
J=J+(lambda/(2*m))*(sum(sum(Theta_1.*Theta_1))+sum(sum(Theta_2.*Theta_2)));
%Gradient calculation
delta3=h_theta2-y_2;
delta2=(delta3*Theta2).*h_theta1.*(1-h_theta1);
Theta2_grad=Theta2_grad+delta3'*h_theta1;
Theta2_grad=(1/m)*Theta2_grad;
delta_2=delta2;
delta_2(:,1)=[];
Theta1_grad=Theta1_grad+delta_2'*X;
Theta1_grad=(1/m)*Theta1_grad;
I then submit the above code using a submit()
function in Octave.然后我在 Octave 中使用submit()
function 提交上述代码。 The code works for J
calculation but then gives the following error:该代码适用于J
计算,但随后出现以下错误:
octave:80> submit()
== Submitting solutions | Neural Networks Learning...
Use token from last successful submission? (Y/n): Y
!! Submission failed: operator -: nonconformant arguments
(op1 is 16x4, op2 is 5000x10)
Function: nnCostFunction
LineNumber: 98
Please correct your code and resubmit.
Any help would be much appreciated.任何帮助将非常感激。
I figured out where the problem was.我弄清楚问题出在哪里。 The thing is the grader tests my answer with a totally different dataset and I had created y_2
with fixed dimensions.问题是评分者使用完全不同的数据集测试我的答案,并且我创建了具有固定尺寸的y_2
。 What I should've done instead was to create y_2
as follows:我应该做的是按如下方式创建y_2
:
y_2=zeros(m,num_labels);
for k=1:num_labels
condition=y(:,1)==k;
y_2(condition,k)=1;
end
Which makes the code work for any value of m and num_labels.这使得代码适用于 m 和 num_labels 的任何值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.