机器学习:Softmax Classifier (两个隐含层)
程序实现 softmax classifier, 含有两个隐含层的情况。activation function 是 ReLU : f(x)=max(0,x)
f1=w1x+b1
h1=max(0,f1)
f2=w2h1+b2
h2=max(0,f2)
f3=w3h2+b3
y=ef3i∑jef3j
function Out=Softmax_Classifier_2(train_x, train_y, opts)
% setting learning parameters
step_size=opts.step_size;
reg=opts.reg;
batchsize = opts.batchsize;
numepochs = opts.numepochs;
K=opts.class;
h1=opts.hidden_1;
h2=opts.hidden_2;
D=size(train_x, 2);
W1=0.01*randn(D,h1);
b1=zeros(1,h1);
W2=0.01*randn(h1, h2);
b2=zeros(1,h2);
W3=0.01*randn(h2, K);
b3=zeros(1, K);
loss(1 : numepochs)=0;
num_examples=size(train_x, 1);
numbatches = num_examples / batchsize;
for epoch=1:numepochs
kk = randperm(num_examples);
loss(epoch)=0;
tic;
sprintf('epoch %d: \n' , epoch)
for bat=1:numbatches
batch_x = train_x(kk((bat - 1) * batchsize + 1 : bat * batchsize), :);
batch_y = train_y(kk((bat - 1) * batchsize + 1 : bat * batchsize), :);
%% forward
f1=batch_x*W1+repmat(b1, batchsize, 1);
hiddenval_1=max(0, f1);
f2=hiddenval_1*W2+repmat(b2, batchsize, 1);
hiddenval_2=max(0, f2);
scores=hiddenval_2*W3+repmat(b3, batchsize, 1);
%% the loss
exp_scores=exp(scores);
dd=repmat(sum(exp_scores, 2), 1, K);
probs=exp_scores./dd;
correct_logprobs=-log(sum(probs.*batch_y, 2));
data_loss=sum(correct_logprobs)/batchsize;
reg_loss=0.5*reg*sum(sum(W1.*W1))+0.5*reg*sum(sum(W2.*W2))+0.5*reg*sum(sum(W3.*W3));
loss(epoch) =loss(epoch)+ data_loss + reg_loss;
%% back propagation
% output layer
dscores = probs-batch_y;
dscores=dscores/batchsize;
dW3=hiddenval_2'*dscores;
db3=sum(dscores);
% hidden layer 2
dhiddenval_2=dscores*W3';
mask=max(sign(hiddenval_2), 0);
df_2=dhiddenval_2.*mask;
dW2=hiddenval_1'*df_2;
db2=sum(df_2);
% hidden layer 1
dhiddenval_1=df_2*W2';
mask=max(sign(hiddenval_1), 0);
df_1=dhiddenval_1.*mask;
dW1=batch_x'*df_1;
db1=sum(df_1);
%% update
dW3=dW3+reg*W3;
dW2=dW2+reg*W2;
dW1=dW1+reg*W1;
W3=W3-step_size*dW3;
b3=b3-step_size*db3;
W2=W2-step_size*dW2;
b2=b2-step_size*db2;
W1=W1-step_size*dW1;
b1=b1-step_size*db1;
end
loss(epoch)=loss(epoch)/numbatches;
sprintf('training loss is %f: \n', loss(epoch))
toc;
end
Out.W1=W1;
Out.W2=W2;
Out.W3=W3;
Out.b1=b1;
Out.b2=b2;
Out.b3=b3;
Out.loss=loss;
机器学习:Softmax Classifier (两个隐含层)的更多相关文章
- 机器学习: Softmax Classifier (三个隐含层)
程序实现 softmax classifier, 含有三个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...
- 机器学习 Softmax classifier (一个隐含层)
程序实现 softmax classifier, 含有一个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...
- 机器学习 Softmax classifier (无隐含层)
程序实现 Softmax classifer, 没有隐含层, f=wx+b y=efi∑jefj %% Softmax classifier function Out=Softmax_Classifi ...
- 理解dropout——本质是通过阻止特征检测器的共同作用来防止过拟合 Dropout是指在模型训练时随机让网络某些隐含层节点的权重不工作,不工作的那些节点可以暂时认为不是网络结构的一部分,但是它的权重得保留下来(只是暂时不更新而已),因为下次样本输入时它可能又得工作了
理解dropout from:http://blog.csdn.net/stdcoutzyx/article/details/49022443 http://www.cnblogs.com/torna ...
- 基于MNIST数据集使用TensorFlow训练一个包含一个隐含层的全连接神经网络
包含一个隐含层的全连接神经网络结构如下: 包含一个隐含层的神经网络结构图 以MNIST数据集为例,以上结构的神经网络训练如下: #coding=utf-8 from tensorflow.exampl ...
- 基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络
基础 在参考①中我们详细介绍了没有隐含层的神经网络结构,该神经网络只有输入层和输出层,并且输入层和输出层是通过全连接方式进行连接的.具体结构如下: 我们用此网络结构基于MNIST数据集(参考②)进行训 ...
- [DeeplearningAI笔记]序列模型2.6Word2Vec/Skip-grams/hierarchical softmax classifier 分级softmax 分类器
5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.6 Word2Vec Word2Vec相对于原先介绍的词嵌入的方法来说更加的简单快速. Mikolov T, Chen ...
- ubuntu之路——day13 只用python的numpy在较为底层的阶段实现单隐含层神经网络
首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...
- 3.4 常用的两种 layer 层 3.7 字体与文本
3.4 常用的两种 layer 层 //在cocos2d-x中,经常使用到的两种 layer 层 : CCLayer 和 CCLayerColor //CCLayer 的创建 CCLayer* la ...
随机推荐
- 找不到或无法载入主类 org.jivesoftware.openfire.starter.ServerStarter
刚接触openfire的配置就出现了这个错误.解决方法非常easy,忘记了将openfire的源文件加入到user entries中了
- jquery模拟可输入的下拉框
//页面html <div id="select" class="select" > <ul> <c:forEach items= ...
- Android RxBus的实现及简单使用
RxJava目前已经很火了,如果你尚未了解请看这里.对于RxJava这里不多做介绍.RxBus并不是一个库,而是一种模式.相信大多数开发者都使用过EventBus,作为事件总线通信库,如果你的项目已经 ...
- python一切皆对象的理解
min_error=pls(x_train, x_test, y_train, y_test) #这里我之前写的是error,但是前面有一个定义的error函数.所以导致出现了警告. 可能是因为pyt ...
- ES6 数组、对象的扩展
8. 数组的扩展 扩展运算符(...),将一个数组转为用逗号分隔的参数序列. 复制数组 const a2=[...a1] 合并数组 [...arr1, ...arr2, ...arr3]; arr1. ...
- 网络博客 VC\图案像处理
http://blog.csdn.net/lvwx369/article/category/1185452 http://blog.csdn.net/lyy289065406/article/deta ...
- 洛谷 P1510 精卫填海
洛谷 P1510 精卫填海 题目描述 [版权说明] 本题为改编题. [问题描述] 发鸠之山,其上多柘木.有鸟焉,其状如乌,文首,白喙,赤足,名曰精卫,其名自詨.是炎帝之少女,名曰女娃.女娃游于东海,溺 ...
- Altium Designer设置走线间距
- JMS服务器ActiveMQ的初体验并持久化消息到MySQL数据库中
JMS服务器ActiveMQ的初体验并持久化消息到MySQL数据库中 一.JMS的理解JMS(Java Message Service)是jcp组织02-03年定义了jsr914规范(http://j ...
- [Node] Run Local DevDependencies from the Command Line with npx
In the past, you've needed to either write a package.json script or use the node_modules/.bin direct ...