我正在玩一个ANN,这是Udacity DeepLearning课程的一部分.
我有一个任务,涉及使用L2丢失将一个隐藏的ReLU层引入网络.我想知道如何正确地引入它,以便所有权重都受到惩罚,而不仅仅是输出层的权重.
没有概括的网络代码位于帖子的底部(实际运行培训的代码超出了问题的范围).
引入L2的明显方法是用这样的方法替换损失计算(如果beta为0.01):
loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(out_layer, tf_train_labels) + 0.01*tf.nn.l2_loss(out_weights))
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,它会考虑输出层权重的值.我不确定,我们如何正确地惩罚进入隐藏的ReLU层的权重.是否需要它或引入输出层的惩罚将以某种方式保持隐藏的权重也检查?
#some importing
from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
from six.moves import range
#loading data
pickle_file = '/home/maxkhk/Documents/Udacity/DeepLearningCourse/SourceCode/tensorflow/examples/udacity/notMNIST.pickle'
with open(pickle_file, 'rb') as f:
save = pickle.load(f)
train_dataset = save['train_dataset']
train_labels = save['train_labels']
valid_dataset = save['valid_dataset']
valid_labels = save['valid_labels']
test_dataset = save['test_dataset']
test_labels = save['test_labels']
del save # hint to help gc free up memory
print('Training …Run Code Online (Sandbox Code Playgroud) machine-learning neural-network regularized deep-learning tensorflow
我目前正在玩ANN,这是Udactity DeepLearning课程的一部分.
我成功建立并培训了网络,并在所有权重和偏差上引入了L2正则化.现在我正在尝试隐藏图层的丢失,以便改进泛化.我想知道,将L2正则化引入隐藏层并在同一层上丢失是否有意义?如果是这样,如何正确地做到这一点?
在辍学期间,我们实际上关闭了隐藏层的一半激活并使其余神经元输出的量加倍.在使用L2时,我们计算所有隐藏权重的L2范数.但我不知道如何使用dropout来计算L2.我们关闭了一些激活,我们不应该从L2计算中删除现在"未使用"的权重吗?关于这个问题的任何参考都会有用,我还没有找到任何信息.
如果您有兴趣,我的具有L2正规化的ANN代码如下:
#for NeuralNetwork model code is below
#We will use SGD for training to save our time. Code is from Assignment 2
#beta is the new parameter - controls level of regularization. Default is 0.01
#but feel free to play with it
#notice, we introduce L2 for both biases and weights of all layers
beta = 0.01
#building tensorflow graph
graph = tf.Graph()
with graph.as_default():
# Input data. For the training data, we use a placeholder …Run Code Online (Sandbox Code Playgroud) machine-learning neural-network regularized deep-learning tensorflow
我是第一次使用Python Pandas.我有csv格式的5分钟滞后流量数据:
...
2015-01-04 08:29:05,271238
2015-01-04 08:34:05,329285
2015-01-04 08:39:05,-1
2015-01-04 08:44:05,260260
2015-01-04 08:49:05,263711
...
Run Code Online (Sandbox Code Playgroud)
有几个问题:
我想获得一个固定的时间序列,所以每隔(完全)5分钟(并且没有丢失的价值)的条目.我已成功使用以下代码插入时间序列,以使用此代码逼近-1值:
ts = pd.TimeSeries(values, index=timestamps)
ts.interpolate(method='cubic', downcast='infer')
Run Code Online (Sandbox Code Playgroud)
我怎样才能插入和规范观测的频率?谢谢大家的帮助.
我有一个严重条件矩阵,它rcond()接近于零,因此,该矩阵的逆矩阵并不正确.我尝试过使用,pinv()但这并没有解决问题.这就是我采取相反的方式:
X = (A)\(b);
Run Code Online (Sandbox Code Playgroud)
我查找了这个问题的解决方案,并找到了改进矩阵的链接(最后的解决方案).那里的解决方案建议使用这个:
A_new = A_old + c*eye(size(A_old));
Run Code Online (Sandbox Code Playgroud)
哪里c > 0.到目前为止,采用这种技术可以使基质A更好地调节,并且所得溶液看起来更好.但是,我研究了使用不同的值,c所得到的解决方案取决于所选择的值c.
除了手动调查价值之外c,是否有一种自动方式可以找到c我获得最佳解决方案的价值?
我正在尝试使用正规化的LR,在matlab中使用这个公式很简单:
成本函数:
J(theta) = 1/m*sum((-y_i)*log(h(x_i)-(1-y_i)*log(1-h(x_i))))+(lambda/2*m)*sum(theta_j)
Run Code Online (Sandbox Code Playgroud)
渐变:
?J(theta)/?theta_0 = [(1/m)*(sum((h(x_i)-y_i)*x_j)] if j=0
?j(theta)/?theta_n = [(1/m)*(sum((h(x_i)-y_i)*x_j)]+(lambda/m)*(theta_j) if j>1
Run Code Online (Sandbox Code Playgroud)
这不是matlab代码只是公式.
到目前为止我已经这样做了:
function [J, grad] = costFunctionReg(theta, X, y, lambda)
J = 0;
grad = zeros(size(theta));
temp_theta = [];
%cost function
%get the regularization term
for jj = 2:length(theta)
temp_theta(jj) = theta(jj)^2;
end
theta_reg = lambda/(2*m)*sum(temp_theta);
temp_sum =[];
%for the sum in the cost function
for ii =1:m
temp_sum(ii) = -y(ii)*log(sigmoid(theta'*X(ii,:)'))-(1-y(ii))*log(1-sigmoid(theta'*X(ii,:)'));
end
tempo = sum(temp_sum);
J = (1/m)*tempo+theta_reg;
%regulatization
%theta 0
reg_theta0 = 0; …Run Code Online (Sandbox Code Playgroud) Keras辍学的实施参考了本文.
以下摘录来自该文件:
这个想法是在测试时使用单个神经网络而不会丢失.该网络的权重是训练权重的缩小版本.如果在训练期间以概率p保留单位,则在测试时间将该单位的输出权重乘以p,如图2所示.
Keras文档提到丢失仅用于列车时间,以及Dropout实施中的以下行
x = K.in_train_phase(K.dropout(x, level=self.p), x)
似乎表明确实来自图层的输出只是在测试时间内传递.
此外,我无法找到在训练完成后按比例缩小权重的代码.我的理解是,这个缩放步骤对于使丢失工作从根本上是必要的,因为它相当于在"子网络"集合中获取中间层的预期输出.没有它,计算就不再被视为从这个"子网络"集合中进行采样.
那么,我的问题是,在Keras实施的辍学的缩放效果在哪里呢?
更新1:好的,所以Keras使用反向丢失,虽然在Keras文档和代码中称为dropout.链接http://cs231n.github.io/neural-networks-2/#reg似乎并不表示两者是等价的.回答也没有https://stats.stackexchange.com/questions/205932/dropout-scaling-the-activation-versus-inverting-the-dropout.我可以看到他们做了类似的事情,但我还没有看到有人说他们完全一样.我认为他们不是.
所以一个新问题:辍学和倒退辍学相当吗?要清楚,我正在寻找说出他们是否存在的数学理由.
machine-learning neural-network regularized deep-learning keras
我正在使用本教程关于autoencoders:https://blog.keras.io/building-autoencoders-in-keras.html
所有代码都正常工作,但是当我为正则化参数设置10e-5时性能非常糟糕(结果很模糊),这是教程代码中定义的参数.实际上,我需要将正则化减少到10e-8以获得正确的输出.
我的问题如下:为什么结果与教程有如此不同?数据是相同的,参数是相同的,我没想到会有很大的差异.
我怀疑从2016年5月14日起Keras功能的默认行为已经改变(在所有情况下都执行自动批量标准化?).
输出:
用10e-5正则化(模糊); 50个时期后val_loss为0.2967,100个时期后为0.2774
使用10e-8正则化:50个历元后的val_loss为0.1080,100个历元后为0.1009.
没有正则化:50个时期后val_loss为0.1018,100个时期后为0.0944.
完整代码(供参考):
# Source: https://blog.keras.io/building-autoencoders-in-keras.html
import numpy as np
np.random.seed(2713)
from keras.layers import Input, Dense
from keras.models import Model
from keras import regularizers
encoding_dim = 32
input_img = Input(shape=(784,))
# add a Dense layer with a L1 activity regularizer
encoded = Dense(encoding_dim, activation='relu',
activity_regularizer=regularizers.l1(10e-5))(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
# create …Run Code Online (Sandbox Code Playgroud) 我试图在Keras中设置一个非线性回归问题.不幸的是,结果表明过度拟合正在发生.这是代码,
model = Sequential()
model.add(Dense(number_of_neurons, input_dim=X_train.shape[1], activation='relu', kernel_regularizer=regularizers.l2(0)))
model.add(Dense(int(number_of_neurons), activation = 'relu', kernel_regularizer=regularizers.l2(0)))
model.add(Dense(int(number_of_neurons), activation='relu', kernel_regularizer=regularizers.l2(0)))
model.add(Dense(int(number_of_neurons), activation='relu',kernel_regularizer=regularizers.l2(0)))
model.add(Dense(int(number_of_neurons), activation='relu',kernel_regularizer=regularizers.l2(0)))
model.add(Dense(outdim, activation='linear'))
Adam = optimizers.Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=Adam, metrics=['mae'])
model.fit(X, Y, epochs=1000, batch_size=500, validation_split=0.2, shuffle=True, verbose=2 , initial_epoch=0)
Run Code Online (Sandbox Code Playgroud)
没有正则化的结果在这里显示没有正则化.与验证相比,训练的平均绝对误差要小得多,并且两者都有固定的间隙,这是过度拟合的标志.
像这样为每一层指定了L2正则化,
model = Sequential()
model.add(Dense(number_of_neurons, input_dim=X_train.shape[1], activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(Dense(int(number_of_neurons), activation = 'relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(Dense(int(number_of_neurons), activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(Dense(int(number_of_neurons), activation='relu',kernel_regularizer=regularizers.l2(0.001)))
model.add(Dense(int(number_of_neurons), activation='relu',kernel_regularizer=regularizers.l2(0.001)))
model.add(Dense(outdim, activation='linear'))
Adam = optimizers.Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=Adam, metrics=['mae'])
model.fit(X, Y, epochs=1000, batch_size=500, validation_split=0.2, shuffle=True, verbose=2 , initial_epoch=0)
Run Code Online (Sandbox Code Playgroud)
这些结果显示在这里L2正则化结果.测试的MAE接近培训,这很好.然而,训练的MAE很差,为0.03(没有正规化,它在0.0028处低得多). …
当使用Tensorflows Object Detection API训练对象检测DNN时,它的可视化平台Tensorboard绘制了一个标量 regularization_loss_1
这是什么?我知道什么是正则化(使网络能够很好地通过各种方法(如辍学)进行概括),但我不清楚这种显示的损失可能是什么。
谢谢!
具有大量参数的ML模型将倾向于过度拟合(因为它们具有大的变化).在我看来,word2vec就是这样一个模型.减少模型方差的方法之一是应用正则化技术,这对于其他嵌入模型是非常常见的,例如矩阵分解.但是,基本版本word2vec没有任何正规化部分.是否有一个原因?
regularized ×10
keras ×3
tensorflow ×3
matlab ×2
python ×2
autoencoder ×1
embedding ×1
matrix ×1
nlp ×1
pandas ×1
regression ×1
tensorboard ×1
time-series ×1
word2vec ×1