- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Matlab 创建了反向传播神经网络。我尝试使用 Matlab 实现异或门,然后获取其权重和偏差以在 java 中创建神经网络。网络由 2 个输入神经元、2 个隐藏层组成,每个隐藏层使用 2 个神经元和 1 个输出神经元。训练网络后,我得到以下权重和偏差:
clear;
clc;
i = [0 0 1 1; 0 1 0 1];
o = [0 1 1 0];
net = newff(i,o,{2,2},{'tansig','logsig','purelin'});
net.IW{1,1} = [
-5.5187 -5.4490;
3.7332 2.7697
];
net.LW{2,1} = [
-2.8093 -3.0692;
-1.6685 6.7527
];
net.LW{3,2} = [
-4.9318 -0.9651
];
net.b{1,1} = [
2.1369;
2.6529
];
net.b{2,1} = [
-0.2274;
-4.9512
];
net.b{3,1} = [
1.4848
];
input = net.IW{1,1};
layer = net.LW{2,1};
output = net.LW{3,2};
biasinput = net.b{1,1};
biaslayer = net.b{2,1};
biasoutput= net.b{3,1};
a = sim(net,i);
a;
我使用 1 和 1 作为输入进行模拟,得到以下结果:
>> f = [1;1]
f =
1
1
>> sim(net,f)
ans =
-0.1639
然后我尝试编写简单的 java 代码来计算这个神经网络。我的代码:
public class Xor {
//Value of neuron
static double[] neuroninput = new double[2];
static double[] neuronhidden1 = new double[2];
static double[] neuronhidden2 = new double[2];
static double[] neuronoutput = new double[2];
//Weight variable init
//For first hidden layer
static double[] weighthidden11 = new double[2];
static double[] weighthidden12 = new double[2];
//for second hidden layer
static double[] weighthidden21 = new double[2];
static double[] weighthidden22 = new double[2];
//for output layer
static double[] weightoutput = new double[2];
//End of weight variable init
//Bias value input
static double[] biashidden1 = new double[2];
static double[] biashidden2 = new double[2];
static double[] biasoutput = new double[1];
public static void main(String[] args) {
neuroninput[0] = 1;
neuroninput[1] = 1;
weighthidden11[0] = -5.5187;
weighthidden11[1] = -5.4490;
weighthidden12[0] = 3.7332;
weighthidden12[1] = 2.7697;
weighthidden21[0] = -2.8093;
weighthidden21[1] = -3.0692;
weighthidden22[0] = -1.6685;
weighthidden22[1] = 6.7527;
weightoutput[0] = -4.9318;
weightoutput[1] = -0.9651;
biashidden1[0] = 2.1369;
biashidden1[1] = 2.6529;
biashidden2[0] = -0.2274;
biashidden2[1] = -4.9512;
biasoutput[0] = 1.4848;
//Counting each neuron (Feed forward)
neuronhidden1[0] = sigma(neuroninput,weighthidden11,biashidden1[0]);
neuronhidden1[0] = tansig(neuronhidden1[0]);
neuronhidden1[1] = sigma(neuroninput,weighthidden12,biashidden1[1]);
neuronhidden1[1] = tansig(neuronhidden1[1]);
neuronhidden2[0] = sigma(neuronhidden1,weighthidden21,biashidden2[0]);
neuronhidden2[0] = logsig(neuronhidden2[0]);
neuronhidden2[1] = sigma(neuronhidden1,weighthidden22,biashidden2[1]);
neuronhidden2[1] = logsig(neuronhidden2[1]);
neuronoutput[0] = sigma(neuronhidden2,weightoutput,biasoutput[0]);
neuronoutput[0] = purelin(neuronoutput[0]);
System.out.println(neuronoutput[0]);
}
static double tansig(double x) {
double value = 0;
value = (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
return value;
}
static double logsig(double x) {
double value = 0;
value = 1 / (1+Math.exp(-x));
return value;
}
static double purelin(double x) {
double value = x;
return value;
}
static double sigma(double[] val, double[] weight, double hidden) {
double value = 0;
for (int i = 0; i < val.length; i++) {
value += (val[i] * weight[i]);
//System.out.println(val[i]);
}
value += hidden;
return value;
}
}
但得到的结果如下:
-1.3278721528152158
我的问题,将权重和偏差值从 matlab 导出到 java 时是否有任何错误或我的错误?也许我在我的java程序中犯了错误?非常感谢..
最佳答案
我认为问题在于标准化: http://www.mathworks.com/matlabcentral/answers/14590
如果您使用 0,1 输入,则必须使用 f(x)=2*x-1 标准化函数,该函数将值转换为 [-1; 1] 间隔,然后 g(x)=(x+1)/2 将输出变换回 [0; 1]。伪代码:
g( java_net( f(x), f(y) ) ) = matlab_net(x, y)
我在其他网络上尝试过此操作并为我工作。
关于java - 如何正确将反向传播神经网络的权重和偏差值导出到另一种编程语言(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14229224/
我有几个系统,其中包含用户表以及某种形式的业力/权重/声誉。有时是用户发布的帖子数量,有时是用户在网站上的所有事件中收到的赞成/反对票数。 USER { id int name str
我需要能够使用填充的相对大小 - 根据设备的分辨率和 dpi 变大或变小。 例如,如果我的 View 宽度为 100 像素,我希望左侧填充 10 像素,右侧填充 10 像素。但是,如果它在更高密度的屏
我目前正在使用由大约 10 个字段组成的 Solr 索引数据。当我执行搜索时,我希望某些字段的权重更高。谁能帮我指出正确的方向? 例如,在所有字段中搜索“超人”等术语时,应在“Description”
我正在使用 igraph 模拟网络随时间的变化在 r并且我正在寻找一种有效且可扩展的方式来对此进行编码以用于业务。 网络变化的主要驱动因素是: 新增节点 新领带 新节点权重 在第一阶段,在 100 个
我一直在寻找一种使用OpenVINO框架上的C++ API获取网络每一层权重/参数和偏差张量的方法。我在文档中找不到任何内容,在示例中也找不到任何示例。我如何提取这些张量? 谢谢, 塞萨尔 编辑: 分
我的问题与 PHP Memcache 扩展的 addServer 函数中的“weight”参数有关。 在过去的几个月里,我一直在为所有服务器使用“weight = 1”。我现在正在尝试应用以下配置以最
我应该使用哪种数据结构来保持元素按给定权重排序?我需要在集合中添加元素,其中每个元素都会生成特定的权重,但该权重不包含(也不计算)在元素本身内部;它是由元素之外的其他人计算的。而且,权重不需要存储(但
我正在尝试在 keras/tensorflow 中使用具有多个类的焦点损失,这导致使用我猜的分类焦点损失。我找到了一些实现here和 there或there 。 据我了解,焦点损失中的参数a主要用于二
我有一个像这样的 Pandas 数据框: df = pd.DataFrame({'id': [121, 34324, 111, 12, 45, 232], 'weight'
我有一个带有输出神经元的神经网络,我想在软最大化之前使用经过训练的权重进行线性缩放。 我有 10 个输出,我想要 10 个权重,在输出被软最大化之前乘以每个输出。因此每个输出的权重为 1。 但我不断收
我有这样的布局: 我希望它看起来像: TextView - 宽度的 40%,带 ImageView 的布局
这让我彻底疯了。我想在 android 中使用有点复杂的布局。我正在尝试使用权重而不是固定事物的大小。也许我应该放弃…… 这是我想做的: 我想要 3 个 ScrollView (里面有 TextVie
我正在尝试开发类似于网格但使用 LinearLayout。我想在单行中有 3 张图像和图像后的确切底部文本。 我尝试过的: LinearLayout layout = new LinearLayout
我想在同一行添加一个 EditText 和一个 Button,我想将 80% 的行给 editText,20% 给 Button。 这是我的代码:
我有一个 mysql 表,其中存储一列(称为 tickets),并且 tickets 值可以是任意数字。 我想要做的是有一个mysql查询,从列中选择所有行,按降序对它们进行排序,然后使用PHP对查询
我搜索了一段时间,但结果让我很困惑,因为我对 MySQL 还很陌生。 我有一个包含这 4 列的表:AUTO_INCREMENT ID、NAME、TYPE、CHANCE 所以行看起来像这样: 1, NO
我不完全是 JS 专业人士,虽然不漂亮或高效,但这是有效的。 实际上,我在表单中有重复的字段组,并允许用户根据需要将数据从第一个字段复制到所有 5 个字段。 如何使这段代码更高效? function
我正在使用xgboost库来训练二元分类器。我想通过向权重添加噪声(例如集合中树的叶节点的值)来防止训练算法的数据泄漏。为此,我需要检索每棵树的权重并修改它们。 我可以通过在 Booster 对象上使
我正在尝试让 LinearLayout 中的 View 填充宽度。我尝试使用 LayoutParams 设置它,但它给了我一个错误: 我的代码: EditText et = new EditText(
我想改变 ScrollView 的权重,但找不到实现它的方法。 这是我的 XML: **other layout.. 从代码中只能设置scrollView的高度或者宽度,weigh
我是一名优秀的程序员,十分优秀!