import java.util.Random;
public class SdA {
public int N;
public int n_ins;
public int[] hidden_layer_sizes;
public int n_outs;
public int n_layers;
public HiddenLayer[] sigmoid_layers;
public dA[] dA_layers;
public LogisticRegression log_layer;
public Random rng;
public static double sigmoid(double x) {
return 1.0 / (1.0 + Math.pow(Math.E, -x));
}
public SdA(int N, int n_ins, int[] hidden_layer_sizes, int n_outs, int n_layers, Random rng) {
int input_size;
this.N = N;
this.n_ins = n_ins;
this.hidden_layer_sizes = hidden_layer_sizes;
this.n_outs = n_outs;
this.n_layers = n_layers;
this.sigmoid_layers = new HiddenLayer[n_layers];
this.dA_layers = new dA[n_layers];
if(rng == null) this.rng = new Random(1234);
else this.rng = rng;
// construct multi-layer
for(int i=0; i<this.n_layers; i++) {
if(i == 0) {
input_size = this.n_ins;
} else {
input_size = this.hidden_layer_sizes[i-1];
}
// construct sigmoid_layer
this.sigmoid_layers[i] = new HiddenLayer(this.N, input_size, this.hidden_layer_sizes[i], null, null, rng);
// construct dA_layer
this.dA_layers[i] = new dA(this.N, input_size, this.hidden_layer_sizes[i], this.sigmoid_layers[i].W, this.sigmoid_layers[i].b, null, rng);
}
// layer for output using LogisticRegression
this.log_layer = new LogisticRegression(this.N, this.hidden_layer_sizes[this.n_layers-1], this.n_outs);
}
public void pretrain(int[][] train_X, double lr, double corruption_level, int epochs) {
int[] layer_input = new int[0];
int prev_layer_input_size;
int[] prev_layer_input;
for(int i=0; i<n_layers; i++) { // layer-wise
for(int epoch=0; epoch<epochs; epoch++) { // training epochs
for(int n=0; n<N; n++) { // input x1...xN
// layer input
for(int l=0; l<=i; l++) {
if(l == 0) {
layer_input = new int[n_ins];
for(int j=0; j<n_ins; j++) layer_input[j] = train_X[n][j];
} else {
if(l == 1) prev_layer_input_size = n_ins;
else prev_layer_input_size = hidden_layer_sizes[l-2];
prev_layer_input = new int[prev_layer_input_size];
for(int j=0; j<prev_layer_input_size; j++) prev_layer_input[j] = layer_input[j];
layer_input = new int[hidden_layer_sizes[l-1]];
sigmoid_layers[l-1].sample_h_given_v(prev_layer_input, layer_input);
}
}
dA_layers[i].train(layer_input, lr, corruption_level);
}
}
}
}
public void finetune(int[][] train_X, int[][] train_Y, double lr, int epochs) {
int[] layer_input = new int[0];
// int prev_layer_input_size;
int[] prev_layer_input = new int[0];
for(int epoch=0; epoch<epochs; epoch++) {
for(int n=0; n<N; n++) {
// layer input
for(int i=0; i<n_layers; i++) {
if(i == 0) {
prev_layer_input = new int[n_ins];
for(int j=0; j<n_ins; j++) prev_layer_input[j] = train_X[n][j];
} else {
prev_layer_input = new int[hidden_layer_sizes[i-1]];
for(int j=0; j<hidden_layer_sizes[i-1]; j++) prev_layer_input[j] = layer_input[j];
}
layer_input = new int[hidden_layer_sizes[i]];
sigmoid_layers[i].sample_h_given_v(prev_layer_input, layer_input);
}
log_layer.train(layer_input, train_Y[n], lr);
}
// lr *= 0.95;
}
}
public void predict(int[] x, double[] y) {
double[] layer_input = new double[0];
// int prev_layer_input_size;
double[] prev_layer_input = new double[n_ins];
for(int j=0; j<n_ins; j++) prev_layer_input[j] = x[j];
double linear_output;
// layer activation
for(int i=0; i<n_layers; i++) {
layer_input = new double[sigmoid_layers[i].n_out];
linear_output = 0.0;
for(int k=0; k<sigmoid_layers[i].n_out; k++) {
for(int j=0; j<sigmoid_layers[i].n_in; j++) {
linear_output += sigmoid_layers[i].W[k][j] * prev_layer_input[j];
}
linear_output += sigmoid_layers[i].b[k];
layer_input[k] = sigmoid(linear_output);
}
if(i < n_layers-1) {
prev_layer_input = new double[sigmoid_layers[i].n_out];
for(int j=0; j<sigmoid_layers[i].n_out; j++) prev_layer_input[j] = layer_input[j];
}
}
for(int i=0; i<log_layer.n_out; i++) {
y[i] = 0;
for(int j=0; j<log_layer.n_in; j++) {
y[i] += log_layer.W[i][j] * layer_input[j];
}
y[i] += log_layer.b[i];
}
log_layer.softmax(y);
}
private static void test_sda() {
Random rng = new Random(123);
double pretrain_lr = 0.1;
double corruption_level = 0.3;
int pretraining_epochs = 1000;
double finetune_lr = 0.1;
int finetune_epochs = 500;
int train_N = 10;
int test_N = 4;
int n_ins = 28;
int n_outs = 2;
int[] hidden_layer_sizes = {15, 15};
int n_layers = hidden_layer_sizes.length;
// training data
int[][] train_X = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}
};
int[][] train_Y = {
{1, 0},
{1, 0},
{1, 0},
{1, 0},
{1, 0},
{0, 1},
{0, 1},
{0, 1},
{0, 1},
{0, 1}
};
// construct SdA
SdA sda = new SdA(train_N, n_ins, hidden_layer_sizes, n_outs, n_layers, rng);
// pretrain
sda.pretrain(train_X, pretrain_lr, corruption_level, pretraining_epochs);
// finetune
sda.finetune(train_X, train_Y, finetune_lr, finetune_epochs);
// test data
int[][] test_X = {
{1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1}
};
double[][] test_Y = new double[test_N][n_outs];
// test
for(int i=0; i<test_N; i++) {
sda.predict(test_X[i], test_Y[i]);
for(int j=0; j<n_outs; j++) {
System.out.print(test_Y[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
test_sda();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java写的深度学习算法代码
共11个文件
java:11个
4星 · 超过85%的资源 需积分: 34 148 下载量 81 浏览量
2015-10-18
14:01:35
上传
评论 3
收藏 11KB RAR 举报
温馨提示
用java写的深度神经网络的代码,里面有语音识别的demo,是研究深度学习和语音识别很好的资料
资源推荐
资源详情
资源评论
收起资源包目录
java.rar (11个子文件)
java
SdA
src
HiddenLayer.java 1KB
dA.java 3KB
SdA.java 7KB
LogisticRegression.java 1KB
LogisticRegression
src
LogisticRegression.java 2KB
DBN
src
DBN.java 6KB
HiddenLayer.java 1KB
RBM.java 4KB
LogisticRegression.java 1KB
RBM
src
RBM.java 5KB
dA
src
dA.java 5KB
共 11 条
- 1
资源评论
- zgszgs12017-02-20比较简单,缺少注释及介绍。
- wangdongj2ee2016-09-28值得学习,谢谢楼主
- 嫣婷2016-03-30值得学习一下,还可以
- lsq1122332016-09-30不是我想要的
- taxxuezc2016-02-26最近在研究深度学习,代码还是很好用的,只要简答的改动
沉默的杰克
- 粉丝: 20
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功