我将尝试解释发生的情况和问题。
这有点数学,因此不支持乳胶,因此遗憾的是我不得不求助于图像。我希望可以。
我不知道为什么要倒转,对此感到抱歉。无论如何,这是一个线性系统Ax = b,我们知道A和b,因此我们可以找到x,这是我们在下一个时间步长上的近似值。我们将继续执行直到时间t_final。
这是代码
import numpy as np
tau = 2 * np.pi
tau2 = tau * tau
i = complex(0,1)
def solution_f(t, x):
return 0.5 * (np.exp(-tau * i * x) * np.exp((2 - tau2) * i * t) + np.exp(tau * i * x) * np.exp((tau2 + 4) * i * t))
def solution_g(t, x):
return 0.5 * (np.exp(-tau * i * x) * np.exp((2 - tau2) * i * t) - np.exp(tau * i * …Run Code Online (Sandbox Code Playgroud) 我一直在抨击这堵砖墙,看起来像是永恒的,我似乎无法绕过它.我正在尝试仅使用numpy和矩阵乘法来实现自动编码器.没有theano或keras技巧允许.
我将描述问题及其所有细节.它起初有点复杂,因为有很多变量,但它确实非常简单.
我们知道什么
1)X是一种m由n矩阵这是我们的输入.输入是该矩阵的行.每个输入都是一个n维度行向量,我们有m它们.
2)我们(单个)隐藏层中的神经元数量,即k.
3)我们的神经元(sigmoid,将表示为g(x))及其衍生物的激活功能g'(x)
我们不知道什么,想找到什么
总的来说,我们的目标是找到6个矩阵:w1这是n通过k,b1这是m通过k,w2这是k由n,B2是m通过n,w3这是n由n和b3是m由n.
它们随机初始化,我们找到使用梯度下降的最佳解决方案.
这个过程
首先我们计算z1 = Xw1+b1.它是m通过k并输入到我们的隐藏层.然后我们计算h1 = g(z1),它只是将sigmoid函数应用于所有元素z1.当然它也是m通过k和我们的隐层的输出.
然后,我们计算 …
我正在尝试编写一个获取A任意大小矩阵的程序,并且SVD将其分解:
A = U * S * V'
Run Code Online (Sandbox Code Playgroud)
A用户输入的矩阵在哪里,U正交矩阵由特征值的特征向量组成A * A',S是奇异值的对角矩阵,并且V是特征向量的正交矩阵A' * A.
问题是:MATLAB函数eig有时会返回错误的特征向量.
这是我的代码:
function [U,S,V]=badsvd(A)
W=A*A';
[U,S]=eig(W);
max=0;
for i=1:size(W,1) %%sort
for j=i:size(W,1)
if(S(j,j)>max)
max=S(j,j);
temp_index=j;
end
end
max=0;
temp=S(temp_index,temp_index);
S(temp_index,temp_index)=S(i,i);
S(i,i)=temp;
temp=U(:,temp_index);
U(:,temp_index)=U(:,i);
U(:,i)=temp;
end
W=A'*A;
[V,s]=eig(W);
max=0;
for i=1:size(W,1) %%sort
for j=i:size(W,1)
if(s(j,j)>max)
max=s(j,j);
temp_index=j;
end
end
max=0;
temp=s(temp_index,temp_index);
s(temp_index,temp_index)=s(i,i);
s(i,i)=temp;
temp=V(:,temp_index);
V(:,temp_index)=V(:,i);
V(:,i)=temp;
end
s=sqrt(s);
end
Run Code Online (Sandbox Code Playgroud)
我的代码返回正确的s矩阵,也"几乎"正确U …
我正在尝试实现一个区分k不同类的多类逻辑回归分类器.
这是我的代码.
import numpy as np
from scipy.special import expit
def cost(X,y,theta,regTerm):
(m,n) = X.shape
J = (np.dot(-(y.T),np.log(expit(np.dot(X,theta))))-np.dot((np.ones((m,1))-y).T,np.log(np.ones((m,1)) - (expit(np.dot(X,theta))).reshape((m,1))))) / m + (regTerm / (2 * m)) * np.linalg.norm(theta[1:])
return J
def gradient(X,y,theta,regTerm):
(m,n) = X.shape
grad = np.dot(((expit(np.dot(X,theta))).reshape(m,1) - y).T,X)/m + (np.concatenate(([0],theta[1:].T),axis=0)).reshape(1,n)
return np.asarray(grad)
def train(X,y,regTerm,learnRate,epsilon,k):
(m,n) = X.shape
theta = np.zeros((k,n))
for i in range(0,k):
previousCost = 0;
currentCost = cost(X,y,theta[i,:],regTerm)
while(np.abs(currentCost-previousCost) > epsilon):
print(theta[i,:])
theta[i,:] = theta[i,:] - learnRate*gradient(X,y,theta[i,:],regTerm)
print(theta[i,:])
previousCost = currentCost
currentCost …Run Code Online (Sandbox Code Playgroud) python logarithm machine-learning divide-by-zero logistic-regression
我知道这是一个非常基本的问题,但我需要帮助理解这个简短的概念.
我正在学习Python,书中说"Python中的每个变量都是一个指向对象的指针.所以当你写出像y=x你这样的东西时,它们都会指向同一个对象.如果你改变原始对象,你会更改指向它的每个其他指针"
然后他们举个例子:
x=[1,2,3]
y=x
x[1]=3
print y
Run Code Online (Sandbox Code Playgroud)
它确实打印 [1,3,3]
但是,当我写下面的代码时:
x=5
y=x
x=7
print y
Run Code Online (Sandbox Code Playgroud)
它不打印7.它打印5.
为什么?
我正在尝试创建一个头文件(包括我为AVL Trees编写的函数),但我有一个小问题和对包含警卫的语法的误解.
现在我的代码看起来像这样
#ifndef STDIO_H
#define STDIO_H
#endif
#ifndef STDLIB_H
#define STDLIB_H
#endif
#ifndef CONIO_H
#define CONIO_H
#endif
Run Code Online (Sandbox Code Playgroud)
问题是,我认为它只包括<stdio.h>.当我尝试使用malloc时,它表示malloc未定义,即使我包含了stdlib.
根据http://www.cprogramming.com/reference/preprocessor/ifndef.html,如果我理解正确,ifndef检查是否定义了令牌,如果它不是,它定义我在ifndef之后写的所有内容,直到#endif.所以我的代码应该工作.
是否定义了stdio?没有.所以定义它.万一.是stdlib定义的吗?没有.所以定义它.万一.是conio定义的吗?没有.所以定义它.万一.我没有看到问题.
如果我想添加这3个标题,那么正确的语法是什么?
我正在尝试编写一个程序,为现有的txt文件添加行号.
例如,如果文件当前是:
Hello
this is
an
exercise
Run Code Online (Sandbox Code Playgroud)
然后运行代码后,它将是:
(1) Hello
(2) this is
(3) an
(4) exercise
Run Code Online (Sandbox Code Playgroud)
我写了这段代码:
#include<stdio.h>
#include<conio.h>
FILE *fp;
void main()
{
int counter=1;
char newline;
fp=fopen("G:\\name.txt","r+");
if(fp==NULL)
printf("Failed to open file!");
fprintf(fp,"(%d)",counter);
newline=fgetc(fp);
while(newline!=EOF)
{
if(newline=='\n')
{
counter++;
fprintf(fp,"(%d)",counter);
}
newline=fgetc(fp);
}
printf("All done!");
getch();
fclose(fp);
}
Run Code Online (Sandbox Code Playgroud)
输出很奇怪.
首先,它不会在文件的开头打印.出于某种原因,它从文件的末尾开始.而另一件奇怪的事情是,只有第一次印刷是成功的.
while循环中的那些是胡言乱语(看起来像小点,根本不像数字)
当我在fopen中使用"r +"时,整个数据被删除,我所能看到的只有(1)然后是乱码.
如果我在fopen中使用"a +",它会从文件末尾开始,然后写入(1)和乱码.
首先,我要道歉,英语不是我的母语,我无法想出一个更适合我的情况的头衔.
我得到了这个不完整的汇编代码:
.code
mov [mybyte],______
mov SP,0574h
xor ax,ax
here:
add AL,[mybyte]
push AX
dec BYTE PTR [mybyte]
jnz here
pop es
nop
Run Code Online (Sandbox Code Playgroud)
问题是:_____应该写什么,这样当我们达到"nop"命令时,SP的值将是570.
我理解这个问题,我认为我也理解代码,但问题是,我看到它的方式 - SP永远不会改变.代码中唯一引用SP的位置在该行mov sp,0574h.所以无论我们在____所在的位置写什么,SP都不会改变.
我对么?或者我误解了代码?
作为我正在研究的项目的一部分,我需要计算2m向量之间的均方误差.
基本上我两个矩阵x和xhat,两者都是大小m通过n与我感兴趣的载体是这些矢量的行.
我用这段代码计算了MSE
def cost(x, xhat): #mean squared error between x the data and xhat the output of the machine
return (1.0/(2 * m)) * np.trace(np.dot(x-xhat,(x-xhat).T))
Run Code Online (Sandbox Code Playgroud)
它工作正常,这个公式是正确的.
问题是,在我的具体情况下,我m和n非常大.特别是m = 60000和n = 785.因此,当我运行我的代码并进入此函数时,我收到内存错误.
有没有更好的方法来计算MSE?我宁愿避免循环,而是我倾向于矩阵乘法,但矩阵乘法似乎非常浪费.也许是numpy我不知道的东西?
我的代码中某处出现错误,我想我正在进入无限循环.基本上我得到一个矩阵和两个索引,i,j,我需要计算[i] [j]周围有多少邻居的值为1或2.
这是我的代码:
int number_of_living_neighbors(matrix mat,int i, int j, int n,int m)
{
int counter=0,row_index=0,column_index=0;
for(row_index=i-1;row_index<=i+1;row_index++)
{
for(column_index=j-1;column_index=j+1;column_index++)
{
if((row_index>=0)&&(row_index<n)&&(column_index>=0)&&(column_index<m))
{
if((mat[row_index][column_index]==1)||(mat[row_index][column_index]==2))
counter++;
if((row_index==i)&&(column_index==j)&&(mat[i][j]==1))
counter--;
}
}
}
printf("The number of living neighbors is %d", counter);
return counter;
}
Run Code Online (Sandbox Code Playgroud)
它什么都不打印.mat是我得到的矩阵,i,j是指针,n是行数,m是列数.
我正在尝试编写一个获取字符串的程序,并计算在特定文件中找到该字符串的次数.
该文件目前是:你好我的名字你好oria我喜欢你好编程
而我所说的这个词是你好.
这是我的代码
int num_of_words(FILE* stream,char* str)
{
int count=0,i=0,length;
char c;
rewind(stream);
length=strlen(str);
do
{
c=fgetc(stream);
while(c==*(str+i))
{
c=fgetc(stream);
i++;
if(i==length)
{
count++;
i=0;
}
}
i=0;
}while(c!=EOF);
return count;
}
Run Code Online (Sandbox Code Playgroud)
这个想法是有一个叫做i的索引,只有当字母之间有匹配时它才会前进.如果我达到字符串的长度,那么这意味着我们连续发现了所有字母,并且我将计数提高了一个.
出于某种原因,它总是返回零.